@@ -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,82 @@ 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.
404
+
405
+ ``` rust
406
+ #[must_use]
407
+ fn five () -> i32 { 5i32 }
408
+
409
+ fn main () {
410
+ // Violates the unused_must_use lint.
411
+ five ();
412
+ }
413
+ ```
414
+
415
+ > Note: Trivial no-op expressions containing the value will not violate the
416
+ > lint. Examples include wrapping the value in a type that does not implement
417
+ > [ ` Drop ` ] and then not using that type and being the final expression of a
418
+ > [ block expression] that is not used.
419
+ >
420
+ > ``` rust
421
+ > #[must_use]
422
+ > fn five () -> i32 { 5i32 }
423
+ >
424
+ > fn main () {
425
+ > // None of these violate the unused_must_use lint.
426
+ > (five (),);
427
+ > Some (five ());
428
+ > { five () };
429
+ > if true { five () } else { 0i32 };
430
+ > match true {
431
+ > _ => five ()
432
+ > };
433
+ > }
434
+ > ```
435
+
436
+ > Note : It is idiomatic to use a [let statement] with a pattern of `_`
437
+ > when a must- used value is purposely discarded.
438
+ >
439
+ > ```rust
440
+ > #[must_use]
441
+ > fn five() -> i32 { 5i32 }
442
+ >
443
+ > fn main() {
444
+ > // Does not violate the unused_must_use lint.
445
+ > let _ = five();
446
+ > }
447
+ > ```
448
+
449
+ The `must_use ` attribute may also include a message by using
450
+ `#[must_use = " message" ]`. The message will be given alongside the warning .
451
+
379
452
### Inline attribute
380
453
381
454
The inline attribute suggests that the compiler should place a copy of
@@ -430,4 +503,14 @@ You can implement `derive` for your own type through [procedural macros].
430
503
[ Doc comments ] : comments.html#doc-comments
431
504
[ The Rustdoc Book ] : ../rustdoc/the-doc-attribute.html
432
505
[ procedural macros ] : procedural-macros.html
433
- [ unstable book plugin ] : ../unstable-book/language-features/plugin.html#lint-plugins
506
+ [ struct ] : items/structs.html
507
+ [ enum ] : items/enumerations.html
508
+ [ union ] : items/unions.html
509
+ [ functions ] : items/functions.html
510
+ [ expression ] : expressions.html
511
+ [ expression statement ] : statements.html#expression-statements
512
+ [ call expression ] : expressions/call-expr.html
513
+ [ block expression ] : expressions/block-expr.html
514
+ [ `Drop` ] : special-types-and-traits.html#drop
515
+ [ let statement ] : statements.html#let-statements
516
+ [ unstable book plugin ] : ../unstable-book/language-features/plugin.html#lint-plugins
0 commit comments