Sub topic 10
Project-wide lint policy in Cargo.toml
This chapter assumes you can build a Cargo project, run cargo clippy, and know the four lint levels (allow, warn, deny, forbid). It also assumes Cargo 1.74 or newer, the first version with the [lints] table.
Why move lint settings into Cargo.toml
Attributes like #![warn(clippy::pedantic)] at the top of main.rs or lib.rs work, but they are scattered. Every crate in a workspace needs its own copy, and a reviewer cannot see the whole policy in one place. Command-line flags such as -D warnings are easy to forget locally.
The [lints] table in Cargo.toml states the policy once. Cargo passes these settings to the compiler and to Clippy on every build, whether you run it in a terminal, an editor, or CI.
The basic table
Each tool gets its own sub-table. Use [lints.rust] for compiler lints and [lints.clippy] for Clippy lints. Do not write the clippy:: prefix in the key.
# Cargo.toml
[package]
name = "demo"
version = "0.1.0"
edition = "2021"
[lints.rust]
unsafe_code = "forbid"
missing_docs = "warn"
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"
module_name_repetitions = "allow"The values are the level names: "allow", "warn", "deny", and "forbid". Use the inline-table form { level = "...", priority = N } when you need a priority.
Why priority matters
pedantic is a group of many lints. If you also name one lint from that group, such as module_name_repetitions, Cargo must decide which setting wins. Cargo sorts the entries by priority and applies lower numbers first. Entries with the same priority are applied in alphabetical order. The last setting applied wins.
Plain entries default to priority 0. Giving the group priority = -1 makes it apply first, so your individual entries override it. Without that, the group could be applied after your entry and silently undo it. The module_name_repetitions = "allow" line would then have no effect.
flowchart TD
A["pedantic = warn (priority -1)"] --> B["unwrap_used = warn (priority 0)"]
B --> C["module_name_repetitions = allow (priority 0)"]
C --> D["Final levels passed to Clippy"]To mark the diagram properly, here it is in the platform's format:
Sharing a policy across a workspace
In a workspace, define the policy once in the root Cargo.toml under [workspace.lints]. Each member crate then opts in.
# Root Cargo.toml
[workspace]
members = ["app", "core"]
resolver = "2"
[workspace.lints.rust]
unsafe_code = "forbid"
[workspace.lints.clippy]
pedantic = { level = "warn", priority = -1 }
unwrap_used = "warn"# app/Cargo.toml and core/Cargo.toml
[lints]
workspace = trueOpting in is not automatic. A member without workspace = true gets no workspace lints. A member also cannot mix inherited lints with its own [lints.clippy] entries. You either inherit the whole table or define a full one locally.
How it interacts with attributes and flags
Lint attributes in source code override [lints] settings for the item they annotate, except that nothing can override forbid. This is why #[allow(clippy::too_many_arguments)] still works on a single function when the project warns about it.
Use [lints] for the baseline and attributes for justified exceptions. In CI, keep -D warnings on the command line so that warnings fail the build, even though the table only says warn.
Common errors
- Writing
[lints.clippy] "clippy::pedantic" = "warn". The prefix belongs in attributes only. In the table, writepedantic. - Forgetting
priority = -1on a group, so individual overrides are ignored. - Adding
[workspace.lints]but not addingworkspace = trueto the members. - Using the table on a Cargo older than 1.74, which ignores or rejects it. Check
cargo --version.
Exercise
Your Cargo.toml has [lints.clippy] with pedantic = "warn" and module_name_repetitions = "allow", both at the default priority. You still see modulenamerepetitions warnings. What do you change, and why?
Show answer
Change the group entry to pedantic = { level = "warn", priority = -1 }. Both entries have priority 0, so Cargo applies them alphabetically, and pedantic comes after module_name_repetitions. The group therefore overrides the individual allow. With priority -1 the group is applied first and the allow wins.