Skip to content

Commit 435d191

Browse files
ehusstraviscross
authored andcommitted
Move expect examples to example blocks
1 parent bb86120 commit 435d191

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

src/attributes/diagnostics.md

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -159,74 +159,77 @@ r[attributes.diagnostics.expect]
159159
r[attributes.diagnostics.expect.behavior]
160160
The `expect(C)` attribute creates a lint expectation for lint `C`. The expectation will be fulfilled, if a `warn(C)` attribute at the same location would result in a lint emission. If the expectation is unfulfilled, because lint `C` would not be emitted, the `unfulfilled_lint_expectations` lint will be emitted at the attribute.
161161
162-
```rust
163-
fn main() {
164-
// This `#[expect]` attribute creates a lint expectation, that the `unused_variables`
165-
// lint would be emitted by the following statement. This expectation is
166-
// unfulfilled, since the `question` variable is used by the `println!` macro.
167-
// Therefore, the `unfulfilled_lint_expectations` lint will be emitted at the
168-
// attribute.
169-
#[expect(unused_variables)]
170-
let question = "who lives in a pineapple under the sea?";
171-
println!("{question}");
172-
173-
// This `#[expect]` attribute creates a lint expectation that will be fulfilled, since
174-
// the `answer` variable is never used. The `unused_variables` lint, that would usually
175-
// be emitted, is suppressed. No warning will be issued for the statement or attribute.
176-
#[expect(unused_variables)]
177-
let answer = "SpongeBob SquarePants!";
178-
}
179-
```
162+
> [!EXAMPLE]
163+
> ```rust
164+
> fn main() {
165+
> // This `#[expect]` attribute creates a lint expectation, that the `unused_variables`
166+
> // lint would be emitted by the following statement. This expectation is
167+
> // unfulfilled, since the `question` variable is used by the `println!` macro.
168+
> // Therefore, the `unfulfilled_lint_expectations` lint will be emitted at the
169+
> // attribute.
170+
> #[expect(unused_variables)]
171+
> let question = "who lives in a pineapple under the sea?";
172+
> println!("{question}");
173+
>
174+
> // This `#[expect]` attribute creates a lint expectation that will be fulfilled, since
175+
> // the `answer` variable is never used. The `unused_variables` lint, that would usually
176+
> // be emitted, is suppressed. No warning will be issued for the statement or attribute.
177+
> #[expect(unused_variables)]
178+
> let answer = "SpongeBob SquarePants!";
179+
> }
180+
> ```
180181
181182
r[attributes.diagnostics.expect.fulfillment]
182183
The lint expectation is only fulfilled by lint emissions which have been suppressed by the `expect` attribute. If the lint level is modified in the scope with other level attributes like `allow` or `warn`, the lint emission will be handled accordingly and the expectation will remain unfulfilled.
183184
184-
```rust
185-
#[expect(unused_variables)]
186-
fn select_song() {
187-
// This will emit the `unused_variables` lint at the warn level
188-
// as defined by the `warn` attribute. This will not fulfill the
189-
// expectation above the function.
190-
#[warn(unused_variables)]
191-
let song_name = "Crab Rave";
192-
193-
// The `allow` attribute suppresses the lint emission. This will not
194-
// fulfill the expectation as it has been suppressed by the `allow`
195-
// attribute and not the `expect` attribute above the function.
196-
#[allow(unused_variables)]
197-
let song_creator = "Noisestorm";
198-
199-
// This `expect` attribute will suppress the `unused_variables` lint emission
200-
// at the variable. The `expect` attribute above the function will still not
201-
// be fulfilled, since this lint emission has been suppressed by the local
202-
// expect attribute.
203-
#[expect(unused_variables)]
204-
let song_version = "Monstercat Release";
205-
}
206-
```
185+
> [!EXAMPLE]
186+
> ```rust
187+
> #[expect(unused_variables)]
188+
> fn select_song() {
189+
> // This will emit the `unused_variables` lint at the warn level
190+
> // as defined by the `warn` attribute. This will not fulfill the
191+
> // expectation above the function.
192+
> #[warn(unused_variables)]
193+
> let song_name = "Crab Rave";
194+
>
195+
> // The `allow` attribute suppresses the lint emission. This will not
196+
> // fulfill the expectation as it has been suppressed by the `allow`
197+
> // attribute and not the `expect` attribute above the function.
198+
> #[allow(unused_variables)]
199+
> let song_creator = "Noisestorm";
200+
>
201+
> // This `expect` attribute will suppress the `unused_variables` lint emission
202+
> // at the variable. The `expect` attribute above the function will still not
203+
> // be fulfilled, since this lint emission has been suppressed by the local
204+
> // expect attribute.
205+
> #[expect(unused_variables)]
206+
> let song_version = "Monstercat Release";
207+
> }
208+
> ```
207209
208210
r[attributes.diagnostics.expect.independent]
209-
If the `expect` attribute contains several lints, each one is expected separately. For a lint group it's enough if one lint inside the group has been emitted:
210-
211-
```rust
212-
// This expectation will be fulfilled by the unused value inside the function
213-
// since the emitted `unused_variables` lint is inside the `unused` lint group.
214-
#[expect(unused)]
215-
pub fn thoughts() {
216-
let unused = "I'm running out of examples";
217-
}
218-
219-
pub fn another_example() {
220-
// This attribute creates two lint expectations. The `unused_mut` lint will be
221-
// suppressed and with that fulfill the first expectation. The `unused_variables`
222-
// wouldn't be emitted, since the variable is used. That expectation will therefore
223-
// be unsatisfied, and a warning will be emitted.
224-
#[expect(unused_mut, unused_variables)]
225-
let mut link = "https://www.rust-lang.org/";
211+
If the `expect` attribute contains several lints, each one is expected separately. For a lint group it's enough if one lint inside the group has been emitted.
226212
227-
println!("Welcome to our community: {link}");
228-
}
229-
```
213+
> [!EXAMPLE]
214+
> ```rust
215+
> // This expectation will be fulfilled by the unused value inside the function
216+
> // since the emitted `unused_variables` lint is inside the `unused` lint group.
217+
> #[expect(unused)]
218+
> pub fn thoughts() {
219+
> let unused = "I'm running out of examples";
220+
> }
221+
>
222+
> pub fn another_example() {
223+
> // This attribute creates two lint expectations. The `unused_mut` lint will be
224+
> // suppressed and with that fulfill the first expectation. The `unused_variables`
225+
> // wouldn't be emitted, since the variable is used. That expectation will therefore
226+
> // be unsatisfied, and a warning will be emitted.
227+
> #[expect(unused_mut, unused_variables)]
228+
> let mut link = "https://www.rust-lang.org/";
229+
>
230+
> println!("Welcome to our community: {link}");
231+
> }
232+
> ```
230233
231234
> [!NOTE]
232235
> The behavior of `expect(unfulfilled_lint_expectations)` is currently defined to always generate the `unfulfilled_lint_expectations` lint.

0 commit comments

Comments
 (0)