1
1
# Lint Levels
2
2
3
- In ` rustc ` , lints are divided into five * levels* :
3
+ In ` rustc ` , lints are divided into six * levels* :
4
4
5
5
1 . allow
6
- 2 . warn
7
- 3 . force-warn
8
- 4 . deny
9
- 5 . forbid
6
+ 2 . expect
7
+ 3 . warn
8
+ 4 . force-warn
9
+ 5 . deny
10
+ 6 . forbid
10
11
11
12
Each lint has a default level (explained in the lint listing later in this
12
13
chapter), and the compiler has a default warning level. First, let's explain
@@ -33,6 +34,40 @@ But this code violates the `missing_docs` lint.
33
34
These lints exist mostly to be manually turned on via configuration, as we'll
34
35
talk about later in this section.
35
36
37
+ ## expect
38
+
39
+ Sometimes, it can be helpful to suppress lints, but at the same time ensure that
40
+ the code in question still emits them. The 'expect' level does exactly this. If
41
+ the lint in question is not emitted, the ` unfulfilled_lint_expectation ` lint
42
+ triggers on the ` expect ` attribute, notifying you that the expectation is no
43
+ longer fulfilled.
44
+
45
+ ``` rust
46
+ fn main () {
47
+ #[expect(unused_variables)]
48
+ let unused = " Everyone ignores me" ;
49
+
50
+ #[expect(unused_variables)]
51
+ let used = " I'm useful" ;
52
+ println! (" The `used` value is equal to: {:?}" , used );
53
+ }
54
+ ```
55
+
56
+ This will produce the following warning:
57
+
58
+ ``` txt
59
+ warning: this lint expectation is unfulfilled
60
+ --> src/main.rs:7:14
61
+ |
62
+ 7 | #[expect(unused_variables)]
63
+ | ^^^^^^^^^^^^^^^^
64
+ |
65
+ = note: `#[warn(unfulfilled_lint_expectations)]` on by default
66
+ ```
67
+
68
+ This level can only be defined via the ` #[expect] ` attribute and not via the
69
+ console. Lints with the special 'force-warn' lint will still be emitted as usual.
70
+
36
71
## warn
37
72
38
73
The 'warn' lint level will produce a warning if you violate the lint. For example,
@@ -240,6 +275,21 @@ And use multiple attributes together:
240
275
pub fn foo() {}
241
276
```
242
277
278
+ All lint attributes support an additional `reason` parameter, to give context why
279
+ a certain attribute was added. This reason will be displayed as part of the lint
280
+ message, if the lint is emitted at the defined level.
281
+
282
+ ```rust
283
+ use std::path::PathBuf;
284
+ pub fn get_path() -> PathBuf {
285
+ #[allow(unused_mut, reason = "this is only modified on some platforms")]
286
+ let mut file_name = PathBuf::from("git");
287
+ #[cfg(target_os = "windows")]
288
+ file_name.set_extension("exe");
289
+ file_name
290
+ }
291
+ ```
292
+
243
293
### Capping lints
244
294
245
295
`rustc` supports a flag, `--cap-lints LEVEL` that sets the "lint cap level."
0 commit comments