Skip to content

Commit f76be08

Browse files
authored
tidy things up a bit
1 parent 4d9204e commit f76be08

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
# When to add `#[must_use]`
22

3-
The `#[must_use]` attribute can be applied to types or functions where failing to explicitly consider them or their output is almost certainly a bug.
3+
The `#[must_use]` attribute can be applied to types or functions when failing to explicitly consider them or their output is almost certainly a bug.
44

55
As an example, `Result` is `#[must_use]` because failing to consider it may indicate a caller didn't realise a method was fallible:
66

77
```rust
8-
// If `check_status` returns a `Result`, we might assume this
9-
// call was infallible
8+
// Is `check_status` infallible? Or did we forget to look at its `Result`?
109
check_status();
1110
```
1211

13-
Operators like `saturating_add` are also `#[must_use]` because failing to consider its output might indicate a caller didn't realise they don't mutate the left hand side:
12+
Operators like `saturating_add` are also `#[must_use]` because failing to consider their output might indicate a caller didn't realise they don't mutate the left-hand-side:
1413

1514
```rust
16-
let a = 42;
17-
let b = 13;
18-
1915
// A caller might assume this method mutates `a`
2016
a.saturating_add(b);
2117
```
2218

23-
Combinators produced by the `Iterator` trait are `#[must_use]` because failing to consider them might indicate a caller didn't realize `Iterator`s are lazy and won't actually do anything unless you drive them:
19+
Combinators produced by the `Iterator` trait are `#[must_use]` because failing to use them might indicate a caller didn't realize `Iterator`s are lazy and won't actually do anything unless you drive them:
2420

2521
```rust
2622
// A caller might not realise this code won't do anything
2723
// unless they call `collect`, `count`, etc.
28-
slice.iter().filter(|v| v > 10).map(|v| v + 2);
24+
v.iter().map(|x| println!("{}", x));
2925
```
3026

31-
On the other hand, `thread::JoinHandle` isn't `#[must_use]` because spawning and forgetting background work is a legitimate pattern and forcing callers to ignore it is less likely to catch bugs:
27+
On the other hand, `thread::JoinHandle` isn't `#[must_use]` because spawning fire-and-forget work is a legitimate pattern and forcing callers to explicitly ignore handles could be a nuisance rather than an indication of a bug:
3228

3329
```rust
3430
thread::spawn(|| {
@@ -38,4 +34,6 @@ thread::spawn(|| {
3834

3935
## For reviewers
4036

37+
Look for any legitimate use-cases where `#[must_use]` will cause callers to explicitly ignore values. If these are common then `#[must_use]` probably isn't appropriate.
38+
4139
The `#[must_use]` attribute only produces warnings, so it can technically be introduced at any time. To avoid accumulating nuisance warnings though ping `@rust-lang/libs` for input before adding new `#[must_use]` attributes to existing types and functions.

0 commit comments

Comments
 (0)