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,41 @@ 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, with
41
+ it a lint can be expected. If the lint in question is not an emitted, a new lint
42
+ ` unfulfilled_lint_expectation ` at the attribute notifying you that the expectation,
43
+ is no 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 usefull" ;
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
+ The fulfillment of expectations is still tracked.
71
+
36
72
## warn
37
73
38
74
The 'warn' lint level will produce a warning if you violate the lint. For example,
@@ -240,6 +276,24 @@ And use multiple attributes together:
240
276
pub fn foo() {}
241
277
```
242
278
279
+ All lint attributes support an additional `reason` parameter, to give context why
280
+ a certain attribute was added. This reason will be displayed as part of the lint
281
+ message, if the lint is emitted at the defined level.
282
+
283
+ ```rust
284
+ use std::path::PathBuf;
285
+
286
+ pub fn get_path() -> PathBuf {
287
+ #[allow(unused_mut, reason = "this is only modified on some platforms")]
288
+ let mut file_name = PathBuf::from("git");
289
+
290
+ #[cfg(target_os = "windows")]
291
+ file_name.set_extension("exe");
292
+
293
+ file_name
294
+ }
295
+ ```
296
+
243
297
### Capping lints
244
298
245
299
`rustc` supports a flag, `--cap-lints LEVEL` that sets the "lint cap level."
0 commit comments