Sub topic 10
Enforcing shared lints with [workspace.lints]
This chapter assumes you can read a single-package Cargo.toml, you know what a lint is (a compiler or Clippy check that warns about or rejects certain code), and you have the workspace from earlier chapters: a virtual root manifest with textkit and app under crates/.
Why share lints
Without a shared setting, each crate decides its own lint levels. One crate forbids unsafe, another forgets, and the rules drift. The [workspace.lints] table puts the policy in one place. Members opt in with a single line.
Cargo supports this since Rust 1.74.
Defining lints in the root
The root manifest groups lints by tool. rust holds compiler lints and clippy holds Clippy lints.
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.lints.rust]
unsafe_code = "forbid"
unused_must_use = "deny"
[workspace.lints.clippy]
dbg_macro = "warn"
todo = "warn"Each entry maps a lint name to a level. The levels are forbid, deny, warn, allow and force-warn. A forbid level cannot be overridden by #[allow] attributes in the source.
Opting in from a member
Defining lints in the root does nothing by itself. Each member must say it wants them:
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
[lints]
workspace = trueThis is different from version.workspace = true. Lints use a separate [lints] table, and the only key you can set there when inheriting is workspace = true.
Checking that it works
Add a small violation to crates/textkit/src/lib.rs:
pub fn shout(input: &str) -> String {
let x = unsafe { 1 + 1 };
format!("{}{}!", input.to_uppercase(), x)
}Run cargo build -p textkit. The compiler stops with an error that usage of an unsafe block is forbidden, and it names the lint level that came from the workspace. Remove the unsafe block and the build passes. Run cargo clippy --workspace to apply the clippy entries as well.
Common errors
- Forgetting
[lints] workspace = true. The root table is silently ignored for that crate. If a lint "does not fire", check this first. - Mixing inheritance with local lints. You cannot write both
workspace = trueand extra entries such as[lints.rust]in the same member. Cargo reports an error. Lint levels apply to the whole workspace as one set. - Using an older toolchain. Before Rust 1.74, Cargo rejects the
[workspace.lints]table or ignores it with a warning about an unused manifest key. - Expecting
forbidto be a build-wide switch. It applies per crate that opted in. Dependencies from crates.io are built with their own lint settings, and Cargo caps lints for them so they do not break your build.
Summary of the pattern
- Declare
[workspace.lints.rust]and[workspace.lints.clippy]in the root. - Add
[lints]withworkspace = trueto every member. - Verify with
cargo clippy --workspace.
Exercise
A teammate adds unwrap_used = "deny" under [workspace.lints.clippy], but cargo clippy --workspace still accepts .unwrap() calls in the app crate. Name two likely causes.
Show answer
First, crates/app/Cargo.toml may lack a [lints] table with workspace = true, so app never inherits the lint. Second, the code may carry a local #[allow(clippy::unwrap_used)] attribute, which overrides deny. Only forbid blocks such an attribute. A third possibility is that the lint was placed under [workspace.lints.rust] instead of [workspace.lints.clippy].
Exercise
Why can a member crate not combine [lints] workspace = true with its own [lints.rust] entries?
Show answer
Cargo treats inherited lints as one complete set defined by the workspace, and it rejects a manifest that mixes the two. To use different rules, a crate must either define all its own lints or inherit the workspace set.