Skip to content

Commit a268f71

Browse files
committed
Improve #[expect] documentation again
1 parent 41b63dc commit a268f71

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/attributes/diagnostics.md

+24-22
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ to which the attribute applies.
1414
For any lint check `C`:
1515

1616
* `#[allow(C)]` overrides the check for `C` so that violations will go
17-
unreported,
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.
17+
unreported.
18+
* `#[expect(C)]` indicates that lint `C` is expected to be emitted. The
19+
attribute will suppres the emission of `C` or issue a warning, if the
20+
expectation is unfillfilled.
2021
* `#[warn(C)]` warns about violations of `C` but continues compilation.
2122
* `#[deny(C)]` signals an error after encountering a violation of `C`,
2223
* `#[forbid(C)]` is the same as `deny(C)`, but also forbids changing the lint
@@ -104,14 +105,13 @@ message if the lint is emitted at the defined level.
104105
fn dyn() {}
105106
```
106107

107-
Here we have another example, where the lint is allowed with a reason:
108+
Here is another example, where the lint is allowed with a reason:
108109

109110
```rust
110111
use std::path::PathBuf;
111112

112113
pub fn get_path() -> PathBuf {
113-
// Using `reason` with an `allow` attribute has no effect other than to
114-
// provide documentation to the reader.
114+
// The `reason` parameter on `allow` attributes acts as documentation for the reader.
115115
#[allow(unused_mut, reason = "this is only modified on some platforms")]
116116
let mut file_name = PathBuf::from("git");
117117

@@ -122,35 +122,37 @@ pub fn get_path() -> PathBuf {
122122
}
123123
```
124124

125-
### The `expect` attribute
125+
### The `#[expect]` attribute
126126

127-
The *`expect` attribute* is used to mark that a particular lint must be triggered
128-
within its scope. If this expectation is not fulfilled a new warning is emitted to
129-
the user.
127+
The `#[expect(C)]` attribute creates a lint expectation for lint `C`. The
128+
expectation will be fulfilled, if a `#[warn(C)]` attribute at the same location
129+
would result in a lint emission. If the expectation is unfulfilled, because
130+
lint `C` would not be emitted, the `unfulfilled_lint_expectations` lint will
131+
be emitted at the attribute.
130132

131133
```rust
132134
fn main() {
133-
// This `expect` attribute creates an expectation, that the `unused_variables`
134-
// will be triggered by the following statement. This expectation will not be
135-
// fulfilled, since the `question` variable is used by the `println!` macro.
135+
// This `#[expect]` attribute creates a lint expectation, that the `unused_variables`
136+
// lint would be emitted by the following statement. This expectation is
137+
// unfulfilled, since the `question` variable is used by the `println!` macro.
138+
// Therefore, the `unfulfilled_lint_expectations` lint will be emitted at the
139+
// attribute.
136140
#[expect(unused_variables)]
137141
let question = "who lives in a pineapple under the sea?";
138142
println!("{question}");
139143

140-
// This `expect` attribute creates an expectation that will be fulfilled, since
141-
// the `answer` variable is never used. It will therefore trigger the
142-
// `unused_variables` lint which will be suppressed by the expectation and fullfil
143-
// it as well.
144+
// This `#[expect]` attribute creates a lint expectation that will be fulfilled, since
145+
// the `answer` variable is never used. The `unused_variables` lint, that would usually
146+
// be emitted, is supressed. No warning will be issued for the statement or attribute.
144147
#[expect(unused_variables)]
145148
let answer = "SpongeBob SquarePants!";
146149
}
147150
```
148151

149152
The lint expectation is only fulfilled by lint emissions which have been suppressed by
150153
the `expect` attribute. If the lint level is modified in the scope with other level
151-
attributes like `warn` or `deny`, the lint will be emitted at the defined level and not
152-
satisdy the expectation. Lint suppressions via `allow` or `expect` attributes inside the
153-
scope will also not fulfill the expectation.
154+
attributes like `allow` or `warn`, the lint emission will be handled accordingly and the
155+
expectation will remain unfulfilled.
154156

155157
```rust
156158
#[expect(unused_variables)]
@@ -190,8 +192,8 @@ pub fn thoughts() {
190192
pub fn another_example() {
191193
// This attribute creates two lint expectations. The `unused_mut` lint will be
192194
// suppressed and with that fulfill the first expectation. The `unused_variables`
193-
// won't be emitted, since the variable is used. That expectation will therefore
194-
// not be satisfied, and a warning will be emitted.
195+
// wouldn't be emitted, since the variable is used. That expectation will therefore
196+
// be unsatified, and a warning will be emitted.
195197
#[expect(unused_mut, unused_variables)]
196198
let mut link = "https://www.rust-lang.org/";
197199

0 commit comments

Comments
 (0)