minikin/cargo-crap

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

What it solves

cargo-crap is a tool for Rust developers to identify "risky" code. It targets functions that are both highly complex (hard to read and maintain) and poorly tested (low coverage), which are the most likely places for bugs to hide.

How it works

The tool calculates the CRAP (Change Risk Anti-Patterns) metric. This is a formula that combines cyclomatic complexity (CC) and test coverage into a single score.

  • Low Score: A simple, fully tested function scores a 1.0.
  • High Score: As complexity increases and coverage decreases, the score rises.
  • Hard Limit: If a function's complexity is too high (roughly above 30), it is flagged as risky regardless of how much test coverage it has.

It integrates with coverage tools like cargo-llvm-cov to ingest LCOV reports and can be used in CI/CD pipelines to fail builds if code risk exceeds a certain threshold.

Who it’s for

Rust developers and teams who want to maintain high code quality and ensure that complex logic is not left untested.

Highlights

  • CI Integration: Can gate builds using --fail-above or --fail-regression to prevent risk from increasing.
  • Baseline Comparison: Supports comparing current results against a previous JSON baseline to track improvements or regressions.
  • ** uma Duplicate Detection:** Can identify candidate duplicate functions based on structural similarity.
  • Multiple Output Formats: Supports human-readable tables, JSON, GitHub annotations, SARIF, and Shields.io badges.

Related

  • Project
  • Project
  • Project
  • Project
  • Project