为 Rust 项目设置变更风险反模式 (CRAP) 指标
[!TIP] For more context on the motivation behind this crate, read: cargo-crap: Finding Untested Complexity in AI-Generated Rust Code or watch Your AI Code Might Be CRAP! (Here's How To Fix It).
Compute the CRAP (Change Risk Anti-Patterns) metric for Rust projects.
CRAP combines cyclomatic complexity and test coverage into a single number
that is high when code is both hard to understand and poorly tested — i.e.
where bugs love to hide. The metric was introduced by Savoia & Evans in
2007 and was originally implemented for Java (Crap4j) and .NET (NDepend).
cargo-crap brings it to the Rust ecosystem.
CRAP(m) = comp(m)² × (1 − cov(m)/100)³ + comp(m)A few properties worth internalizing before you use the output:
Via cargo binstall (downloads the right pre-built binary automatically):
cargo binstall cargo-crapFrom source (requires Rust stable ≥ 1.88):
cargo install cargo-crapFrom the AUR:
paru -S cargo-crapPre-built binary (manual download):
…Windows: download cargo-crap-x86_64-pc-windows-msvc.zip from the latest release and extract cargo-crap.exe into a directory on your PATH.
…Example output:
…| Flag | Default | Purpose |
|---|---|---|
--lcov |
— | LCOV file from cargo llvm-cov or cargo tarpaulin. Omitting it is not a coverage-free run: every function is scored as if it had 0% coverage, so CRAP collapses to CC² + CC and the whole table reads red. That is useful for a first look at the complexity distribution — it is not a CRAP run. |
--path |
. |
Root to walk for .rs files (respects .gitignore). |
--threshold |
30 |
Score above which a function is flagged. |
--min |
— | Hide entries below this score. |
--top |
— | Show only the N worst offenders. |
--sort {crap,file} |
crap |
Final ordering of entries. crap sorts by score descending (best for reading top-down). file sorts by (file, function, line) ascending — stable across score changes, so a committed JSON baseline produces minimal diffs. --top always selects the N highest-CRAP functions first, then --sort reorders them. Applies to every format. |
--missing {pessimistic,optimistic,skip} |
pessimistic |
How to score a function with no coverage data. |
--exclude |
— | Skip files matching this pattern (repeatable). ** crosses directories. Appends to the default exclusions. |
--no-default-excludes |
off | Disable the built-in default exclusions (tests/**, benches/**, examples/**, matched relative to each analyzed root). By default these standard Cargo target directories are skipped — integration tests exist to cover production code, and benches/examples are not executed during a coverage run, so they only add 0%-coverage noise. |
--allow |
— | Suppress matching functions (repeatable). An entry containing / or ** is a path glob and matches the file the function is in (e.g. src/generated/**); otherwise it matches the function name and * crosses :: (e.g. Foo::*). Path globs analyze the file but hide its functions — distinct from --exclude, which skips files at walk time. |
--duplicates |
off | Also report candidate duplicate functions — functions whose normalized structure is close enough to be worth review. A second analysis over the same AST; off by default because it costs time on a large tree. Needs no --lcov. |
--dup-threshold |
0.82 |
Similarity at or above which a pair is reported, in 0.0..=1.0. Named apart from --threshold, which is the CRAP score threshold. |
--format {human,json,github,markdown,pr-comment,sarif,shields} |
human |
Output format. json emits a versioned envelope (see JSON output schema below). github emits ::warning annotations. markdown emits a GFM table (exhaustive). pr-comment is the opinionated PR-bot variant: hides unchanged rows, caps each section, collapses non-critical info into `` blocks. sarif emits SARIF 2.1.0 JSON for upload to GitHub Code Scanning, VS Code, and other static-analysis tooling (see SARIF output below). shields emits Shields.io endpoint-badge JSON for a README badge (see Shields.io badge below). |
--summary |
off | Print only aggregate stats (total, crappy count, worst offender) — no per-function table. In --workspace mode this becomes the per-crate summary plus the aggregat |
暂无开放 Issues,或尚未同步最近议题。