@@ -7,17 +7,19 @@ messages during compilation.
7
7
8
8
A lint check names a potentially undesirable coding pattern, such as
9
9
unreachable code or omitted documentation. The lint attributes ` allow ` ,
10
- ` warn ` , ` deny ` , and ` forbid ` use the [ _ MetaListPaths_ ] syntax to specify a
11
- list of lint names to change the lint level for the entity to which the
12
- attribute applies.
10
+ ` expect ` , ` warn ` , ` deny ` , and ` forbid ` use the [ _ MetaListPaths_ ] syntax
11
+ to specify a list of lint names to change the lint level for the entity
12
+ to which the attribute applies.
13
13
14
14
For any lint check ` C ` :
15
15
16
- * ` allow(C) ` overrides the check for ` C ` so that violations will go
16
+ * ` #[ allow(C)] ` overrides the check for ` C ` so that violations will go
17
17
unreported,
18
- * ` warn(C) ` warns about violations of ` C ` but continues compilation.
19
- * ` deny(C) ` signals an error after encountering a violation of ` C ` ,
20
- * ` forbid(C) ` is the same as ` deny(C) ` , but also forbids changing the lint
18
+ * ` #[expect(c)] ` suppresses all lint emissions of ` C ` , but will issue
19
+ a warning, if the lint wasn't emitted in the expected scope.
20
+ * ` #[warn(C)] ` warns about violations of ` C ` but continues compilation.
21
+ * ` #[deny(C)] ` signals an error after encountering a violation of ` C ` ,
22
+ * ` #[forbid(C)] ` is the same as ` deny(C) ` , but also forbids changing the lint
21
23
level afterwards,
22
24
23
25
> Note: The lint checks supported by ` rustc ` can be found via ` rustc -W help ` ,
@@ -83,6 +85,56 @@ pub mod m3 {
83
85
> [ command-line] [ rustc-lint-cli ] , and also supports [ setting
84
86
> caps] [ rustc-lint-caps ] on the lints that are reported.
85
87
88
+ All lint attributes support an additional ` reason ` parameter, to give context why
89
+ a certain attribute was added. This reason will be displayed as part of the lint
90
+ message, if the lint is emitted at the defined level.
91
+
92
+ ``` rust
93
+ use std :: path :: PathBuf ;
94
+
95
+ pub fn get_path () -> PathBuf {
96
+ #[allow(unused_mut, reason = " this is only modified on some platforms" )]
97
+ let mut file_name = PathBuf :: from (" git" );
98
+
99
+ #[cfg(target_os = " windows" )]
100
+ file_name . set_extension (" exe" );
101
+
102
+ file_name
103
+ }
104
+ ```
105
+
106
+ ### Lint expectations
107
+
108
+ With the ` #[expect] ` attributes lints can be expected in a certain scope. If
109
+ this expectation is not fulfilled a new warning is emitted to the user. The
110
+ lint levels can be overridden with other lint attributes as usual.
111
+
112
+ ``` rust
113
+ #[warn(missing_docs)]
114
+ pub mod m2 {
115
+ #[expect(missing_docs)]
116
+ pub mod nested {
117
+ // This missing documentation fulfills the expectation above
118
+ pub fn undocumented_one () -> i32 { 1 }
119
+
120
+ // Missing documentation signals a warning here, despite the expectation
121
+ // above. This emission would not fulfill the expectation
122
+ #[warn(missing_docs)]
123
+ pub fn undocumented_two () -> i32 { 2 }
124
+ }
125
+
126
+ #[expect(missing_docs)]
127
+ /// This comment explains something cool about the function. The
128
+ /// expectation will not be fulfilled and in turn issue a warning.
129
+ pub fn undocumented_too () -> i32 { 3 }
130
+ }
131
+ ```
132
+
133
+ > Note: Lint expectations have been proposed in [ RFC 2383] . It was not defined
134
+ > how expectations of the expectation lint should be handled. The rustc
135
+ > implementation currently doesn't allow the expextation of the
136
+ > ` unfulfilled_lint_expectation ` lint. This can change in the future.
137
+
86
138
### Lint groups
87
139
88
140
Lints may be organized into named groups so that the level of related lints
@@ -392,6 +444,7 @@ error[E0277]: My Message for `ImportantTrait<i32>` implemented for `String`
392
444
[ let statement ] : ../statements.md#let-statements
393
445
[ macro definition ] : ../macros-by-example.md
394
446
[ module ] : ../items/modules.md
447
+ [ RFC 2383 ] : https://rust-lang.github.io/rfcs/2383-lint-reasons.html
395
448
[ rustc book ] : ../../rustc/lints/index.html
396
449
[ rustc-lint-caps ] : ../../rustc/lints/levels.html#capping-lints
397
450
[ rustc-lint-cli ] : ../../rustc/lints/levels.html#via-compiler-flag
0 commit comments