Skip to content

Commit 911cda8

Browse files
authored
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
2 parents bf80935 + fb7deda commit 911cda8

File tree

4 files changed

+4
-1
lines changed

4 files changed

+4
-1
lines changed

src/liballoc/borrow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub trait ToOwned {
5959
/// let vv: Vec<i32> = v.to_owned();
6060
/// ```
6161
#[stable(feature = "rust1", since = "1.0.0")]
62+
#[must_use = "cloning is often expensive and is not expected to have side effects"]
6263
fn to_owned(&self) -> Self::Owned;
6364

6465
/// Uses borrowed data to replace owned data, usually by cloning.

src/libcore/clone.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub trait Clone : Sized {
105105
/// assert_eq!("Hello", hello.clone());
106106
/// ```
107107
#[stable(feature = "rust1", since = "1.0.0")]
108+
#[must_use = "cloning is often expensive and is not expected to have side effects"]
108109
fn clone(&self) -> Self;
109110

110111
/// Performs copy-assignment from `source`.

src/libcore/iter/iterator.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ pub trait Iterator {
13681368
/// [`Result`]: ../../std/result/enum.Result.html
13691369
#[inline]
13701370
#[stable(feature = "rust1", since = "1.0.0")]
1371+
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
13711372
fn collect<B: FromIterator<Self::Item>>(self) -> B where Self: Sized {
13721373
FromIterator::from_iter(self)
13731374
}

src/librustc_mir/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ newtype_index!(ScopeId);
317317
/// macro (and methods below) makes working with `BlockAnd` much more
318318
/// convenient.
319319
320-
#[must_use] // if you don't use one of these results, you're leaving a dangling edge
320+
#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
321321
struct BlockAnd<T>(BasicBlock, T);
322322

323323
trait BlockAndExtension {

0 commit comments

Comments
 (0)