Skip to content

Commit 325ba23

Browse files
committed
Auto merge of #44620 - zackmdavis:rfc_1940_housekeeping, r=nikomatsakis
RFC 1940 housekeeping * move test to own directory, as requested in #43302 (comment) * exercise trait methods in test * unstable book section r? @nikomatsakis
2 parents 5d5dcae + d09db63 commit 325ba23

File tree

5 files changed

+130
-53
lines changed

5 files changed

+130
-53
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# `fn_must_use`
2+
3+
The tracking issue for this feature is [#43302].
4+
5+
[#43302]: https://github.com/rust-lang/rust/issues/43302
6+
7+
------------------------
8+
9+
The `fn_must_use` feature allows functions and methods to be annotated with
10+
`#[must_use]`, indicating that the `unused_must_use` lint should require their
11+
return values to be used (similarly to how types annotated with `must_use`,
12+
most notably `Result`, are linted if not used).
13+
14+
## Examples
15+
16+
```rust
17+
#![feature(fn_must_use)]
18+
19+
#[must_use]
20+
fn double(x: i32) -> i32 {
21+
2 * x
22+
}
23+
24+
fn main() {
25+
double(4); // warning: unused return value of `double` which must be used
26+
27+
let _ = double(4); // (no warning)
28+
}
29+
30+
```

src/test/ui/lint/fn_must_use.rs

-35
This file was deleted.

src/test/ui/lint/fn_must_use.stderr

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(fn_must_use)]
12+
#![warn(unused_must_use)]
13+
14+
struct MyStruct {
15+
n: usize,
16+
}
17+
18+
impl MyStruct {
19+
#[must_use]
20+
fn need_to_use_this_method_value(&self) -> usize {
21+
self.n
22+
}
23+
}
24+
25+
trait EvenNature {
26+
#[must_use = "no side effects"]
27+
fn is_even(&self) -> bool;
28+
}
29+
30+
impl EvenNature for MyStruct {
31+
fn is_even(&self) -> bool {
32+
self.n % 2 == 0
33+
}
34+
}
35+
36+
trait Replaceable {
37+
fn replace(&mut self, substitute: usize) -> usize;
38+
}
39+
40+
impl Replaceable for MyStruct {
41+
// ↓ N.b.: `#[must_use]` attribute on a particular trait implementation
42+
// method won't work; the attribute should be on the method signature in
43+
// the trait's definition.
44+
#[must_use]
45+
fn replace(&mut self, substitute: usize) -> usize {
46+
let previously = self.n;
47+
self.n = substitute;
48+
previously
49+
}
50+
}
51+
52+
#[must_use = "it's important"]
53+
fn need_to_use_this_value() -> bool {
54+
false
55+
}
56+
57+
fn main() {
58+
need_to_use_this_value();
59+
60+
let mut m = MyStruct { n: 2 };
61+
m.need_to_use_this_method_value();
62+
m.is_even(); // trait method!
63+
64+
m.replace(3);
65+
66+
2.eq(&3);
67+
68+
// FIXME: operators should probably be `must_use` if underlying method is
69+
2 == 3;
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
warning: unused return value of `need_to_use_this_value` which must be used: it's important
2+
--> $DIR/fn_must_use.rs:58:5
3+
|
4+
58 | need_to_use_this_value();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/fn_must_use.rs:12:9
9+
|
10+
12 | #![warn(unused_must_use)]
11+
| ^^^^^^^^^^^^^^^
12+
13+
warning: unused return value of `MyStruct::need_to_use_this_method_value` which must be used
14+
--> $DIR/fn_must_use.rs:61:5
15+
|
16+
61 | m.need_to_use_this_method_value();
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
warning: unused return value of `EvenNature::is_even` which must be used: no side effects
20+
--> $DIR/fn_must_use.rs:62:5
21+
|
22+
62 | m.is_even(); // trait method!
23+
| ^^^^^^^^^^^^
24+
25+
warning: unused return value of `std::cmp::PartialEq::eq` which must be used
26+
--> $DIR/fn_must_use.rs:66:5
27+
|
28+
66 | 2.eq(&3);
29+
| ^^^^^^^^^
30+

0 commit comments

Comments
 (0)