Sub topic 9
Tuning Clippy with clippy.toml
This chapter assumes you can run cargo clippy, read its diagnostics, and set lint levels with attributes or the [lints] table. Lint levels decide whether a lint fires. clippy.toml decides how a lint behaves once it is enabled.
What clippy.toml controls
Some Clippy lints depend on a number or a version. For example, too_many_arguments fires when a function has more than a set number of parameters. Another lint suggests newer standard library APIs. clippy.toml holds these values so they can be set once per project.
The file is separate from Cargo.toml and from rustfmt.toml. Clippy looks for clippy.toml or .clippy.toml in the project root. Settings use TOML keys with dashes, such as too-many-arguments-threshold.
A first configuration
Create clippy.toml next to Cargo.toml:
# clippy.toml
msrv = "1.70"
too-many-arguments-threshold = 8The two settings do different jobs.
msrvis the minimum supported Rust version. Clippy will not suggest APIs or syntax that were stabilized after this version.too-many-arguments-thresholdraises the limit used byclippy::too_many_arguments. The default is 7, so a function with 8 parameters is accepted once you set the value to 8.
Now consider this function from the earlier chapter on lint levels:
#[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);
}It has eight parameters. With the threshold at 8, the #[allow] attribute is no longer needed, so you can remove it. Raising the threshold suits a project where long signatures are normal. An #[allow] suits a single exception.
| Need | Tool |
|---|---|
| One function is a justified exception | `#[allow(clippy::lint_name)]` on that item |
| The whole project tolerates a larger limit | A threshold in `clippy.toml` |
| A lint is wrong for the whole project | `lint_name = "allow"` in `[lints.clippy]` |
Setting the minimum supported Rust version
Without an msrv, Clippy assumes you can use any feature of the current toolchain. A library that promises to build on an older compiler can then receive suggestions that break that promise.
Suppose a lint suggests an API added in Rust 1.80, but your msrv is "1.70". Clippy stays silent on that suggestion. The setting costs nothing and prevents a class of broken pull requests.
Cargo can also supply the version. If Cargo.toml has a rust-version field, recent Clippy releases read it and use it as the msrv. Keeping one source of truth avoids two numbers that drift apart. If you set both, make them match.
How Clippy finds the file
Clippy searches for the configuration starting in the directory given by the CLIPPY_CONF_DIR environment variable. If that is unset, it uses the CARGO_MANIFEST_DIR of the crate being checked. It then searches that directory and its parents. In a workspace, a single clippy.toml at the workspace root therefore applies to every member, unless a member has its own file.
Common errors
- Unknown key. Clippy reports an error for a misspelled setting, which is useful. Use the exact names from the Clippy configuration reference, with dashes rather than underscores.
- Changes seem ignored. Clippy caches results. After editing
clippy.toml, runcargo clean -p your_crateor touch a source file so the crate is checked again. - Expecting a lint to appear. Raising or lowering a threshold does nothing if the lint is allowed. Check the lint level first.
- Different results in CI and locally. The two environments may use different toolchain versions or different config files. Commit
clippy.tomland pin the toolchain.
Exercise
Your team's functions often take 9 parameters, and clippy::too_many_arguments warns on all of them. You want to keep the lint but stop it from firing at 9 parameters. Which file and setting do you change, and what value?
Show answer
Add too-many-arguments-threshold = 9 to clippy.toml in the project root. The lint still runs, but it only fires for functions with more than 9 parameters.
Exercise
Your library promises to build on Rust 1.70. Which clippy.toml setting stops Clippy from suggesting newer APIs, and where else can Clippy read that version from?
Show answer
Set msrv = "1.70". Clippy can also read the rust-version field in Cargo.toml.