Sub topic 6
Understanding Clippy lint groups
This chapter assumes you can run cargo clippy and read its warnings. It also assumes you know what a lint is: a check that reports code that is likely wrong or unidiomatic. Clippy has several hundred lints, and it sorts them into groups. Each group has a default level and a purpose. Knowing the groups tells you which lints you already get and which you must turn on yourself.
The groups and their defaults
Every Clippy lint belongs to exactly one group. A group is named on the command line or in a config file as clippy::<group>.
| Group | Default level | What it covers |
|---|---|---|
| `correctness` | deny | Code that is almost certainly wrong |
| `suspicious` | warn | Code that is probably wrong or misleading |
| `style` | warn | Code that works but is not idiomatic |
| `complexity` | warn | Code that does something simply in a roundabout way |
| `perf` | warn | Code that can run faster with a small change |
| `pedantic` | allow | Stricter lints with some false positives |
| `restriction` | allow | Lints that ban specific language features or patterns |
| `nursery` | allow | New lints that are still under development |
| `cargo` | allow | Lints about `Cargo.toml` metadata |
The first five groups are on by default. Together they form the clippy::all group, so clippy::all does not include pedantic, restriction, nursery, or cargo.
A lint from a default group
This loop uses an index only to read from the vector. Clippy reports clippy::needless_range_loop, which is a style lint.
fn main() {
let v = vec![10, 20, 30];
for i in 0..v.len() {
println!("{}", v[i]);
}
}The warning appears with no configuration, because style is on by default. The idiomatic fix iterates over the vector directly.
fn main() {
let v = vec![10, 20, 30];
for item in &v {
println!("{item}");
}
}The opt-in groups
Each opt-in group needs a different level of care.
pedanticlints are usually correct but opinionated. Some flag code that is fine in context, such as casts between integer types. Many teams enable the group and then allow the few lints that do not suit them.restrictionlints do not claim that the code they flag is bad. Each one bans a single feature, such asunwrap_usedorprint_stdout. Do not enable the whole group. Doing so turns on lints that contradict each other. Pick individual lints that match your project's rules.nurserylints are not yet stable. They can produce false positives and can change or move between releases. Use them to experiment, not as a CI gate.cargolints inspectCargo.tomlinstead of Rust code. Examples arecargo_common_metadata, which wants fields such asdescriptionandlicense, andmultiple_crate_versions, which flags dependency trees with two versions of one crate. Libraries you publish benefit most from these.
Turning groups on
On the command line, -W sets a group to warn. Arguments after -- go to the lint driver, not to Cargo.
cargo clippy -- -W clippy::pedanticFor a lasting setting, use the [lints] table in Cargo.toml. A group and a single lint can conflict, so the group gets a lower priority. Entries with a higher priority are applied later and win.
# Cargo.toml
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"Here pedantic is set to warn at priority -1. The individual entries use the default priority of 0, so module_name_repetitions = "allow" overrides the group's warning for that one lint. Without the negative priority, Cargo gives the group and the single lints the same priority, and the result depends on ordering you should not rely on. This chapter only introduces the table. Lint levels and the [lints] table get their own chapters.
Common mistakes
- Enabling
clippy::restrictionas a whole. Choose individual lints instead. - Gating CI on
nursery. A new Clippy release can then break your build with no change in your code. - Assuming
clippy::allis everything. It is only the default set. - Silencing a noisy
pedanticlint by dropping the whole group. Allow the one lint.
Exercise
Your team wants clippy::pedantic as warnings and wants unwrap_used flagged too, but module_name_repetitions is too noisy. Which group does each of the two lints belong to, and how do you keep the single-lint settings from being overridden by the group?
Show answer
unwrap_used is a restriction lint, so it is off by default and must be enabled individually. module_name_repetitions is a pedantic lint. In the [lints.clippy] table, set pedantic = { level = "warn", priority = -1 } so the group has a lower priority than the single lints, then add unwrap_used = "warn" and module_name_repetitions = "allow". The single entries then take precedence over the group.