@@ -7,17 +7,19 @@ messages during compilation.
77
88A lint check names a potentially undesirable coding pattern, such as
99unreachable 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.
1313
1414For any lint check ` C ` :
1515
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
1717 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
2123 level afterwards,
2224
2325> Note: The lint checks supported by ` rustc ` can be found via ` rustc -W help ` ,
@@ -83,6 +85,60 @@ pub mod m3 {
8385> [ command-line] [ rustc-lint-cli ] , and also supports [ setting
8486> caps] [ rustc-lint-caps ] on the lints that are reported.
8587
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+ #![feature(lint_reasons)]
94+
95+ use std :: path :: PathBuf ;
96+
97+ pub fn get_path () -> PathBuf {
98+ #[allow(unused_mut, reason = " this is only modified on some platforms" )]
99+ let mut file_name = PathBuf :: from (" git" );
100+
101+ #[cfg(target_os = " windows" )]
102+ file_name . set_extension (" exe" );
103+
104+ file_name
105+ }
106+ ```
107+
108+ ### Lint expectations
109+
110+ With the ` #[expect] ` attributes lints can be expected in a certain scope. If
111+ this expectation is not fulfilled a new warning is emitted to the user. The
112+ lint levels can be overridden with other lint attributes as usual.
113+
114+ ``` rust
115+ #![feature(lint_reasons)]
116+
117+ #[warn(missing_docs)]
118+ pub mod m2 {
119+ #[expect(missing_docs)]
120+ pub mod nested {
121+ // This missing documentation fulfills the expectation above
122+ pub fn undocumented_one () -> i32 { 1 }
123+
124+ // Missing documentation signals a warning here, despite the expectation
125+ // above. This emission would not fulfill the expectation
126+ #[warn(missing_docs)]
127+ pub fn undocumented_two () -> i32 { 2 }
128+ }
129+
130+ #[expect(missing_docs)]
131+ /// This comment explains something cool about the function. The
132+ /// expectation will not be fulfilled and in turn issue a warning.
133+ pub fn undocumented_too () -> i32 { 3 }
134+ }
135+ ```
136+
137+ > Note: Lint expectations have been proposed in [ RFC 2383] . It was not defined
138+ > how expectations of the expectation lint should be handled. The rustc
139+ > implementation currently doesn't allow the expextation of the
140+ > ` unfulfilled_lint_expectation ` lint. This can change in the future.
141+
86142### Lint groups
87143
88144Lints may be organized into named groups so that the level of related lints
@@ -322,6 +378,7 @@ When used on a function in a trait implementation, the attribute does nothing.
322378[let statement ]: .. / statements . md#let - statements
323379[macro definition ]: .. / macros - by - example . md
324380[module ]: .. / items / modules . md
381+ [RFC 2383 ]: https : // rust-lang.github.io/rfcs/2383-lint-reasons.html
325382[rustc book ]: .. / .. / rustc / lints / index . html
326383[rustc - lint - caps ]: .. / .. / rustc / lints / levels . html#capping - lints
327384[rustc - lint - cli ]: .. / .. / rustc / lints / levels . html#via - compiler - flag
0 commit comments