Skip to content
Permalink
Browse files

Rollup merge of rust-lang#49533 - scottmcm:more-must-use, r=nikomatsakis

Add #[must_use] to a few standard library methods

Chosen to start a precedent of using it on ones that are potentially-expensive and where using it for side effects is particularly discouraged.

Discuss :)

```rust
warning: unused return value of `std::iter::Iterator::collect` which must be used: if you really need to exhaust the iterator, consider `.for_each(drop)` instead
  --> $DIR/fn_must_use_stdlib.rs:19:5
   |
LL |     "1 2 3".split_whitespace().collect::<Vec<_>>();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused return value of `std::borrow::ToOwned::to_owned` which must be used: cloning is often expensive and is not expected to have side effects
  --> $DIR/fn_must_use_stdlib.rs:21:5
   |
LL |     "hello".to_owned();
   |     ^^^^^^^^^^^^^^^^^^^

warning: unused return value of `std::clone::Clone::clone` which must be used: cloning is often expensive and is not expected to have side effects
  --> $DIR/fn_must_use_stdlib.rs:23:5
   |
LL |     String::from("world").clone();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

cc rust-lang#48926
  • Loading branch information...
kennytm committed Apr 4, 2018
2 parents f4f1388 + fb7deda commit 6c8b8091fc73f5cec88d838d32fa165f54ac4ead
Showing with 4 additions and 1 deletion.
  1. +1 −0 src/liballoc/borrow.rs
  2. +1 −0 src/libcore/clone.rs
  3. +1 −0 src/libcore/iter/iterator.rs
  4. +1 −1 src/librustc_mir/build/mod.rs
@@ -59,6 +59,7 @@ pub trait ToOwned {
/// let vv: Vec<i32> = v.to_owned();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "cloning is often expensive and is not expected to have side effects"]
fn to_owned(&self) -> Self::Owned;

/// Uses borrowed data to replace owned data, usually by cloning.
@@ -105,6 +105,7 @@ pub trait Clone : Sized {
/// assert_eq!("Hello", hello.clone());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "cloning is often expensive and is not expected to have side effects"]
fn clone(&self) -> Self;

/// Performs copy-assignment from `source`.
@@ -1368,6 +1368,7 @@ pub trait Iterator {
/// [`Result`]: ../../std/result/enum.Result.html
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
fn collect<B: FromIterator<Self::Item>>(self) -> B where Self: Sized {
FromIterator::from_iter(self)
}
@@ -317,7 +317,7 @@ newtype_index!(ScopeId);
/// macro (and methods below) makes working with `BlockAnd` much more
/// convenient.

#[must_use] // if you don't use one of these results, you're leaving a dangling edge
#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
struct BlockAnd<T>(BasicBlock, T);

trait BlockAndExtension {

0 comments on commit 6c8b809

Please sign in to comment.
You can’t perform that action at this time.