Cargo CRAP
cargo-crap scores every Rust function with the CRAP metric, which combines cyclomatic complexity (CC) with line coverage from an LCOV file. A score is high when code is both complex and poorly tested; at 100% coverage it equals CC.
Reference files
The vendored upstream docs in resources/auto/ are the source of truth. Search them before quoting a flag, key, default, or colour:
- Formula and its properties:
resources/auto/minikin-cargo-crap-main-docs-explanation-crap-metric.md. - Every flag and the
--missingpolicy:resources/auto/minikin-cargo-crap-main-docs-reference-cli.md. - Every
.cargo-crap.tomlkey with its range and default:resources/auto/minikin-cargo-crap-main-docs-reference-config.md. - Exit codes:
resources/auto/minikin-cargo-crap-main-docs-reference-exit-codes.md. - CI guides, all named
resources/auto/minikin-cargo-crap-main-docs-guides-<topic>.md:github-action(the upstream Action),ci(absolute gate and SARIF),regression-gate,pr-comment, andbadge. - Install options:
resources/auto/minikin-cargo-crap-main-docs-install.md.
Workflow
-
Install the tools if missing:
cargo binstall cargo-crap(orcargo install cargo-crap --locked) andcargo install cargo-llvm-cov.cargo testalone produces no coverage data;cargo llvm-covinstruments the build and writes LCOV. -
Generate coverage:
cargo llvm-cov --workspace --lcov --output-path lcov.info -
Score functions:
cargo crap --workspace --lcov lcov.infoDrop
--workspacefor a single crate; use-p <name>to select members. -
Read the table:
✗marks a score above--threshold(default 30),▲a score above a third of it,✓the rest. Lower a score by testing the uncovered lines (CRAP falls toward CC) or by splitting the function (CC falls). A function whose CC exceeds the threshold stays✗at full coverage, so only splitting fixes it. Setuncovered-hints = truein.cargo-crap.tomlto see which line ranges lack tests.
Configuration
.cargo-crap.toml sits at the project root or any parent; CLI flags override it, and unknown keys are rejected. Common keys:
threshold = 30.0
missing = "pessimistic" # pessimistic | optimistic | skip
exclude = ["src/generated/**"] # appends to the defaults: tests/**, benches/**, examples/**
uncovered-hints = true # config only
try-weight = 1.0 # config only; what each `?` adds to CC (0 to 100)
default-excludesreplaces the built-in exclusion list;excludeappends to it.try-weightbelow1.0stops idiomatic?error propagation from inflating CC. A baseline recorded under a different weight is not comparable.- Keep
missing = "pessimistic"for CI gates, so functions without coverage data score as 0% covered instead of disappearing.
Gates and reports
- Absolute gate:
cargo crap --workspace --lcov lcov.info --fail-aboveexits 1 when any function exceeds the threshold. - Regression gate, for codebases with existing debt: record
--format json --sort file --output baseline.jsonon the default branch, then run--baseline baseline.json --fail-regressionon pull requests. - On GitHub, the upstream Action
minikin/cargo-crap@v1runs coverage, scores the pull request’s base commit, comments, and gates in one step. Prefer it over hand-written comment scripts. - Other outputs come from
--format:sariffor code scanning,pr-commentfor a Markdown comment body,shieldsfor a badge endpoint JSON. Badge colour is brightgreen with no offenders, yellow for 1-5, and red for 6 or more.
Exit code 0 means no requested gate tripped, 1 means a gate tripped after the report was written, and 2 means the run failed (usage, input, analysis, or output error). Wrappers can branch on the code without parsing logs.
For CI workflow templates (SARIF upload, a reusable-workflow caller example), read resources/manual/ci-recipes.md.
Where to run it
Run cargo-crap in CI or on demand, not in pre-commit hooks: coverage instrumentation rebuilds the workspace with -C instrument-coverage, which invalidates the incremental build cache and runs the full test suite on every commit.
Completion criteria
cargo crapran against an LCOV file produced by the current code, and its exit code was checked.- Every function reported
✗(or regressed against the baseline) was either fixed, explicitly accepted by the user, or listed in the final report with its score, CC, and coverage. - Any flag, config key, or default you cite matches the vendored reference files.