about summary refs log tree commit diff
path: root/src/libcore/iter
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-04-04 11:07:16 +0200
committerGitHub <noreply@github.com>2018-04-04 11:07:16 +0200
commit6c8b8091fc73f5cec88d838d32fa165f54ac4ead (patch)
treeb31e0adf063c09a9ebe9a17ecf25c41c8689d7c6 /src/libcore/iter
parentf4f13883e5f888d36681b4390bc07fdb1e4eb7e0 (diff)
parentfb7deda27419eae61da3cbf5a5b1b4f51ae16d04 (diff)
Rollup merge of #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 https://github.com/rust-lang/rust/issues/48926
Diffstat (limited to 'src/libcore/iter')
-rw-r--r--src/libcore/iter/iterator.rs1
1 files changed, 1 insertions, 0 deletions
diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs
index f039d1730eb..75138f31a9d 100644
--- a/src/libcore/iter/iterator.rs
+++ b/src/libcore/iter/iterator.rs
@@ -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)
     }