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 flags. It covers the second layer of Clippy control: configuration values that change how a lint behaves, as opposed to whether it runs.
Levels versus configuration
Clippy has two separate controls, and people often confuse them.
- A lint level (
allow,warn,deny,forbid) decides whether a lint reports anything and how severe the report is. - A configuration value, set in
clippy.toml, changes the inputs a lint uses. Examples are a size threshold, a list of banned names, or the Rust version your crate supports.
Setting too-many-arguments-threshold = 8 does not turn clippy::too_many_arguments on or off. It changes the count at which that lint fires. The lint's level still comes from its group, an attribute, a flag, or the [lints] table.
Where Clippy looks for the file
Clippy reads a file named clippy.toml or .clippy.toml. If both exist, clippy.toml wins. It searches upward from the directory of the crate being checked and uses the first file it finds. It also honors the CLIPPY_CONF_DIR environment variable, which points at a directory containing the file.
Two consequences follow.
- In a workspace, a
clippy.tomlat the workspace root applies to every member crate that has no file of its own. - A
clippy.tomlin a member crate replaces the root file for that crate. The two files are not merged. If you want the root settings in that member, copy them.
Clippy reports which file it loaded when it runs with verbose output, and it warns about keys it does not recognize. Pay attention to that warning, because a misspelled key is otherwise silently ignored.
A first configuration
The file uses TOML. Keys use hyphens, not underscores.
# clippy.toml
msrv = "1.70"
too-many-arguments-threshold = 8This file does two things.
msrvdeclares the minimum supported Rust version. Clippy then avoids suggesting APIs or syntax added after Rust 1.70.too-many-arguments-thresholdraises the argument count at whichclippy::too_many_argumentsfires. The default is 7, so a function with 8 arguments no longer triggers it.
Minimum supported Rust version
The msrv setting prevents a common frustration. Suppose your crate promises to build on Rust 1.70. Clippy, running on a newer toolchain, might suggest replacing a manual if let ... else with Option::is_some_and, which was stabilized in 1.70, or with an API from a later release. If you applied a suggestion that needs Rust 1.80, users on 1.70 would get a compile error.
With msrv set, lints that depend on newer features check the declared version and stay quiet when the feature is too new.
There are three places to declare it:
msrvinclippy.toml.- The
rust-versionfield inCargo.toml. Recent Clippy versions read this automatically whenmsrvis absent. - A
#![clippy::msrv = "1.70"]attribute at the crate root.
# Cargo.toml
[package]
name = "demo"
version = "0.1.0"
edition = "2021"
rust-version = "1.70"Using rust-version in Cargo.toml has an extra benefit: Cargo itself refuses to build the crate on an older compiler and reports a clear message. Prefer it when your Clippy is recent enough, and keep msrv in clippy.toml only when you need Clippy's value to differ from Cargo's.
Common thresholds
Many lints that measure size or complexity expose a threshold. A few that you are likely to touch:
| Key | Lint | Default | Meaning |
|---|---|---|---|
| `too-many-arguments-threshold` | `too_many_arguments` | 7 | Maximum function parameters |
| `too-many-lines-threshold` | `too_many_lines` | 100 | Maximum lines in a function |
| `cognitive-complexity-threshold` | `cognitive_complexity` | 25 | Maximum complexity score |
| `type-complexity-threshold` | `type_complexity` | 250 | Maximum type complexity score |
| `enum-variant-size-threshold` | `large_enum_variant` | 200 | Byte gap between variants |
Defaults can change between releases, so confirm them in the Clippy lint documentation for your version. Note also that too_many_lines and cognitive_complexity belong to the pedantic and nursery groups respectively. They report nothing until you enable their groups or the individual lints.
A threshold is a judgment call about your team's tolerance. Raising it to silence one function is usually the wrong move. If a single function is the exception, an attribute on that item is more honest, as the next section shows.
Configuration versus a local attribute
Both approaches quiet a lint, but they have different reach.
#[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);
}The attribute affects one function, and it is visible at the place a reader would wonder about the length of the parameter list. Changing too-many-arguments-threshold affects the whole project and leaves no trace at the call site.
Use this rule of thumb:
- If your team agrees a different number is the right limit everywhere, change the threshold.
- If one item is a justified exception, use
#[allow(...)]on that item, ideally with a comment explaining why.
Rust 1.81 and later also supports #[expect(clippy::too_many_arguments)]. It behaves like allow but warns if the lint stops firing, so stale suppressions get found and removed.
Disallowing specific items
Some options let you build a project-specific policy without writing a custom lint. The disallowed-* keys make Clippy flag items you choose.
# clippy.toml
disallowed-methods = [
{ path = "std::env::set_var", reason = "not thread safe; configure through a config struct" },
"std::process::exit",
]
disallowed-types = [
{ path = "std::collections::HashMap", reason = "use indexmap::IndexMap for stable order" },
]
disallowed-names = ["foo", "baz", "quux"]Each entry is either a plain string path or a table with path and an optional reason. The reason appears in the diagnostic, which helps the next developer who hits it.
These keys are read by lints that are enabled by default (clippy::disallowed_methods, clippy::disallowed_types, and clippy::disallowed_names), so no extra level change is needed. Note that disallowed_names replaces the default list of banned names. It does not extend it. Include foo, baz, and quux yourself if you still want them.
Paths must be fully qualified and must resolve. If the path is wrong, Clippy emits a warning about an unresolved path, so check the output after editing.
Extending defaults with the `".."` marker
Several list-valued options replace their default list when you set them. To keep the defaults and add your own entries, put ".." in the list.
# clippy.toml
doc-valid-idents = ["MyCompany", "GraphQL", ".."]Here doc-valid-idents tells the clippy::doc_markdown lint which capitalized words do not need backticks in doc comments. Without "..", you would lose built-in entries such as GitHub and OpenGL, and the lint would start flagging them.
Not every list option supports "..". The Clippy configuration reference marks which ones do. When the documentation shows a default list, assume that setting your own value overwrites it unless ".." is supported.
Checking that your configuration works
Configuration errors are quiet, so verify them. A short loop works well.
- Write the setting.
- Create or find code that crosses the threshold.
- Run Clippy and confirm the lint appears or disappears as expected.
Here is a function with eight parameters that you can use to test the argument threshold.
fn blend(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8, g: u8, h: u8) -> u8 {
a / 8 + b / 8 + c / 8 + d / 8 + e / 8 + f / 8 + g / 8 + h / 8
}
fn main() {
println!("{}", blend(8, 16, 24, 32, 40, 48, 56, 64));
}With no clippy.toml, cargo clippy reports clippy::too_many_arguments on blend. Add too-many-arguments-threshold = 8 and run it again. The warning disappears. Lower the value to 7 and it returns.
Common errors
- Underscores in keys.
too_many_arguments_thresholdis not recognized. Use hyphens. Clippy prints an unknown-field error that lists the valid keys. - Expecting a config to enable a lint. A threshold never changes a lint level. If nothing is reported, check the level first.
- Stale cache. Clippy reuses results from earlier runs. After editing
clippy.toml, Cargo may not re-check unchanged code. Cargo tracksclippy.tomlas an input in recent versions, but if a change does not seem to take effect, runtouch src/lib.rsorcargo clean -p your-crateand rerun. - Unmerged workspace files. A member-level
clippy.tomlhides the root one entirely. - Wrong location. A file in
src/is not found. Put it next toCargo.tomlor higher. - Value types. Thresholds are integers, not strings.
too-many-arguments-threshold = "8"is an error.
How it fits with the rest of the policy
A realistic project splits the policy across files, each with one job.
Keep levels in Cargo.toml, tuning values in clippy.toml, and one-off exceptions next to the code they affect. This separation makes review simpler, because a change to a threshold is visibly different from a change to what is enforced.
Summary of decisions
- Declare your supported Rust version once, preferably as
rust-versioninCargo.toml. - Change a threshold only when the team agrees the default is wrong for the whole project.
- Use
disallowed-*keys for rules such as banned APIs, and give each areason. - Use
".."to extend a default list where supported. - Test every change against code that should trigger the lint.
Exercise
Your crate must build on Rust 1.65. Clippy on your newer toolchain suggests using Option::is_some_and, which needs Rust 1.70. You also want clippy::too_many_arguments to allow functions with 9 parameters. Write the clippy.toml that fixes both problems, and explain why setting the threshold does not need a change in lint level.
Show answer
The file is msrv = "1.65" and too-many-arguments-threshold = 9, each on its own line, with hyphens in the key. With msrv set, lints that rely on newer APIs stop suggesting is_some_and. The threshold only changes the number at which too_many_arguments fires. The lint is in the default complexity group at warn, so it already runs, and the threshold just moves its trigger point.
Exercise
A teammate adds disallowed-names = ["temp"] to clippy.toml. Afterward, Clippy stops warning about variables named foo. Why did that happen, and how do you fix it?
Show answer
disallowed-names replaces the default list instead of extending it, so foo, baz, and quux are no longer banned. Fix it by adding the ".." marker, as in disallowed-names = ["temp", ".."], which keeps the defaults and adds temp. Then run Clippy on a file that uses a variable named foo to confirm the warning returns.