Skip to content
rusthacks.dev
Tooling

Clippy Explained How Lints Are Grouped and Configured

Clippy is the Rust linter with hundreds of lints in groups: correctness, style, perf, pedantic and more. Learn what each group means, how to configure them and how to adopt it in CI.

2 min read
Summarize in AI
Flat illustration for the post “Clippy Explained How Lints Are Grouped and Configured”, composed around deploy.

The compiler tells you what is wrong. Clippy tells you what is suspicious: a comparison that is always true, a loop that should be an iterator, a clone on a Copy type. It ships with Rust, and its lints are grouped so you can choose how strict to be. This post explains the groups and the rollout that works in practice. It pairs with lints in Cargo.toml.

Rust 1.90, Clippy bundled.

A first run

rust
fn main() {
    let v = vec![1, 2, 3];
    if v.len() == 0 {
        println!("empty");
    }
    let doubled: Vec<i32> = v.iter().map(|x| x * 2).collect();
    println!("{}", doubled.len() > 0);
}
Shell
cargo clippy
Text
warning: length comparison to zero
 --> src/main.rs:3:8
  |
3 |     if v.len() == 0 {
  |        ^^^^^^^^^^^^ help: using `is_empty` is clearer and more idiomatic: `v.is_empty()`
  |
  = note: `#[warn(clippy::len_zero)]` on by default

The note names the lint and the group default. The second comparison produces the same warning. cargo clippy --fix applies both.

The groups

GroupDefaultMeaning
correctnessdenycode that is almost certainly wrong
suspiciouswarncode that is likely wrong
stylewarnidiomatic spelling
complexitywarnsimpler way to write it
perfwarnfaster way to write it
pedanticallowstricter, sometimes subjective
restrictionallowbans a language feature
nurseryallownew, may have false positives
cargoallowCargo.toml quality

The default groups are the baseline; pick up pedantic as a second step.

Configure

Cargo.tomlTOML
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"

The priority = -1 lets individual lints override the group. Thresholds live in clippy.toml, for example too-many-arguments-threshold = 6. Where to put the settings in a workspace is in lints in Cargo.toml.

Roll it out

  1. Run cargo clippy and fix what the default groups report; the changes are usually small and mechanical.
  2. Add cargo clippy --all-targets -- -D warnings to CI.
  3. Enable pedantic as warnings, and allow the lints that fight your style, with a one line reason.
  4. Add selected restriction lints such as unwrap_used for library code.

Pin the toolchain, as in rust-toolchain.toml. A new Clippy release adds lints, and an unpinned CI can fail on code that did not change.

Allowing a lint

rust
#[allow(clippy::too_many_arguments)] // mirrors the C API
fn open(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8, g: u8, h: u8) {}

Scope it narrowly and say why. A crate-wide #![allow(clippy::all)] silences everything, including the lints that find bugs.

Where Clippy is wrong

Lints have false positives, particularly in nursery and in macro-generated code. File an issue if you find one. Clippy cannot find logic errors, and it does not replace tests. The lint list with explanations is at rust-lang.github.io/rust-clippy.

Taggedclippy
Share

Keep reading

Flat illustration for the post “Incident Response for Rust Services a Short Runbook”, composed around deploy.
DevOps

Incident Response for Rust Services a Short Runbook

A one-page runbook for when a Rust service misbehaves: stabilize first, check recent changes, read the four golden signals, find the failing dependency, and use the Rust-specific evidence: panics, memory, executor stalls.

4 min read
Flat illustration for the post “Debugging Async Backtraces and Task Dumps in Tokio”, composed around async.
Tooling

Debugging Async Backtraces and Task Dumps in Tokio

A hung async service has no useful thread backtrace. Tokio task dumps capture the await stack of every task. Enable tokio_unstable and taskdump, trigger a dump from a signal or endpoint, and read where each task is parked.

4 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.