Sub topic 10
Project-wide lint policy in Cargo.toml
Earlier chapters set lint levels with attributes in source files and with command-line flags. Both work, but neither describes a policy that every contributor, editor, and CI job sees in the same place. This chapter covers the [lints] table in Cargo.toml, which does that.
This chapter assumes you know:
- Clippy lint groups such as
pedanticandcorrectness. - The four lint levels:
allow,warn,deny, andforbid. - How to run
cargo clippy.
The [lints] table needs Cargo 1.74 or newer.
Why a manifest table
Lint attributes like #![warn(clippy::pedantic)] sit at the top of lib.rs or main.rs. In a project with several crates you have to repeat them in each root file, and it is easy to forget one. Flags passed through RUSTFLAGS or after -- apply only to the commands that carry them. A teammate running plain cargo clippy in an editor does not get them.
The [lints] table is part of the manifest. Cargo reads it for every build, check, and Clippy run, and it passes the settings to the compiler as lint flags. Tools that run Cargo, such as rust-analyzer and CI, therefore pick up the same policy without extra setup.
The basic shape
The table has one sub-table per tool namespace:
[lints.rust]for lints built into the compiler, such asunsafe_codeorunused_must_use.[lints.clippy]for Clippy lints, written without theclippy::prefix.[lints.rustdoc]for rustdoc lints, such asbroken_intra_doc_links.
Each key is a lint name or a lint group name. The value is either a level string or a table with a level and a priority.
# Cargo.toml
[package]
name = "inventory"
version = "0.1.0"
edition = "2021"
[lints.rust]
unsafe_code = "forbid"
unused_must_use = "deny"
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"
[lints.rustdoc]
broken_intra_doc_links = "deny"The four accepted levels are allow, warn, deny, and forbid, with the meanings you saw in the lint levels chapter. forbid is deny that source code cannot loosen.
You can also write a section as a dotted key under [lints], such as clippy.unwrap_used = "warn". The section form above is easier to read once the list grows.
Running cargo build with unsafe_code = "forbid" fails if any unsafe block appears in the crate. You do not need Clippy for [lints.rust] entries, because the compiler enforces them.
Clippy lints in [lints.clippy] are harmless during a normal cargo build. The compiler recognizes the clippy tool namespace and ignores those entries. They take effect only under cargo clippy.
Priority: making groups and individual lints coexist
A group such as pedantic contains many lints. If you set the group to warn and one member to allow, the result must be the individual setting. Cargo needs a rule for which entry wins, and that rule is priority.
- Priority is an integer, and the default is
0. - Cargo emits lower priorities first, so entries with higher priority are applied later and override earlier ones.
- For two settings that touch the same lint, the one applied later wins.
Set a group at a negative priority so that every individual entry at the default 0 overrides it:
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
module_name_repetitions = "allow"Now Cargo applies pedantic = warn first, then module_name_repetitions = allow. The lint module_name_repetitions belongs to the pedantic group, so without a priority difference both entries would sit at priority 0. You should not depend on the order of equal-priority entries. Cargo may reorder them, and the group could end up overriding your exception. A priority gap makes the result deterministic.
Cargo's own linter, which checks the manifest, flags this mistake with the lint_groups_priority lint, so you will see a warning if you set a group and a member at the same priority.
The diagram below shows how the final level of one lint is decided.
Source attributes still apply
The manifest sets the default level for the whole crate. A #[allow(...)] attribute on a function or module still overrides a warn or deny from Cargo.toml for that item:
// Cargo.toml has: unwrap_used = "warn"
fn parse_port(text: &str) -> u16 {
#[allow(clippy::unwrap_used)]
// Caller guarantees the string came from a validated constant.
text.parse().unwrap()
}
fn main() {
println!("{}", parse_port("8080"));
}This is the intended escape hatch. Keep it narrow and give a reason in a comment, so reviewers can see why the exception exists.
forbid is different. If Cargo.toml says unsafe_code = "forbid", an #[allow(unsafe_code)] in source is itself an error. Use forbid for rules with no exceptions, and deny for rules where you want to permit reviewed exceptions.
Command-line flags interact with the table in a similar way. cargo clippy -- -D warnings turns every warning into an error, including those from warn entries in the table. It does not turn an allow into a warning, so lints you allowed in the manifest stay silent.
Sharing a policy across a workspace
A workspace holds several crates under one root. Repeating the same [lints] block in each member invites drift. Instead, define the policy once in the root Cargo.toml under [workspace.lints] and have each member opt in.
# Cargo.toml (workspace root)
[workspace]
members = ["core", "cli"]
resolver = "2"
[workspace.lints.rust]
unsafe_code = "forbid"
[workspace.lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"# core/Cargo.toml
[package]
name = "core"
version = "0.1.0"
edition = "2021"
[lints]
workspace = trueThe line workspace = true under [lints] tells Cargo to take the table from the workspace root. Members that omit it get no lints from the workspace at all. Cargo does not apply workspace lints automatically, so check each new crate for the opt-in line.
There is one restriction. A member that inherits with workspace = true cannot also add its own lint entries. Cargo treats the table as either inherited or local, never a merge of the two. If one crate needs an extra exception, you have three choices:
- Add the exception in that crate's source with an attribute.
- Drop
workspace = truefor that crate and write its full table by hand. - Loosen the rule in the workspace root if every crate would benefit.
The first option is usually best, because it keeps one shared policy and puts the exception where a reader will see it.
A practical policy
Teams adopting this table often begin with a small set and grow it. A reasonable start:
[lints.rust]
unsafe_code = "forbid"
missing_debug_implementations = "warn"
[lints.clippy]
all = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
expect_used = "warn"
dbg_macro = "deny"
todo = "warn"Notes on these choices:
allis already warn by default, but writing it makes the intent visible and lets you change it later.dbg_macro = "deny"stops leftoverdbg!calls from reaching the main branch.unwrap_usedandexpect_usedare in therestrictiongroup, which is never enabled by a group-level setting. You have to name each lint you want.
Do not enable the whole restriction group. Its lints contradict each other, and many ban normal code, such as print_stdout and indexing_slicing. Pick individual lints from it.
Enabling pedantic on an existing codebase can produce hundreds of warnings. Add it at warn, fix or allow lints in batches, and only then raise the level to deny. A policy that nobody can satisfy gets ignored.
Configuring cfg checks
One common [lints.rust] entry deals with conditional compilation. The lint unexpected_cfgs warns when code tests a cfg name the build never defines. If a build script or custom tooling sets such a name, declare it so the lint stays quiet:
[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ['cfg(fuzzing)', 'cfg(loom)']This is a table form of the entry. The check-cfg array lists the extra cfg expressions that are known to be valid.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Cargo reports an unknown key under `[lints]` | Cargo older than 1.74 | Update the toolchain, or use attributes instead |
| A lint you set to `allow` still fires | Group at the same priority overrides it | Give the group `priority = -1` |
| Workspace lints have no effect in a member | Missing `[lints] workspace = true` | Add the opt-in to the member manifest |
| Error about both `workspace = true` and other lint keys | A member tried to merge inherited and local lints | Remove the local keys and use a source attribute |
| `#[allow]` rejected with a `forbid` message | The manifest sets the lint to `forbid` | Change the manifest level to `deny`, or remove the exception |
| `clippy::` lint name rejected | The prefix was written in the manifest | Write `unwrap_used`, not `clippy::unwrap_used`, under `[lints.clippy]` |
What stays in clippy.toml
The [lints] table decides which lints run and at what level. It does not hold lint parameters. Thresholds such as too-many-arguments-threshold still live in clippy.toml. Clippy also reads rust-version from [package] in Cargo.toml as the minimum supported Rust version when clippy.toml does not set msrv, so you may not need to state it twice.
Exercise
A crate's Cargo.toml has pedantic = "warn" and module_name_repetitions = "allow" both under [lints.clippy] with no priorities. The allow sometimes does not take effect. What is the cause, and what is the corrected configuration?
Show answer
Both entries have the default priority 0, so Cargo does not guarantee which one is applied last, and the group can override the individual lint. Give the group a lower priority: pedantic = { level = "warn", priority = -1 }, and leave module_name_repetitions = "allow" at priority 0. The individual entry is then applied after the group and wins.
Exercise
A workspace has crates core and cli. You defined [workspace.lints.clippy] in the root, but running cargo clippy shows no unwrap_used warnings in cli. core works. What do you check first?
Show answer
Check cli/Cargo.toml for a [lints] section containing workspace = true. Cargo applies workspace lints only to members that opt in, and core likely has the line while cli does not.
Exercise
Your manifest sets unsafe_code = "forbid". A colleague adds #[allow(unsafe_code)] on one function to call a C library. What happens, and what are two ways to resolve it?
Show answer
The build fails, because forbid cannot be lowered by a source attribute. Either change the manifest level to deny so reviewed exceptions with #[allow(unsafe_code)] are possible, or move the unsafe code into a separate crate that does not forbid it, keeping the main crate fully safe.