Skip to content

Commit 6e8d426

Browse files
committed
Doc: clarify priority of lint level sources
This updates the rustc book to clearly document how conflicting lint configurations are resolved across different sources, including command-line flags, crate-level attributes, in-line attributes, and `--cap-lints`. It also explains the special behavior of `forbid` and `force_warn`.
1 parent 449c801 commit 6e8d426

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/doc/rustc/src/lints/levels.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,56 @@ $
330330
331331
This feature is used heavily by Cargo; it will pass `--cap-lints allow` when
332332
compiling your dependencies, so that if they have any warnings, they do not
333-
pollute the output of your build.
333+
pollute the output of your build. However, note that `--cap-lints allow` does **not** override lints marked as `force-warn`.
334+
335+
## Priority of Lint Level Sources
336+
337+
Rust allows setting lint levels (`allow`, `warn`, `deny`, etc.) through various sources:
338+
339+
- Attributes (`#[allow(...)]`, `#![deny(...)]`, etc.)
340+
- Command-line options (e.g., `--cap-lints`, `-A unused_variables`)
341+
342+
These mechanisms can interact in subtle ways, the compiler resolves conflicts based on the **source’s scope and strength**.
343+
344+
### Priority of sources (`allow`, `warn`, and `deny`)
345+
346+
For standard lint levels, **the most specific scope** takes precedence.
347+
348+
For example, the following allows the `unused-variables` lint, because it has a more specific scope:
349+
350+
```rust
351+
#![deny(unused_variables)]
352+
353+
#[allow(unused_variables)]
354+
fn main() {
355+
let x = 42; // Allow wins
356+
}
357+
```
358+
359+
### Priority of sources (`force-warn`and `forbid`)
360+
361+
Special lint levels take precedence over standard levels. When multiple special levels are in effect, **the least specific (outermost) scope** takes precedence.
362+
363+
For example, consider the following:
364+
365+
```rust
366+
#[forbid(unused_variables)]
367+
fn main() {
368+
let x = 42; // Allow wins
369+
}
370+
```
371+
372+
If we compile it with `--force-warn unused_variables`:
373+
374+
```bash
375+
$ rustc --force-warn unused_variables lib.rs
376+
warning: unused variable: `x`
377+
--> lib.rs:3:9
378+
|
379+
40 | let x = 42;
380+
| ^ help: if this is intentional, prefix it with an underscore: `_x`
381+
|
382+
= note: requested on the command line with `--force-warn unused-variables`
383+
384+
warning: 1 warning emitted
385+
```

0 commit comments

Comments
 (0)