@@ -166,9 +166,6 @@ macro scope.
166
166
object file that this item's contents will be placed into.
167
167
- ` no_mangle ` - on any item, do not apply the standard name mangling. Set the
168
168
symbol for this item to its identifier.
169
- - ` must_use ` - on structs and enums, will warn if a value of this type isn't used or
170
- assigned to a variable. You may also include an optional message by using
171
- ` #[must_use = "message"] ` which will be given alongside the warning.
172
169
173
170
### Deprecation
174
171
@@ -325,7 +322,7 @@ The lint checks supported by the compiler can be found via `rustc -W help`,
325
322
along with their default settings. [ Compiler
326
323
plugins] [ unstable book plugin ] can provide additional lint checks.
327
324
328
- ``` rust,ignore
325
+ ``` rust
329
326
pub mod m1 {
330
327
// Missing documentation is ignored here
331
328
#[allow(missing_docs)]
@@ -366,7 +363,7 @@ pub mod m2{
366
363
This example shows how one can use ` forbid ` to disallow uses of ` allow ` for
367
364
that lint check:
368
365
369
- ``` rust,ignore
366
+ ``` rust,compile_fail
370
367
#[forbid(missing_docs)]
371
368
pub mod m3 {
372
369
// Attempting to toggle warning signals an error here
@@ -376,6 +373,105 @@ pub mod m3 {
376
373
}
377
374
```
378
375
376
+ #### ` must_use ` Attribute
377
+
378
+ The ` must_use ` attribute can be used on user-defined composite types
379
+ ([ ` struct ` s] [ struct ] , [ ` enum ` s] [ enum ] , and [ ` union ` s] [ union ] ) and [ functions] .
380
+
381
+ When used on user-defined composite types, if the [ expression] of an
382
+ [ expression statement] has that type, then the ` unused_must_use ` lint is
383
+ violated.
384
+
385
+ ``` rust
386
+ #[must_use]
387
+ struct MustUse {
388
+ // some fields
389
+ }
390
+
391
+ # impl MustUse {
392
+ # fn new () -> MustUse { MustUse {} }
393
+ # }
394
+ #
395
+ fn main () {
396
+ // Violates the `unused_must_use` lint.
397
+ MustUse :: new ();
398
+ }
399
+ ```
400
+
401
+ When used on a function, if the [ expression] of an
402
+ [ expression statement] is a [ call expression] to that function, then the
403
+ ` unused_must_use ` lint is violated. The exceptions to this is if the return type
404
+ of the function is ` () ` , ` ! ` , or a [ zero-variant enum] , in which case the
405
+ attribute does nothing.
406
+
407
+ ``` rust
408
+ #[must_use]
409
+ fn five () -> i32 { 5i32 }
410
+
411
+ fn main () {
412
+ // Violates the unused_must_use lint.
413
+ five ();
414
+ }
415
+ ```
416
+
417
+ When used on a function in a trait declaration, then the behavior also applies
418
+ when the call expression is a function from an implementation of the trait.
419
+
420
+ ``` rust
421
+ trait Trait {
422
+ #[must_use]
423
+ fn use_me (& self ) -> i32 ;
424
+ }
425
+
426
+ impl Trait for i32 {
427
+ fn use_me (& self ) -> i32 { 0i32 }
428
+ }
429
+
430
+ fn main () {
431
+ // Violates the `unused_must_use` lint.
432
+ 5i32 . use_me ();
433
+ }
434
+ ```
435
+
436
+ When used on a function in an implementation, the attribute does nothing.
437
+
438
+ > Note: Trivial no-op expressions containing the value will not violate the
439
+ > lint. Examples include wrapping the value in a type that does not implement
440
+ > [ ` Drop ` ] and then not using that type and being the final expression of a
441
+ > [ block expression] that is not used.
442
+ >
443
+ > ``` rust
444
+ > #[must_use]
445
+ > fn five () -> i32 { 5i32 }
446
+ >
447
+ > fn main () {
448
+ > // None of these violate the unused_must_use lint.
449
+ > (five (),);
450
+ > Some (five ());
451
+ > { five () };
452
+ > if true { five () } else { 0i32 };
453
+ > match true {
454
+ > _ => five ()
455
+ > };
456
+ > }
457
+ > ```
458
+
459
+ > Note : It is idiomatic to use a [let statement] with a pattern of `_`
460
+ > when a must- used value is purposely discarded.
461
+ >
462
+ > ```rust
463
+ > #[must_use]
464
+ > fn five() -> i32 { 5i32 }
465
+ >
466
+ > fn main() {
467
+ > // Does not violate the unused_must_use lint.
468
+ > let _ = five();
469
+ > }
470
+ > ```
471
+
472
+ The `must_use ` attribute may also include a message by using
473
+ `#[must_use = " message" ]`. The message will be given alongside the warning .
474
+
379
475
### Inline attribute
380
476
381
477
The inline attribute suggests that the compiler should place a copy of
@@ -430,4 +526,15 @@ You can implement `derive` for your own type through [procedural macros].
430
526
[ Doc comments ] : comments.html#doc-comments
431
527
[ The Rustdoc Book ] : ../rustdoc/the-doc-attribute.html
432
528
[ procedural macros ] : procedural-macros.html
529
+ [ struct ] : items/structs.html
530
+ [ enum ] : items/enumerations.html
531
+ [ union ] : items/unions.html
532
+ [ functions ] : items/functions.html
533
+ [ expression ] : expressions.html
534
+ [ expression statement ] : statements.html#expression-statements
535
+ [ call expression ] : expressions/call-expr.html
536
+ [ block expression ] : expressions/block-expr.html
537
+ [ `Drop` ] : special-types-and-traits.html#drop
538
+ [ let statement ] : statements.html#let-statements
433
539
[ unstable book plugin ] : ../unstable-book/language-features/plugin.html#lint-plugins
540
+ [ zero-variant enum ] : enumerations.html#zero-variant-enums
0 commit comments