Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#[must_use] on traits in stdlib #56677

Merged
merged 3 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcore/future/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use task::{Poll, LocalWaker};
///
/// When using a future, you generally won't call `poll` directly, but instead
/// `await!` the value.
#[must_use]
pub trait Future {
/// The result of the `Future`.
type Output;
Expand Down
1 change: 1 addition & 0 deletions src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item=()>) {}
message="`{Self}` is not an iterator"
)]
#[doc(spotlight)]
#[must_use]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

Suggested change
#[must_use]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]

(which can be fixed in a follow up PR)

pub trait Iterator {
/// The type of the elements being iterated over.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/ops/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
label="expected an `Fn<{Args}>` closure, found `{Self}`",
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use]
pub trait Fn<Args> : FnMut<Args> {
/// Performs the call operation.
#[unstable(feature = "fn_traits", issue = "29625")]
Expand Down Expand Up @@ -150,6 +151,7 @@ pub trait Fn<Args> : FnMut<Args> {
label="expected an `FnMut<{Args}>` closure, found `{Self}`",
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use]
pub trait FnMut<Args> : FnOnce<Args> {
/// Performs the call operation.
#[unstable(feature = "fn_traits", issue = "29625")]
Expand Down Expand Up @@ -228,6 +230,7 @@ pub trait FnMut<Args> : FnOnce<Args> {
label="expected an `FnOnce<{Args}>` closure, found `{Self}`",
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use]
pub trait FnOnce<Args> {
/// The returned type after the call operator is used.
#[stable(feature = "fn_once_output", since = "1.12.0")]
Expand Down
47 changes: 47 additions & 0 deletions src/test/compile-fail/must_use-in-stdlib-traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![deny(unused_must_use)]
#![feature(futures_api, pin, arbitrary_self_types)]

use std::iter::Iterator;
use std::future::Future;

use std::task::{Poll, LocalWaker};
use std::pin::Pin;
use std::unimplemented;

struct MyFuture;

impl Future for MyFuture {
type Output = u32;

fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<u32> {
Poll::Pending
}
}

fn iterator() -> impl Iterator {
std::iter::empty::<u32>()
}

fn future() -> impl Future {
MyFuture
}

fn square_fn_once() -> impl FnOnce(u32) -> u32 {
|x| x * x
}

fn square_fn_mut() -> impl FnMut(u32) -> u32 {
|x| x * x
}

fn square_fn() -> impl Fn(u32) -> u32 {
|x| x * x
}

fn main() {
iterator(); //~ ERROR unused implementer of `std::iter::Iterator` that must be used
future(); //~ ERROR unused implementer of `std::future::Future` that must be used
square_fn_once(); //~ ERROR unused implementer of `std::ops::FnOnce` that must be used
square_fn_mut(); //~ ERROR unused implementer of `std::ops::FnMut` that must be used
square_fn(); //~ ERROR unused implementer of `std::ops::Fn` that must be used
}