Sub topic 6
Understanding Clippy lint groups
This chapter assumes you can run cargo clippy on a project and read a basic warning. It builds on that to explain how Clippy sorts its lints into groups, which groups are on by default, and how to choose which others to enable. Setting levels with attributes and the [lints] table gets its own chapter. Here you only need enough of it to turn groups on and off.
Why Clippy has groups
Clippy ships with several hundred lints. They differ a lot in quality. Some catch code that is almost certainly a bug. Others are matters of taste, and some have known false positives. If Clippy ran everything by default, the output would be noisy enough that people would stop reading it.
Each lint therefore belongs to exactly one group. The group sets two things: the default level (whether the lint runs and how loudly) and what you can expect from it (how likely it is to be right, and how much it matters).
The groups at a glance
| Group | Default level | What it contains | Example lint |
|---|---|---|---|
| `clippy::correctness` | deny | Code that is outright wrong or useless | `cmp_nan` |
| `clippy::suspicious` | warn | Code that is most likely wrong | `suspicious_else_formatting` |
| `clippy::style` | warn | Code that works but is unidiomatic | `needless_range_loop` |
| `clippy::complexity` | warn | Code that does something simple in a complicated way | `clone_on_copy` |
| `clippy::perf` | warn | Code that can be made faster with little effort | `vec_init_then_push` |
| `clippy::pedantic` | allow | Strict or opinionated lints, with some false positives | `cast_possible_truncation` |
| `clippy::restriction` | allow | Lints that forbid language features or patterns | `unwrap_used` |
| `clippy::nursery` | allow | New lints still under development | `use_self` |
| `clippy::cargo` | allow | Lints about `Cargo.toml` | `multiple_crate_versions` |
"Allow" means the lint is silent unless you turn it on. There is also a clippy::deprecated group, which holds lints that were removed or renamed. You do not need to enable it.
The first five rows run without any configuration. The last four are opt-in.
The default groups
The meaning of `clippy::all`
The name clippy::all is easy to misread. It does not mean every lint. It means the five default groups together: correctness, suspicious, style, complexity, and perf. Running plain cargo clippy is equivalent to enabling clippy::all at each group's default level.
Correctness
Correctness lints are denied by default, so they produce errors and stop the build. Clippy only puts a lint here when it is confident the code is wrong. This program compiles with plain rustc, but Clippy rejects it:
fn main() {
let reading: f64 = 0.5;
if reading == f64::NAN {
println!("no reading");
}
}A comparison with NaN is never true, because NaN is not equal to anything, including itself. The cmp_nan lint reports an error. The fix is to use the method meant for this check:
fn main() {
let reading: f64 = 0.5;
if reading.is_nan() {
println!("no reading");
}
}Suspicious, style, complexity, and perf
These four groups warn. Each answers a different question about the code.
- Suspicious asks whether the code probably does not do what the author meant. An
elsethat looks like it belongs to a differentifbecause of indentation is one case. A false positive is possible, but rare. - Style asks whether the code follows common Rust idiom. The
needless_range_loopexample from the previous chapter lives here. A style warning never means a bug. - Complexity asks whether there is a simpler way to write this. Calling
.clone()on aCopytype is an example. - Perf asks whether a cheaper form is available. It flags things like pushing to a
Vecimmediately after creating it, wherevec![...]would allocate once.
A useful habit is to treat these groups differently when reviewing. Suspicious warnings deserve a close look. Style warnings can usually be fixed mechanically. Perf warnings do not prove a slowdown, because Clippy cannot tell whether the code is on a hot path.
The opt-in groups
You enable a group on the command line with -W (warn), -D (deny), or -A (allow), placed after -- so that Cargo passes the flag on to Clippy:
cargo clippy -- -W clippy::pedanticFlags are applied left to right, and later flags override earlier ones. That makes it easy to enable a whole group and then switch off the lints you dislike:
cargo clippy -- -W clippy::pedantic -A clippy::module_name_repetitionsPedantic
Pedantic lints are correct about what they report, but what they report is often fine in context. A typical example is cast_possible_truncation:
fn main() {
let big: u64 = 300;
let small = big as u8;
println!("{small}");
}With -W clippy::pedantic, Clippy warns that the cast may truncate. It is right: the program prints 44, not 300 or 255. Whether that is a bug depends on your intent. If you want a checked conversion, say so:
fn main() {
let big: u64 = 300;
let small = u8::try_from(big).unwrap_or(u8::MAX);
println!("{small}");
}Pedantic is a good fit for new projects and libraries where you want a high bar. For an existing codebase it can produce hundreds of warnings. A practical approach is to enable the group, look at the count per lint, and allow the lints whose cost exceeds their value for your team.
Restriction
The restriction group is different in kind. Its lints do not look for mistakes. Each one bans a feature or pattern, such as unwrap_used, dbg_macro, print_stdout, or indexing_slicing. Many of them contradict each other or contradict idiomatic Rust, and the Clippy documentation warns against enabling the whole group.
Enable restriction lints one at a time, and only when you have a reason. A service that must never panic might enable unwrap_used and expect_used. A library might deny print_stdout so it never writes to the caller's terminal:
cargo clippy -- -W clippy::unwrap_used -W clippy::dbg_macroNursery
Nursery lints are new. They may produce false positives, and their behavior may change between releases. They are a reasonable choice if you want to help find those problems or want early feedback, but a poor choice for a build that must stay stable. Do not combine nursery with -D warnings in CI unless you pin your toolchain version.
Cargo
The cargo group checks Cargo.toml instead of Rust source. Examples are cargo_common_metadata, which reports missing fields like description and license, and multiple_crate_versions, which reports when your dependency tree contains two versions of the same crate. The metadata lints matter if you publish to crates.io. multiple_crate_versions is often noisy because the duplicates may come from dependencies you do not control, so many projects allow it.
Setting groups in `Cargo.toml`
Command-line flags are fine for experiments. For a project policy, put the choice in Cargo.toml so every developer and CI run uses the same settings:
# Cargo.toml
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"The priority = -1 entry matters. Cargo applies lint entries in priority order, and entries with equal priority are not guaranteed to follow the order you wrote them. Giving the group a lower priority than the single lints means the individual entries always override the group. Without it, module_name_repetitions = "allow" could be undone by the group setting. The [lints] table is covered in full in a later chapter.
Lints move between groups
Groups are not permanent. A lint can start in nursery, move to pedantic or style after its false positives are fixed, or be moved out of a default group if it proves too noisy. When you update your toolchain, a default group may gain new lints, and a build that used to pass can start producing warnings.
This has a practical consequence for CI. If you use -D warnings with the stable toolchain, a new Rust release can break your pipeline with no change to your code. Two common responses are to pin the toolchain in a rust-toolchain.toml file and update it deliberately, or to accept the occasional break and fix it when it appears. The second is simpler, and the first is more predictable.
Finding out what a lint does
Every Clippy diagnostic ends with a note naming the lint and a link to its documentation page. The note looks like this:
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
= note: `#[warn(clippy::needless_range_loop)]` on by defaultThe second line is useful for two reasons. It tells you the lint's name, which is what you need to allow or deny it. It also tells you its current level and why, so you can see at a glance whether the lint is on by default or because you enabled it. The documentation page for a lint lists its group, the version that added it, a short explanation, and an example of the code it flags and the code it prefers. You can also browse the full list filtered by group on the same site.
Choosing groups for your project
A workable default policy looks like this:
- Keep the five default groups on. Do not disable them as a whole.
- In CI, turn warnings into errors, so style and complexity issues cannot accumulate.
- For a new project, enable
pedanticat warn level and allow the specific lints your team disagrees with. - Enable individual
restrictionlints only where they enforce a real requirement. - Skip
nurseryunless you want to follow Clippy's development. - Enable
cargoonly for crates you publish, and allowmultiple_crate_versionsif you cannot act on it.
Review the policy when you upgrade your toolchain, because groups and their contents change.
Exercise
A teammate enables -W clippy::restriction and -D warnings in CI. The next build fails with dozens of errors about things like implicit_return and missing_docs_in_private_items. What is wrong with this setup, and what would you change?
Show answer
The restriction group is not meant to be enabled as a whole. Its lints ban features and patterns, and many conflict with idiomatic Rust, so they produce errors on ordinary code. Remove -W clippy::restriction and instead enable only the individual restriction lints the project actually needs, for example -W clippy::unwrap_used. Keep -D warnings so the lints you chose still fail the build.
Exercise
You enable pedantic in Cargo.toml with pedantic = { level = "warn" } and add module_name_repetitions = "allow". The warning for that lint still appears. What is the likely cause, and how do you fix it?
Show answer
Both entries have the same priority (0 by default), so Cargo does not guarantee that the individual allow is applied after the group's warn. Add priority = -1 to the group entry, as in pedantic = { level = "warn", priority = -1 }. The lower priority makes the group apply first, so the individual lint overrides it.