Skip to content

Commit 407d714

Browse files
committed
Document RFC 1940 (must_use on fns)
1 parent 7fe4252 commit 407d714

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

src/attributes.md

Lines changed: 89 additions & 6 deletions
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,82 @@ 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.
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+
379452
### Inline attribute
380453
381454
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].
430503
[Doc comments]: comments.html#doc-comments
431504
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
432505
[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

src/type-layout.md

Lines changed: 1 addition & 1 deletion
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)