Sub topic 10
Enforcing shared lints with [workspace.lints]
A lint is a check that flags suspicious or unidiomatic code. rustc has built-in lints, and Clippy and rustdoc add more. In a multi-crate project, each crate usually needs the same lint rules. Without a shared place for them, the rules end up copied into every lib.rs and main.rs, and they drift apart.
This chapter assumes you can already:
- Write a workspace root manifest with
members. - Use
[workspace.package]andx.workspace = trueinheritance, as in the earlier chapters. - Run
cargo clippy.
The [workspace.lints] table needs Rust 1.74 or newer.
How lint inheritance works
Lint inheritance has two parts, and you need both:
- The workspace root defines the rules under
[workspace.lints]. - Each member opts in by writing
[lints]withworkspace = true.
A member that does not opt in gets no shared lints. Adding a crate to members does not turn them on, which is a common cause of "my rule isn't applied here".
Defining lints in the root
The table is split by tool. Each sub-table is named after the tool that owns the lints:
| Sub-table | Tool | Example lint |
|---|---|---|
| `[workspace.lints.rust]` | `rustc` | `unsafe_code`, `missing_docs` |
| `[workspace.lints.clippy]` | Clippy | `dbg_macro`, `unwrap_used` |
| `[workspace.lints.rustdoc]` | rustdoc | `broken_intra_doc_links` |
Each lint is set to a level. The levels are allow, warn, deny and forbid. allow is silent. warn prints a warning. deny is a hard error. forbid is a deny that code cannot relax with an #[allow] attribute.
This root manifest extends the workspace from the earlier chapters:
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
[workspace.dependencies]
textkit = { path = "crates/textkit" }
[workspace.lints.rust]
unsafe_code = "forbid"
missing_docs = "warn"
[workspace.lints.clippy]
dbg_macro = "warn"
todo = "warn"
unwrap_used = "warn"The [workspace.lints] table works in a virtual manifest. You don't need a root package.
Opting in from a member
Add this to each member's manifest:
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[lints]
workspace = trueThe key is workspace = true inside [lints]. It is not lints.workspace, and it is not placed under [package]. If the workspace root is also a package, it must write [lints] workspace = true itself, because it is not exempt from opting in.
Seeing the lints work
Give textkit a documented function that breaks no rules:
//! Small text helpers.
/// Returns the input in upper case followed by an exclamation mark.
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}Now add a function that uses unsafe:
//! Small text helpers.
/// Returns the first byte of the input without a bounds check.
pub fn first_byte(input: &str) -> u8 {
unsafe { *input.as_bytes().get_unchecked(0) }
}Run cargo build --workspace. The build fails in textkit with an error that says the unsafe block is not allowed, because the lint is set to forbid. If you change the root to unsafe_code = "warn", every opted-in member sees a warning on the next build. You edited one file and the behavior changed everywhere.
To see Clippy's rules, run:
cargo clippy --workspacecargo clippy reads the [workspace.lints.clippy] table. cargo build ignores it, because Clippy lints only run under Clippy.
Lint groups and priority
Both rustc and Clippy organize lints into groups, such as clippy::pedantic or rust_2018_idioms. A group can turn on dozens of lints at once, and you may want to switch off one lint in it. That needs a way to say which setting wins when two rules overlap.
Cargo lets you attach a priority to a lint. Lints with a lower number are applied first, and lints with a higher number are applied later and win. The default priority is 0.
[workspace.lints.rust]
rust_2018_idioms = { level = "warn", priority = -1 }
unsafe_code = "forbid"
[workspace.lints.clippy]
pedantic = { level = "warn", priority = -1 }
module_name_repetitions = "allow"
unwrap_used = "warn"Here the groups have priority -1. They are applied first. The individual lints have the default priority 0, so they are applied afterward and override the group. module_name_repetitions belongs to clippy::pedantic, and the allow takes effect.
Without the negative priority on the group, both entries have priority 0. The order between them is then not something you should rely on, and the group can undo your single-lint setting. The rule is simple: put groups at a negative priority and individual lints at the default.
What you cannot do
You cannot mix inherited and local lint rules in the same [lints] table. This manifest is an error:
[lints]
workspace = true
[lints.rust]
missing_docs = "allow"Cargo rejects it. A member either takes the whole workspace table or defines its own [lints.*] tables. It cannot take the workspace table and then change one entry.
There are three ways to handle an exception:
- Attribute in source: Write
#![allow(missing_docs)]at the top of the crate root, or#[allow(...)]on one item. Cargo passes workspace lints to the compiler as command-line flags, and attributes in the source take precedence over them. This does not work forforbid. Anallowunder aforbidis a compile error. - Opt out and define a local set: Skip
workspace = truefor that crate and write its own[lints.rust]table. You then maintain that table yourself. - Loosen the workspace level: If many crates need the exception, change the root from
forbidtodeny, which source attributes can still relax.
Use forbid only for rules you never want a crate to break. unsafe_code is a good example if the workspace has no unsafe code at all. If one crate needs FFI, choose deny at the root instead, and add #![allow(unsafe_code)] only in that crate.
Scope: what gets linted
Lints apply to the package's own targets: its library, binaries, tests, examples and benches. They do not apply to external dependencies from crates.io. Cargo caps the lint level for those at warn-or-below, so someone else's code cannot break your build.
Path dependencies between members are not "external". Each one is a workspace member and is linted by the settings in its own manifest. If app depends on textkit, building app also compiles textkit, with textkit's lints.
Common errors
- Lint silently not applied. The member lacks
[lints] workspace = true. Check the manifest of the crate in question first. - Wrong table name.
[workspace.lint](singular) is not recognized. The correct name is[workspace.lints]. - Error on
[lints]plus[lints.rust]. Remove one of them, as described above. - Group overrides a single lint. Give the group
priority = -1. - Unknown lint warnings. Newer Clippy lints named in the root cause "unknown lint" warnings on older toolchains. Pin the toolchain with
rust-toolchain.tomlso all members and CI use the same version. - Clippy rules ignored in CI. CI ran
cargo buildorcargo test, which do not run Clippy. Add an explicitcargo clippy --workspacestep.
To turn warnings into failures in CI without editing the manifest, pass a flag after --:
cargo clippy --workspace --all-targets -- -D warningsThis is a different approach from setting deny in the root. A warn level in the manifest keeps local development comfortable, and -D warnings makes the same rules strict in CI.
Exercise
A workspace root sets [workspace.lints.clippy] with pedantic = "warn" and needless_pass_by_value = "allow", both at the default priority. The needless_pass_by_value warning still shows up in a member that has [lints] workspace = true. What is the likely cause, and how do you fix it?
Show answer
needless_pass_by_value is part of the pedantic group, and both entries have priority 0, so the group can override the single-lint setting. Give the group a lower priority: pedantic = { level = "warn", priority = -1 }. The group is then applied first and the allow is applied after it and wins.
Exercise
A member crate ffi must contain unsafe code, but the root sets unsafe_code = "forbid". Why does #![allow(unsafe_code)] inside ffi not help, and what are two ways to resolve it?
Show answer
A forbid level cannot be relaxed by an allow attribute, so the compiler reports an error. You can change the root level to deny and add #![allow(unsafe_code)] in ffi. Or you can leave out workspace = true in ffi and give it its own [lints.rust] table that does not forbid unsafe_code.