You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The *`must_use`[attribute][attributes]* is used to issue a diagnostic warning when a value is not used.
353
+
The *`must_use`[attribute]* marks a value that should be used.
354
354
355
355
r[attributes.diagnostics.must_use.syntax]
356
-
The `must_use` attribute uses either the [MetaWord]syntax or the [MetaNameValueStr]syntax to be able to [specify a message][attributes.diagnostics.must_use.message].
356
+
The `must_use` attribute uses the [MetaWord]and [MetaNameValueStr]syntaxes.
357
357
358
358
> [!EXAMPLE]
359
359
> ```rust
360
360
> #[must_use]
361
-
> fnuse_me() ->u8 { 0 }
361
+
> fnuse_me1() ->u8 { 0 }
362
362
>
363
-
> #[must_use ="explanation of why it must be used"]
363
+
> #[must_use ="explanation of why it should be used"]
364
364
> fnuse_me2() ->u8 { 0 }
365
365
> ```
366
366
@@ -383,102 +383,118 @@ The `must_use` attribute may be used only once on an item.
383
383
> `rustc` lints against any use following the first.This may become an error in the future.
384
384
385
385
r[attributes.diagnostics.must_use.message]
386
-
The `must_use` attribute may include a message by using the [MetaNameValueStr] syntax such as`#[must_use = "example message"]`.The message will be given alongside the warning.
386
+
The `must_use` attribute may include a message by using the [MetaNameValueStr] syntax, e.g., `#[must_use = "example message"]`.The message may be emitted as part of the lint.
387
387
388
388
r[attributes.diagnostics.must_use.type]
389
-
Whenused on user-defined composite types, if the [expression] of an [expression statement] has that type, then the `unused_must_use` lint is violated.
389
+
Whenthe attribute is applied to a [struct], [enumeration], or [union], if the [expression] of an [expression statement] has that type, the use triggers the `unused_must_use` lint.
390
390
391
-
> [!EXAMPLE]
392
-
> ```rust
393
-
> #[must_use]
394
-
> structMustUse {
395
-
> // some fields
396
-
> }
397
-
>
398
-
> # implMustUse {
399
-
> # fn new() ->MustUse { MustUse {} }
400
-
> # }
401
-
> #
402
-
> // Violates the `unused_must_use` lint.
403
-
> MustUse::new();
404
-
> ```
391
+
```rust,compile_fail
392
+
#![deny(unused_must_use)]
393
+
#[must_use]
394
+
structMustUse();
395
+
MustUse(); // ERROR: Unused value that must be used.
396
+
```
405
397
406
398
r[attributes.diagnostics.must_use.fn]
407
-
Whenusedonafunction, ifthe [expression] ofan [expressionstatement] isa [callexpression] tothatfunction, thenthe `unused_must_use` lintisviolated.
399
+
If the [expression] of an [expression statement] is a [call expression]or [method call expression] whose function operand is a function to which the attribute is applied, the use triggers the `unused_must_use` lint.
408
400
409
-
> [!EXAMPLE]
410
-
> ```rust
411
-
> #[must_use]
412
-
> fnfive() ->i32 { 5i32 }
413
-
>
414
-
> // Violates the unused_must_use lint.
415
-
> five();
416
-
> ```
401
+
```rust,compile_fail
402
+
#![deny(unused_must_use)]
403
+
#[must_use]
404
+
fn f() {}
405
+
f(); // ERROR: Unused return value that must be used.
406
+
```
417
407
418
408
r[attributes.diagnostics.must_use.trait]
419
-
Whenusedona [traitdeclaration], a [callexpression] ofan [expressionstatement] toafunctionthatreturnsan [impltrait] ora [dyntrait] ofthattraitviolatesthe `unused_must_use` lint.
409
+
If the [expression] of an [expression statement] is a [call expression]or [method call expression] whose function operand is a function that returns an [impl trait] or a [dyn trait]type where one or more traits in the bound are marked with the attribute, the use triggers the `unused_must_use` lint.
420
410
421
-
> [!EXAMPLE]
422
-
> ```rust
423
-
> #[must_use]
424
-
> traitCritical {}
425
-
> implCriticalfori32 {}
426
-
>
427
-
> fnget_critical() ->implCritical {
428
-
> 4i32
429
-
> }
430
-
>
431
-
> // Violates the `unused_must_use` lint.
432
-
> get_critical();
433
-
> ```
411
+
```rust,compile_fail
412
+
#![deny(unused_must_use)]
413
+
#[must_use]
414
+
trait Tr {}
415
+
impl Tr for () {}
416
+
fn f() -> impl Tr {}
417
+
f(); // ERROR: Unused implementor that must be used.
When the attribute is applied to a function in a trait declaration, the rules described in [attributes.diagnostics.must_use.fn]also apply when the function operand of the [call expression] or [method call expression] is an implementation of that function.
437
422
438
-
> [!EXAMPLE]
439
-
> ```rust
440
-
> traitTrait {
441
-
> #[must_use]
442
-
> fnuse_me(&self) ->i32;
443
-
> }
444
-
>
445
-
> implTraitfori32 {
446
-
> fnuse_me(&self) ->i32 { 0i32 }
447
-
> }
448
-
>
449
-
> // Violates the `unused_must_use` lint.
450
-
> 5i32.use_me();
451
-
> ```
423
+
```rust,compile_fail
424
+
#![deny(unused_must_use)]
425
+
trait Tr {
426
+
#[must_use]
427
+
fn use_me(&self);
428
+
}
429
+
430
+
impl Tr for () {
431
+
fn use_me(&self) {}
432
+
}
433
+
434
+
().use_me(); // ERROR: Unused return value that must be used.
435
+
```
436
+
437
+
```rust,compile_fail
438
+
# #![deny(unused_must_use)]
439
+
# trait Tr {
440
+
# #[must_use]
441
+
# fn use_me(&self);
442
+
# }
443
+
#
444
+
# impl Tr for () {
445
+
# fn use_me(&self) {}
446
+
# }
447
+
#
448
+
<() as Tr>::use_me(&());
449
+
// ^^^^^^^^^^^ ERROR: Unused return value that must be used.
0 commit comments