Skip to content

Commit 0aa3f5e

Browse files
committed
add must_use to extract_if methods
1 parent 67c4cf3 commit 0aa3f5e

File tree

7 files changed

+25
-5
lines changed

7 files changed

+25
-5
lines changed

library/alloc/src/collections/btree/map.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,8 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14341434
///
14351435
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
14361436
/// or the iteration short-circuits, then the remaining elements will be retained.
1437-
/// Use [`retain`] with a negated predicate if you do not need the returned iterator.
1437+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
1438+
/// or [`retain`] with a negated predicate if you also do not need to restrict the range.
14381439
///
14391440
/// [`retain`]: BTreeMap::retain
14401441
///
@@ -1458,6 +1459,8 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14581459
/// assert_eq!(high.keys().copied().collect::<Vec<_>>(), [4, 5, 6, 7]);
14591460
/// ```
14601461
#[stable(feature = "btree_extract_if", since = "1.91.0")]
1462+
#[must_use = "this iterator is lazy and does nothing unless consumed; \
1463+
use `retain` or `extract_if().for_each(drop)` to remove and discard elements"]
14611464
pub fn extract_if<F, R>(&mut self, range: R, pred: F) -> ExtractIf<'_, K, V, R, F, A>
14621465
where
14631466
K: Ord,

library/alloc/src/collections/btree/set.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,8 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
11891189
///
11901190
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
11911191
/// or the iteration short-circuits, then the remaining elements will be retained.
1192-
/// Use [`retain`] with a negated predicate if you do not need the returned iterator.
1192+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
1193+
/// or [`retain`] with a negated predicate if you also do not need to restrict the range.
11931194
///
11941195
/// [`retain`]: BTreeSet::retain
11951196
/// # Examples
@@ -1212,6 +1213,8 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
12121213
/// assert_eq!(high.into_iter().collect::<Vec<_>>(), [4, 5, 6, 7]);
12131214
/// ```
12141215
#[stable(feature = "btree_extract_if", since = "1.91.0")]
1216+
#[must_use = "this iterator is lazy and does nothing unless consumed; \
1217+
use `retain` or `extract_if().for_each(drop)` to remove and discard elements"]
12151218
pub fn extract_if<F, R>(&mut self, range: R, pred: F) -> ExtractIf<'_, T, R, F, A>
12161219
where
12171220
T: Ord,

library/alloc/src/collections/linked_list.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,9 @@ impl<T, A: Allocator> LinkedList<T, A> {
11411141
///
11421142
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
11431143
/// or the iteration short-circuits, then the remaining elements will be retained.
1144-
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator.
1144+
/// Use [`retain`] with a negated predicate if you do not need the returned iterator.
1145+
///
1146+
/// [`retain`]: LinkedList::retain
11451147
///
11461148
/// The iterator also lets you mutate the value of each element in the
11471149
/// closure, regardless of whether you choose to keep or remove it.
@@ -1163,6 +1165,8 @@ impl<T, A: Allocator> LinkedList<T, A> {
11631165
/// assert_eq!(odds.into_iter().collect::<Vec<_>>(), vec![1, 3, 5, 9, 11, 13, 15]);
11641166
/// ```
11651167
#[stable(feature = "extract_if", since = "1.87.0")]
1168+
#[must_use = "this iterator is lazy and does nothing unless consumed; \
1169+
use `retain` to remove and discard elements"]
11661170
pub fn extract_if<F>(&mut self, filter: F) -> ExtractIf<'_, T, F, A>
11671171
where
11681172
F: FnMut(&mut T) -> bool,

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
676676
///
677677
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
678678
/// or the iteration short-circuits, then the remaining elements will be retained.
679-
/// Use [`retain_mut`] with a negated predicate if you do not need the returned iterator.
679+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
680+
/// or [`retain_mut`] with a negated predicate if you also do not need to restrict the range.
680681
///
681682
/// [`retain_mut`]: VecDeque::retain_mut
682683
///
@@ -747,6 +748,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
747748
/// assert_eq!(ones.len(), 3);
748749
/// ```
749750
#[unstable(feature = "vec_deque_extract_if", issue = "147750")]
751+
#[must_use = "this iterator is lazy and does nothing unless consumed; \
752+
use `retain_mut` or `extract_if().for_each(drop)` to remove and discard elements"]
750753
pub fn extract_if<F, R>(&mut self, range: R, filter: F) -> ExtractIf<'_, T, F, A>
751754
where
752755
F: FnMut(&mut T) -> bool,

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3933,7 +3933,8 @@ impl<T, A: Allocator> Vec<T, A> {
39333933
///
39343934
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
39353935
/// or the iteration short-circuits, then the remaining elements will be retained.
3936-
/// Use [`retain_mut`] with a negated predicate if you do not need the returned iterator.
3936+
/// Use `extract_if().for_each(drop)` if you do not need the returned iterator,
3937+
/// or [`retain_mut`] with a negated predicate if you also do not need to restrict the range.
39373938
///
39383939
/// [`retain_mut`]: Vec::retain_mut
39393940
///
@@ -3996,6 +3997,8 @@ impl<T, A: Allocator> Vec<T, A> {
39963997
/// assert_eq!(ones.len(), 3);
39973998
/// ```
39983999
#[stable(feature = "extract_if", since = "1.87.0")]
4000+
#[must_use = "this iterator is lazy and does nothing unless consumed; \
4001+
use `retain_mut` or `extract_if().for_each(drop)` to remove and discard elements"]
39994002
pub fn extract_if<F, R>(&mut self, range: R, filter: F) -> ExtractIf<'_, T, F, A>
40004003
where
40014004
F: FnMut(&mut T) -> bool,

library/std/src/collections/hash/map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,8 @@ impl<K, V, S> HashMap<K, V, S> {
688688
#[inline]
689689
#[rustc_lint_query_instability]
690690
#[stable(feature = "hash_extract_if", since = "1.88.0")]
691+
#[must_use = "this iterator is lazy and does nothing unless consumed; \
692+
use `retain` to remove and discard elements"]
691693
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, K, V, F>
692694
where
693695
F: FnMut(&K, &mut V) -> bool,

library/std/src/collections/hash/set.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ impl<T, S> HashSet<T, S> {
309309
#[inline]
310310
#[rustc_lint_query_instability]
311311
#[stable(feature = "hash_extract_if", since = "1.88.0")]
312+
#[must_use = "this iterator is lazy and does nothing unless consumed; \
313+
use `retain` to remove and discard elements"]
312314
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, T, F>
313315
where
314316
F: FnMut(&T) -> bool,

0 commit comments

Comments
 (0)