Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

cargo-crap

> AI 编程
Open source

Change Risk Anti-Patterns (CRAP) metric for Rust projects

375 stars0 likes0 views
WebsiteGitHub

About

Change Risk Anti-Patterns (CRAP) metric for Rust projects

cargo-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:

  • A trivial function (CC=1, 100% covered) scores exactly 1.0. That's the lower bound.
  • At 100% coverage the quadratic term collapses and CRAP equals CC. When you see matching values in those two columns, that function is fully covered — tests are capping the damage, but the complexity itself remains. It's a good sign, not a bug.
  • Above CC ≈ 30 no amount of coverage keeps you under the default threshold of 30. That's not a bug in the formula — it's the formula saying "this function is too big to certify as clean, regardless of tests."

Install

Via cargo binstall (downloads the right pre-built binary automatically):

bash
cargo binstall cargo-crap

From source (requires Rust stable ≥ 1.88):

bash
cargo install cargo-crap

From the AUR:

bash
paru -S cargo-crap

Pre-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.

Quick start

…

Example output:

…

Flags

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· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustagentsanti-patternscargo-plugincode-quality

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryAI 编程
PricingOpen source

> Related tools

G
GitHub Copilot
GitHub 官方 AI 编程助手,覆盖补全、Chat 与 Agent 模式。
C
Cursor
AI 原生代码编辑器,对话改代码、多文件 Agent 与规则体系是其核心。
S
skills
Skills for Real Engineers. Straight from my .agents directory.