Skip to content

Commit b6d4ed7

Browse files
authored
Merge pull request #247 from Havvy/must-use
Document RFC 1940 (must_use on fns)
2 parents abc7493 + f376333 commit b6d4ed7

File tree

2 files changed

+113
-6
lines changed

2 files changed

+113
-6
lines changed

src/attributes.md

+112-5
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ macro scope.
166166
object file that this item's contents will be placed into.
167167
- `no_mangle` - on any item, do not apply the standard name mangling. Set the
168168
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.
172169

173170
### Deprecation
174171

@@ -325,7 +322,7 @@ The lint checks supported by the compiler can be found via `rustc -W help`,
325322
along with their default settings. [Compiler
326323
plugins][unstable book plugin] can provide additional lint checks.
327324

328-
```rust,ignore
325+
```rust
329326
pub mod m1 {
330327
// Missing documentation is ignored here
331328
#[allow(missing_docs)]
@@ -366,7 +363,7 @@ pub mod m2{
366363
This example shows how one can use `forbid` to disallow uses of `allow` for
367364
that lint check:
368365

369-
```rust,ignore
366+
```rust,compile_fail
370367
#[forbid(missing_docs)]
371368
pub mod m3 {
372369
// Attempting to toggle warning signals an error here
@@ -376,6 +373,105 @@ pub mod m3 {
376373
}
377374
```
378375

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+
379475
### Inline attribute
380476
381477
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].
430526
[Doc comments]: comments.html#doc-comments
431527
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
432528
[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
433539
[unstable book plugin]: ../unstable-book/language-features/plugin.html#lint-plugins
540+
[zero-variant enum]: enumerations.html#zero-variant-enums

src/type-layout.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Closures have no layout guarantees.
106106

107107
## Representations
108108

109-
All user-defined composite types (`struct`s, `enum`, and `union`s) have a
109+
All user-defined composite types (`struct`s, `enum`s, and `union`s) have a
110110
*representation* that specifies what the layout is for the type.
111111

112112
The possible representations for a type are the default representation, `C`, the

0 commit comments

Comments
 (0)