Sub topic 7
Controlling lint levels
This chapter assumes you can run cargo clippy and read its output, and that you know the lint groups from the previous chapter. Here you will learn how to decide what happens when a lint fires.
The four levels
Every Clippy lint has a level. The level decides what the compiler does when the lint matches.
| Level | Effect |
|---|---|
| `allow` | The lint is silent. |
| `warn` | A warning is printed and the build still succeeds. |
| `deny` | An error is reported and the build fails. |
| `forbid` | Like `deny`, but nothing later in the code can lower the level. |
Most Clippy lints are warn by default. The correctness group is deny by default. The pedantic and restriction groups are allow by default.
Setting levels in source code
An attribute changes the level for the item it is attached to. Prefer the narrowest scope that works.
#[allow(clippy::too_many_arguments)]
fn draw(x: i32, y: i32, w: i32, h: i32, r: u8, g: u8, b: u8, a: u8) {
let _ = (x, y, w, h, r, g, b, a);
}
fn main() {
draw(0, 0, 10, 10, 255, 255, 255, 255);
}The allow applies only to draw. Other functions with too many arguments are still reported.
To set a level for a whole crate, put an inner attribute (starting with #!) at the top of main.rs or lib.rs:
#![warn(clippy::unwrap_used)]
#![deny(clippy::dbg_macro)]
fn main() {
println!("lint levels set for the whole crate");
}An inner allow can undo an outer warn or deny. A forbid cannot be undone. Use forbid only when a rule must never be bypassed, because it also blocks legitimate local exceptions.
Setting levels on the command line
Flags after -- go to the compiler and apply to the whole build.
cargo clippy -- -W clippy::pedantic -A clippy::module_name_repetitions
cargo clippy --all-targets --all-features -- -D warnings-Wsets a lint or group towarn.-Asets it toallow.-Dsets it todeny.-Fsets it toforbid.
-D warnings is special. It turns every lint that is at warn level into an error. It is the usual way to make CI strict, and you will see it again in the CI chapter.
Setting a project-wide policy in Cargo.toml
Flags are easy to forget. The [lints] table in Cargo.toml stores the policy with the project, so every developer and every CI run uses it.
# Cargo.toml
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"Cargo passes these entries to the compiler as flags. Because a group and its members can overlap, order matters. Entries with a lower priority are applied first, and later entries override earlier ones. Giving the pedantic group priority = -1 makes it apply before the individual lints. That is why module_name_repetitions = "allow" can switch off one lint inside the pedantic group. Without the priority, the group could override your individual entry.
In a workspace, define the table once under [workspace.lints] in the root manifest. Each member crate then opts in with [lints] workspace = true.
Which setting wins
When several settings apply to the same lint, the most specific one wins. Attributes in source override Cargo.toml and command-line flags, and an attribute on an item overrides one on the crate. forbid is the exception, because it always wins.
An item-level #[allow(...)] still silences a lint when you run with -D warnings. That is useful for justified exceptions, but it means a reviewer should read each allow and ask why it is there.
Common mistakes
- Setting a group in
[lints]withoutpriority = -1, then wondering why individual entries have no effect. - Using
forbidfor a rule and then being unable to add a local exception. - Expecting
-D warningsto override an#[allow]attribute. It does not.
Exercise
Your Cargo.toml enables the whole pedantic group at warn level. You want to silence only clippy::module_name_repetitions for the whole project. Write the [lints.clippy] table, and explain why the priority value is needed.
Show answer
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
modulenamerepetitions = "allow"
The group has a lower priority (-1) than the individual entry (default 0), so it is applied first. The later allow entry then overrides the group's setting for that one lint. Without the priority, the group could override the individual entry.