Skip to content

Commit 5168289

Browse files
authored
Rollup merge of rust-lang#56677 - aelred:must-use-on-traits, r=estebank
#[must_use] on traits in stdlib Based on rust-lang#55506. Adds `#[must_use]` attribute to traits in the stdlib: - `Iterator` - `Future` - `FnOnce` - `Fn` - `FnMut` There may be other traits that should have the attribute, but I couldn't find/think of any.
2 parents 73236ad + 3246f49 commit 5168289

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/libcore/future/future.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use task::{Poll, LocalWaker};
3333
///
3434
/// When using a future, you generally won't call `poll` directly, but instead
3535
/// `await!` the value.
36+
#[must_use]
3637
pub trait Future {
3738
/// The result of the `Future`.
3839
type Output;

src/libcore/iter/iterator.rs

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item=()>) {}
9898
message="`{Self}` is not an iterator"
9999
)]
100100
#[doc(spotlight)]
101+
#[must_use]
101102
pub trait Iterator {
102103
/// The type of the elements being iterated over.
103104
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/ops/function.rs

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
label="expected an `Fn<{Args}>` closure, found `{Self}`",
7373
)]
7474
#[fundamental] // so that regex can rely that `&str: !FnMut`
75+
#[must_use]
7576
pub trait Fn<Args> : FnMut<Args> {
7677
/// Performs the call operation.
7778
#[unstable(feature = "fn_traits", issue = "29625")]
@@ -150,6 +151,7 @@ pub trait Fn<Args> : FnMut<Args> {
150151
label="expected an `FnMut<{Args}>` closure, found `{Self}`",
151152
)]
152153
#[fundamental] // so that regex can rely that `&str: !FnMut`
154+
#[must_use]
153155
pub trait FnMut<Args> : FnOnce<Args> {
154156
/// Performs the call operation.
155157
#[unstable(feature = "fn_traits", issue = "29625")]
@@ -228,6 +230,7 @@ pub trait FnMut<Args> : FnOnce<Args> {
228230
label="expected an `FnOnce<{Args}>` closure, found `{Self}`",
229231
)]
230232
#[fundamental] // so that regex can rely that `&str: !FnMut`
233+
#[must_use]
231234
pub trait FnOnce<Args> {
232235
/// The returned type after the call operator is used.
233236
#[stable(feature = "fn_once_output", since = "1.12.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![deny(unused_must_use)]
2+
#![feature(futures_api, pin, arbitrary_self_types)]
3+
4+
use std::iter::Iterator;
5+
use std::future::Future;
6+
7+
use std::task::{Poll, LocalWaker};
8+
use std::pin::Pin;
9+
use std::unimplemented;
10+
11+
struct MyFuture;
12+
13+
impl Future for MyFuture {
14+
type Output = u32;
15+
16+
fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<u32> {
17+
Poll::Pending
18+
}
19+
}
20+
21+
fn iterator() -> impl Iterator {
22+
std::iter::empty::<u32>()
23+
}
24+
25+
fn future() -> impl Future {
26+
MyFuture
27+
}
28+
29+
fn square_fn_once() -> impl FnOnce(u32) -> u32 {
30+
|x| x * x
31+
}
32+
33+
fn square_fn_mut() -> impl FnMut(u32) -> u32 {
34+
|x| x * x
35+
}
36+
37+
fn square_fn() -> impl Fn(u32) -> u32 {
38+
|x| x * x
39+
}
40+
41+
fn main() {
42+
iterator(); //~ ERROR unused implementer of `std::iter::Iterator` that must be used
43+
future(); //~ ERROR unused implementer of `std::future::Future` that must be used
44+
square_fn_once(); //~ ERROR unused implementer of `std::ops::FnOnce` that must be used
45+
square_fn_mut(); //~ ERROR unused implementer of `std::ops::FnMut` that must be used
46+
square_fn(); //~ ERROR unused implementer of `std::ops::Fn` that must be used
47+
}

0 commit comments

Comments
 (0)