Sub topic 7
Controlling lint levels
Clippy reports many lints, and a project rarely wants all of them at full strength. A lint level tells the compiler what to do when a lint fires: stay silent, print a warning, or fail the build. This chapter covers the four levels, the three places you can set them, and how those settings interact.
This chapter assumes you can run cargo clippy and read its output. It also assumes you know what a Clippy lint group is, such as style, pedantic, or restriction. You should be comfortable editing Cargo.toml and adding attributes to Rust source files.
The four lint levels
Every lint has a level at any point in your code. The level decides what happens when the lint's pattern is found.
| Level | Effect when the lint fires | Can an inner scope change it? |
|---|---|---|
| `allow` | Nothing is reported. | Yes |
| `warn` | A warning is printed. The build still succeeds. | Yes |
| `deny` | An error is printed. The build fails. | Yes |
| `forbid` | Same as `deny`. | No. Changing it is a compile error. |
A fifth form, expect, is covered later. It behaves like allow but checks that the lint would have fired.
Clippy sets the starting levels by group. The correctness group starts at deny. The style, complexity, perf, and suspicious groups start at warn. The pedantic, nursery, restriction, and cargo groups start at allow, so they stay silent until you opt in.
Plain rustc lints, such as unused_variables and unsafe_code, use the same four levels. The same attributes, flags, and Cargo.toml entries control them. Clippy lints are written with a clippy:: prefix, for example clippy::unwrap_used.
Three places to set levels
You can set a level in source code, on the command line, or in Cargo.toml. Each has a different scope and purpose.
| Where | Scope | Typical use |
|---|---|---|
| Attributes in source | One item, one module, or one crate | Local exceptions with a reason |
| Command-line flags | The whole invocation | CI strictness, one-off experiments |
| `[lints]` in `Cargo.toml` | The package or workspace | Shared project policy |
Attributes in source code
An attribute sets the level for the item it is attached to, including everything inside that item. The outer form #[allow(...)] applies to the next item. The inner form #![allow(...)], with an exclamation mark, applies to the enclosing scope. At the top of main.rs or lib.rs, the inner form covers the whole crate.
#![warn(clippy::pedantic)]
#![deny(clippy::unwrap_used)]
fn parse(input: &str) -> i32 {
input.parse().unwrap()
}
fn main() {
println!("{}", parse("42"));
}Running cargo clippy on this file reports an error for the unwrap() call and the build fails. Replace deny with warn and the same call produces a warning instead.
Prefer the narrowest scope that works. Silencing a lint on one function leaves the rest of the crate protected. This example from the spine shows an attribute on a single function:
#[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);
}A common real-world pattern is to deny a strict lint in production code and allow it in tests. An inner attribute inside the test module does this:
#![deny(clippy::unwrap_used)]
pub fn double(input: &str) -> Option<i32> {
let n: i32 = input.parse().ok()?;
Some(n * 2)
}
fn main() {
println!("{:?}", double("21"));
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn doubles_a_number() {
assert_eq!(double("21").unwrap(), 42);
}
}Command-line flags
Arguments after -- in cargo clippy go to the compiler driver. Four flags set levels:
-Aallows a lint or group.-Wwarns on it.-Ddenies it.-Fforbids it.
cargo clippy -- -W clippy::pedantic -A clippy::module_name_repetitionsThis enables the pedantic group at warn level and then turns one noisy pedantic lint off. Order matters. When two flags name the same lint, the later one wins, so put the broad group first and the exceptions after it.
The special name warnings means every lint that is currently at warn. This is the standard CI pattern:
cargo clippy --all-targets --all-features -- -D warningsThe flag only affects lints that are at warn level. It does not enable lints that are at allow. Flags also apply to the crates in your workspace but not to your dependencies. Cargo builds dependencies with lints capped at allow, so a warning in a third-party crate never fails your build.
The `[lints]` table in `Cargo.toml`
Since Rust 1.74, Cargo can store lint levels in the manifest. The [lints.rust] table holds compiler lints and [lints.clippy] holds Clippy lints. The next chapter section of the plan covers this table as a project-wide policy. This chapter covers how it relates to the other two mechanisms.
# Cargo.toml
[lints.rust]
unsafe_code = "forbid"
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"Each entry is either a level string or a table with level and priority. Cargo turns these entries into compiler flags. That makes them equivalent to command-line flags, and source attributes override them.
Which setting wins
Several settings can apply to the same lint at once. These rules decide the outcome:
- A
forbidat any outer level cannot be lowered. An attempt to do so is a compile error. - Otherwise, the attribute closest to the code wins. An item attribute beats a module attribute, and a module attribute beats a crate attribute.
- Source attributes beat command-line flags and
[lints]entries, which only supply the starting level. - Among command-line flags, the later flag wins. Among
[lints]entries, a higherprioritynumber is applied later and wins.
Rule 3 has a consequence people often miss. Running cargo clippy -- -D warnings does not override an #[allow(clippy::something)] in your source. The explicit local allow still silences that lint. This is usually what you want, because the allow records a deliberate decision.
Forbid in practice
forbid is for rules that must never be bypassed in a codebase. This code fails to compile:
#![forbid(unsafe_code)]
#[allow(unsafe_code)]
fn main() {
println!("hello");
}The compiler rejects the allow because it conflicts with the earlier forbid. With deny, the same allow would be accepted. Use forbid sparingly. It also stops legitimate exceptions, and generated code or macros that use allow can break.
Priority in the `[lints]` table
Cargo applies entries in order of priority, lowest first. The default priority is 0. Entries with the same priority have no guaranteed relative order.
This matters when a group and a single lint in that group are both set. Suppose you write the following:
[lints.clippy]
pedantic = "warn"
module_name_repetitions = "allow"Both entries have priority 0, so the compiler might apply pedantic after the individual allow, and the pedantic group would turn the lint back on. Giving the group a lower priority fixes this:
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
module_name_repetitions = "allow"Now the group is applied first and the single-lint entry is applied second, so the exception holds. Always give group entries a negative priority when you override members of the group.
Sharing lints across a workspace
A workspace can define one policy and let each member inherit it. The root manifest uses [workspace.lints]. Each member opts in with workspace = true.
# Cargo.toml at the workspace root
[workspace]
members = ["app"]
resolver = "2"
[workspace.lints.rust]
unsafe_code = "forbid"
[workspace.lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"# app/Cargo.toml
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[lints]
workspace = trueA member that sets workspace = true cannot add its own entries under [lints]. If one crate needs an exception, use an attribute in that crate's source.
Marking exceptions with `expect`
An allow stays in the code after the reason for it disappears. If you later fix the code, the allow silently remains. The expect attribute, stable since Rust 1.81, solves this. It silences the lint like allow, but it produces a warning if the lint did not fire.
#[expect(clippy::too_many_arguments, reason = "mirrors the C API")]
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, 0, 0, 255);
}If you later refactor draw to take a struct, the lint no longer fires. The compiler then reports an unfulfilled lint expectation, which tells you to delete the attribute. The optional reason string also appears in diagnostics and helps reviewers. The reason field works with allow, warn, deny, and forbid too.
Common errors
- Setting a group without a priority. The group overrides your single-lint exceptions. Use
priority = -1on groups. - Expecting
-D warningsto enable new lints. It only promotes lints already atwarn. To enable pedantic lints, pass-W clippy::pedanticas well. - Putting
#![...]in the wrong place. Inner attributes must come before any items in the file. In a module, they go at the top of the module body. - Mistyping a lint name. A misspelled name produces an
unknown_lintswarning, and the level you meant to set is not applied. Check the spelling against the lint list when a setting seems ignored. - Treating
denyas enough for a hard rule. Any innerallowcan undodeny. Useforbidwhen the rule truly cannot be bypassed. - Letting
-D warningsbreak on toolchain updates. A new Clippy release can add lints that turn a green build red. Pin the toolchain in CI, or deny specific lints instead of all warnings.
Choosing a strategy
A workable setup for most projects uses all three mechanisms for different jobs:
- Put the shared policy in
[lints], or in[workspace.lints]for a workspace. Enable the groups you want atwarnand list the exceptions. - Use
#[expect(..., reason = "...")]for local exceptions, so stale ones are detected. - Use
-D warningson the CI command line, so warnings never merge but local development stays flexible. - Use
forbidfor the few rules that must hold everywhere, such asunsafe_codein a crate that has no need for it.
Exercise
A crate's Cargo.toml contains [lints.clippy] with pedantic = "warn" and module_name_repetitions = "allow", both at the default priority. You still see module_name_repetitions warnings. Why, and how do you fix it?
Show answer
Both entries have priority 0, so their relative order is not guaranteed, and the pedantic group can be applied after the single-lint allow, turning the lint back on. Fix it by giving the group a lower priority: pedantic = { level = "warn", priority = -1 }. The group is then applied first and the allow is applied after it.
Exercise
Your source file contains #![forbid(clippy::unwrap_used)] at the top, and a function further down has #[allow(clippy::unwrap_used)]. What happens when you run cargo clippy, and what would change if the top line used deny instead?
Show answer
With forbid, the inner allow is a compile error because a forbidden lint level cannot be lowered. With deny, the inner allow is accepted and silences the lint for that function only.
Exercise
A function has #[allow(clippy::needless_range_loop)]. CI runs cargo clippy -- -D warnings. Does the lint fail the build? Why?
Show answer
No. Source attributes override command-line levels, so the explicit allow on the function still silences the lint. The -D warnings flag only affects lints that would otherwise be at warn.