Skip to content

Commit 1cc29b6

Browse files
committed
Document new #[expect] attribute and reasons parameter (RFC 2383)
1 parent 3b68105 commit 1cc29b6

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

Diff for: src/attributes.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ The following is an index of all built-in attributes.
222222
- [`proc_macro_derive`] — Defines a derive macro.
223223
- [`proc_macro_attribute`] — Defines an attribute macro.
224224
- Diagnostics
225-
- [`allow`], [`warn`], [`deny`], [`forbid`] — Alters the default lint level.
225+
- [`allow`], [`expect`], [`warn`], [`deny`], [`forbid`] — Alters the default lint level.
226226
- [`deprecated`] — Generates deprecation notices.
227227
- [`must_use`] — Generates a lint for unused values.
228228
- [`diagnostic::on_unimplemented`] — Hints the compiler to emit a certain error
@@ -303,6 +303,7 @@ The following is an index of all built-in attributes.
303303
[`deprecated`]: attributes/diagnostics.md#the-deprecated-attribute
304304
[`derive`]: attributes/derive.md
305305
[`export_name`]: abi.md#the-export_name-attribute
306+
[`expect`]: attributes/diagnostics.md#lint-check-attributes
306307
[`forbid`]: attributes/diagnostics.md#lint-check-attributes
307308
[`global_allocator`]: runtime.md#the-global_allocator-attribute
308309
[`ignore`]: attributes/testing.md#the-ignore-attribute

Diff for: src/attributes/diagnostics.md

+60-7
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ messages during compilation.
77

88
A lint check names a potentially undesirable coding pattern, such as
99
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.
1313

1414
For 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,56 @@ 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+
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+
86138
### Lint groups
87139

88140
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`
392444
[let statement]: ../statements.md#let-statements
393445
[macro definition]: ../macros-by-example.md
394446
[module]: ../items/modules.md
447+
[RFC 2383]: https://rust-lang.github.io/rfcs/2383-lint-reasons.html
395448
[rustc book]: ../../rustc/lints/index.html
396449
[rustc-lint-caps]: ../../rustc/lints/levels.html#capping-lints
397450
[rustc-lint-cli]: ../../rustc/lints/levels.html#via-compiler-flag

0 commit comments

Comments
 (0)