Skip to content

Commit 53c5cff

Browse files
committed
Add trait to must_use.
1 parent 5bbf146 commit 53c5cff

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

src/attributes/diagnostics.md

+40-30
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,15 @@ The [RFC][1270-deprecation.md] contains motivations and more details.
147147

148148
## The `must_use` attribute
149149

150-
The *`must_use` attribute* can be used on user-defined composite types
150+
The *`must_use` attribute* is used to issue a diagnostic warning when a value
151+
is not "used". It can be applied to user-defined composite types
151152
([`struct`s][struct], [`enum`s][enum], and [`union`s][union]), [functions],
152153
and [traits].
153154

155+
The `must_use` attribute may include a message by using the
156+
[_MetaNameValueStr_] syntax such as `#[must_use = "example message"]`. The
157+
message will be given alongside the warning.
158+
154159
When used on user-defined composite types, if the [expression] of an
155160
[expression statement] has that type, then the `unused_must_use` lint is
156161
violated.
@@ -165,10 +170,8 @@ struct MustUse {
165170
# fn new() -> MustUse { MustUse {} }
166171
# }
167172
#
168-
fn main() {
169-
// Violates the `unused_must_use` lint.
170-
MustUse::new();
171-
}
173+
// Violates the `unused_must_use` lint.
174+
MustUse::new();
172175
```
173176

174177
When used on a function, if the [expression] of an [expression statement] is a
@@ -179,10 +182,25 @@ violated.
179182
#[must_use]
180183
fn five() -> i32 { 5i32 }
181184

182-
fn main() {
183-
// Violates the unused_must_use lint.
184-
five();
185+
// Violates the unused_must_use lint.
186+
five();
187+
```
188+
189+
When used on a [trait declaration], a [call expression] of an [expression
190+
statement] to a function that returns an [impl trait] of that trait violates
191+
the `unsued_must_use` lint.
192+
193+
```rust
194+
#[must_use]
195+
trait Critical {}
196+
impl Critical for i32 {}
197+
198+
fn get_critical() -> impl Critical {
199+
4i32
185200
}
201+
202+
// Violates the `unused_must_use` lint.
203+
get_critical();
186204
```
187205

188206
When used on a function in a trait declaration, then the behavior also applies
@@ -198,10 +216,8 @@ impl Trait for i32 {
198216
fn use_me(&self) -> i32 { 0i32 }
199217
}
200218

201-
fn main() {
202-
// Violates the `unused_must_use` lint.
203-
5i32.use_me();
204-
}
219+
// Violates the `unused_must_use` lint.
220+
5i32.use_me();
205221
```
206222

207223
When used on a function in a trait implementation, the attribute does nothing.
@@ -215,16 +231,14 @@ When used on a function in a trait implementation, the attribute does nothing.
215231
> #[must_use]
216232
> fn five() -> i32 { 5i32 }
217233
>
218-
> fn main() {
219-
> // None of these violate the unused_must_use lint.
220-
> (five(),);
221-
> Some(five());
222-
> { five() };
223-
> if true { five() } else { 0i32 };
224-
> match true {
225-
> _ => five()
226-
> };
227-
> }
234+
> // None of these violate the unused_must_use lint.
235+
> (five(),);
236+
> Some(five());
237+
> { five() };
238+
> if true { five() } else { 0i32 };
239+
> match true {
240+
> _ => five()
241+
> };
228242
> ```
229243
230244
> Note: It is idiomatic to use a [let statement] with a pattern of `_`
@@ -234,16 +248,10 @@ When used on a function in a trait implementation, the attribute does nothing.
234248
> #[must_use]
235249
> fn five() -> i32 { 5i32 }
236250
>
237-
> fn main() {
238-
> // Does not violate the unused_must_use lint.
239-
> let _ = five();
240-
> }
251+
> // Does not violate the unused_must_use lint.
252+
> let _ = five();
241253
> ```
242254
243-
The `must_use` attribute may include a message by using the
244-
[_MetaNameValueStr_] syntax such as `#[must_use = "example message"]`. The
245-
message will be given alongside the warning.
246-
247255
[Clippy]: https://github.com/rust-lang/rust-clippy
248256
[_MetaListNameValueStr_]: attributes.html#meta-item-attribute-syntax
249257
[_MetaListPaths_]: attributes.html#meta-item-attribute-syntax
@@ -258,13 +266,15 @@ message will be given alongside the warning.
258266
[expression]: expressions.html
259267
[external block item]: items/external-blocks.html
260268
[functions]: items/functions.html
269+
[impl trait]: types/impl-trait.html
261270
[implementation]: items/implementations.html
262271
[item]: items.html
263272
[let statement]: statements.html#let-statements
264273
[module]: items/modules.html
265274
[rustc book]: ../rustc/lints/index.html
266275
[struct field]: items/structs.html
267276
[struct]: items/structs.html
277+
[trait declaration]: items/traits.html
268278
[trait implementation items]: items/implementations.html#trait-implementations
269279
[trait item]: items/traits.html
270280
[traits]: items/traits.html

0 commit comments

Comments
 (0)