Rust Tooling and Code Quality
Clippy
Clippy (invoked via cargo clippy) is Rust's official static analysis linter, providing over 750 lints that catch subtle logic bugs, unnecessary heap allocations, non-idiomatic patterns, and performance bottlenecks that are valid Rust syntax and therefore accepted by rustc alone.
Source Code
|
v `cargo clippy --all-targets -- -D warnings`
[ rustc Type-Checker + Clippy Lint Passes ]
|-- `clippy::correctness` (Deny by default)
|-- `clippy::suspicious` (Warn by default)
|-- `clippy::style` (Warn by default)
|-- `clippy::complexity` (Warn by default)
|-- `clippy::perf` (Warn by default)
+-- `clippy::pedantic` / `restriction` (Allow by default; opt-in)
Clippy Lint Categories
Clippy organizes its lints into lint groups with distinct default severities:
| Lint Group | Default Level | What It Checks |
|---|---|---|
clippy::correctness |
deny |
Code that is outright wrong or buggy (e.g., x == f64::NAN, infinite recursion, out-of-range bit shifts). |
clippy::suspicious |
warn |
Code that is most likely a mistake (e.g., for loop over an Option, duplicate match arms). |
clippy::perf |
warn |
Avoidable runtime overhead (e.g., .or(expensive_call()) instead of `.or_else( |
clippy::complexity |
warn |
Overly convoluted expressions that can be simplified (e.g., .filter(..).next() instead of .find(..)). |
clippy::style |
warn |
Non-idiomatic naming, borrowing (&Vec<T> instead of &[T]), or redundant closures (` |
clippy::pedantic |
allow (opt-in) |
Strict style and documentation lints (missing_errors_doc, must_use_candidate, explicit casts). |
clippy::restriction |
allow (opt-in) |
Domain-specific bans (e.g., unwrap_used, indexing_slicing, dbg_macro for production services). |
clippy::cargo |
allow (opt-in) |
Cargo.toml metadata and duplicate dependency checks. |
Running Clippy and Applying Automatic Fixes
# 1. Lint library, binaries, tests, examples, and benchmarks, failing on any warning:
cargo clippy --all-targets --all-features -- -D warnings
# 2. Automatically apply machine-applicable Clippy suggestions in place:
cargo clippy --fix
Example: Idiomatic Refinements Caught by Clippy
// Non-idiomatic patterns that trigger `clippy::ptr_arg`, `clippy::or_fun_call`,
// and `clippy::manual_find` vs their idiomatic replacements:
pub fn find_first_even(numbers: &[i32], fallback: i32) -> i32 {
// Idiomatic: accepts `&[i32]` instead of `&Vec<i32>`, uses `.copied().find(...)`,
// and uses `.unwrap_or(fallback)` for cheap Copy values.
numbers
.iter()
.copied()
.find(|&n| n % 2 == 0)
.unwrap_or(fallback)
}
fn main() {
let values = vec![3, 7, 12, 19, 24];
println!("First even: {}", find_first_even(&values, 0));
}
First even: 12
Configuring Clippy in Cargo.toml and clippy.toml
You can configure lint severities in Cargo.toml under [lints.clippy] and tune numerical thresholds (such as the maximum cognitive complexity or Minimum Supported Rust Version) in a clippy.toml file at the repository root:
# File: clippy.toml
msrv = "1.85.0"
cognitive-complexity-threshold = 25
too-many-arguments-threshold = 7