diff --git a/library/alloc/benches/btree/map.rs b/library/alloc/benches/btree/map.rs index c304f74884721..89c21929dbcda 100644 --- a/library/alloc/benches/btree/map.rs +++ b/library/alloc/benches/btree/map.rs @@ -290,7 +290,7 @@ where let mut c = 0; for i in 0..BENCH_RANGE_SIZE { for j in i + 1..BENCH_RANGE_SIZE { - black_box(map.range(f(i, j))); + let _ = black_box(map.range(f(i, j))); c += 1; } } @@ -322,7 +322,7 @@ fn bench_iter(b: &mut Bencher, repeats: i32, size: i32) { let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect(); b.iter(|| { for _ in 0..repeats { - black_box(map.iter()); + let _ = black_box(map.iter()); } }); } diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index 9bded6c0f1cf2..a73eeac611584 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -512,6 +512,7 @@ impl BinaryHeap { /// let vec = heap.into_sorted_vec(); /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]); /// ``` + #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "binary_heap_extras_15", since = "1.5.0")] pub fn into_sorted_vec(mut self) -> Vec { let mut end = self.len(); @@ -850,7 +851,6 @@ impl BinaryHeap { /// /// assert_eq!(heap.into_iter_sorted().take(2).collect::>(), vec![5, 4]); /// ``` - #[must_use = "`self` will be dropped if the result is not used"] #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")] pub fn into_iter_sorted(self) -> IntoIterSorted { IntoIterSorted { inner: self } @@ -877,6 +877,7 @@ impl BinaryHeap { /// # Time complexity /// /// Cost is *O*(1) in the worst case. + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn peek(&self) -> Option<&T> { self.data.get(0) @@ -894,6 +895,7 @@ impl BinaryHeap { /// assert!(heap.capacity() >= 100); /// heap.push(4); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn capacity(&self) -> usize { self.data.capacity() @@ -1203,6 +1205,7 @@ impl Drop for Hole<'_, T> { /// documentation for more. /// /// [`iter`]: BinaryHeap::iter +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, T: 'a> { iter: slice::Iter<'a, T>, @@ -1337,6 +1340,7 @@ impl AsIntoIter for IntoIter { } } +#[must_use = "iterators are lazy and do nothing unless consumed"] #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")] #[derive(Clone, Debug)] pub struct IntoIterSorted { diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index fa86e611565e6..3a77cdeac6d09 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -288,6 +288,7 @@ where /// documentation for more. /// /// [`iter`]: BTreeMap::iter +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, K: 'a, V: 'a> { range: LazyLeafRange, K, V>, @@ -316,6 +317,7 @@ pub struct IterMut<'a, K: 'a, V: 'a> { _marker: PhantomData<&'a mut (K, V)>, } +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "collection_debug", since = "1.17.0")] impl fmt::Debug for IterMut<'_, K, V> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -359,6 +361,7 @@ impl fmt::Debug for IntoIter { /// documentation for more. /// /// [`keys`]: BTreeMap::keys +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Keys<'a, K: 'a, V: 'a> { inner: Iter<'a, K, V>, @@ -377,6 +380,7 @@ impl fmt::Debug for Keys<'_, K, V> { /// documentation for more. /// /// [`values`]: BTreeMap::values +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Values<'a, K: 'a, V: 'a> { inner: Iter<'a, K, V>, @@ -395,6 +399,7 @@ impl fmt::Debug for Values<'_, K, V> { /// documentation for more. /// /// [`values_mut`]: BTreeMap::values_mut +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "map_values_mut", since = "1.10.0")] pub struct ValuesMut<'a, K: 'a, V: 'a> { inner: IterMut<'a, K, V>, @@ -413,6 +418,7 @@ impl fmt::Debug for ValuesMut<'_, K, V> { /// See its documentation for more. /// /// [`into_keys`]: BTreeMap::into_keys +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "map_into_keys_values", since = "1.54.0")] pub struct IntoKeys { inner: IntoIter, @@ -431,6 +437,7 @@ impl fmt::Debug for IntoKeys { /// See its documentation for more. /// /// [`into_values`]: BTreeMap::into_values +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "map_into_keys_values", since = "1.54.0")] pub struct IntoValues { inner: IntoIter, @@ -449,6 +456,7 @@ impl fmt::Debug for IntoValues { /// documentation for more. /// /// [`range`]: BTreeMap::range +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "btree_range", since = "1.17.0")] pub struct Range<'a, K: 'a, V: 'a> { inner: LeafRange, K, V>, @@ -467,6 +475,7 @@ impl fmt::Debug for Range<'_, K, V> { /// documentation for more. /// /// [`range_mut`]: BTreeMap::range_mut +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "btree_range", since = "1.17.0")] pub struct RangeMut<'a, K: 'a, V: 'a> { inner: LeafRange, K, V>, @@ -1265,7 +1274,6 @@ impl BTreeMap { /// assert_eq!(keys, [1, 2]); /// ``` #[inline] - #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "map_into_keys_values", since = "1.54.0")] pub fn into_keys(self) -> IntoKeys { IntoKeys { inner: self.into_iter() } @@ -1288,7 +1296,6 @@ impl BTreeMap { /// assert_eq!(values, ["hello", "goodbye"]); /// ``` #[inline] - #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "map_into_keys_values", since = "1.54.0")] pub fn into_values(self) -> IntoValues { IntoValues { inner: self.into_iter() } diff --git a/library/alloc/src/collections/btree/map/entry.rs b/library/alloc/src/collections/btree/map/entry.rs index 3e9048b17688f..5cef007a46f0d 100644 --- a/library/alloc/src/collections/btree/map/entry.rs +++ b/library/alloc/src/collections/btree/map/entry.rs @@ -347,6 +347,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { /// map.entry("poneyland").or_insert(12); /// assert_eq!(map.entry("poneyland").key(), &"poneyland"); /// ``` + #[must_use] #[stable(feature = "map_entry_keys", since = "1.10.0")] pub fn key(&self) -> &K { self.handle.reborrow().into_kv().0 @@ -391,6 +392,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { /// assert_eq!(o.get(), &12); /// } /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn get(&self) -> &V { self.handle.reborrow().into_kv().1 diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index 173960341f859..17389657afb92 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -744,35 +744,35 @@ fn test_range_equal_empty_cases() { #[should_panic] fn test_range_equal_excluded() { let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); - map.range((Excluded(2), Excluded(2))); + let _ = map.range((Excluded(2), Excluded(2))); } #[test] #[should_panic] fn test_range_backwards_1() { let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); - map.range((Included(3), Included(2))); + let _ = map.range((Included(3), Included(2))); } #[test] #[should_panic] fn test_range_backwards_2() { let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); - map.range((Included(3), Excluded(2))); + let _ = map.range((Included(3), Excluded(2))); } #[test] #[should_panic] fn test_range_backwards_3() { let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); - map.range((Excluded(3), Included(2))); + let _ = map.range((Excluded(3), Included(2))); } #[test] #[should_panic] fn test_range_backwards_4() { let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); - map.range((Excluded(3), Excluded(2))); + let _ = map.range((Excluded(3), Excluded(2))); } #[test] @@ -783,7 +783,7 @@ fn test_range_finding_ill_order_in_map() { // we cause a different panic than `test_range_backwards_1` does. // A more refined `should_panic` would be welcome. if Cyclic3::C < Cyclic3::A { - map.range(Cyclic3::C..=Cyclic3::A); + let _ = map.range(Cyclic3::C..=Cyclic3::A); } } @@ -824,7 +824,7 @@ fn test_range_finding_ill_order_in_range_ord() { } let map = (0..12).map(|i| (CompositeKey(i, EvilTwin(i)), ())).collect::>(); - map.range(EvilTwin(5)..=EvilTwin(7)); + let _ = map.range(EvilTwin(5)..=EvilTwin(7)); } #[test] @@ -1239,32 +1239,32 @@ fn test_borrow() { #[allow(dead_code)] fn get(v: &BTreeMap, ()>, t: &T) { - v.get(t); + let _ = v.get(t); } #[allow(dead_code)] fn get_mut(v: &mut BTreeMap, ()>, t: &T) { - v.get_mut(t); + let _ = v.get_mut(t); } #[allow(dead_code)] fn get_key_value(v: &BTreeMap, ()>, t: &T) { - v.get_key_value(t); + let _ = v.get_key_value(t); } #[allow(dead_code)] fn contains_key(v: &BTreeMap, ()>, t: &T) { - v.contains_key(t); + let _ = v.contains_key(t); } #[allow(dead_code)] fn range(v: &BTreeMap, ()>, t: T) { - v.range(t..); + let _ = v.range(t..); } #[allow(dead_code)] fn range_mut(v: &mut BTreeMap, ()>, t: T) { - v.range_mut(t..); + let _ = v.range_mut(t..); } #[allow(dead_code)] diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index d732f65b0d05f..b112cb6e41839 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -92,6 +92,7 @@ impl Clone for BTreeSet { /// See its documentation for more. /// /// [`iter`]: BTreeSet::iter +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, T: 'a> { iter: Keys<'a, T, ()>, @@ -123,6 +124,7 @@ pub struct IntoIter { /// See its documentation for more. /// /// [`range`]: BTreeSet::range +#[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Debug)] #[stable(feature = "btree_range", since = "1.17.0")] pub struct Range<'a, T: 'a> { @@ -668,6 +670,7 @@ impl BTreeSet { /// set.insert(2); /// assert_eq!(set.first(), Some(&1)); /// ``` + #[must_use] #[unstable(feature = "map_first_last", issue = "62924")] pub fn first(&self) -> Option<&T> where @@ -694,6 +697,7 @@ impl BTreeSet { /// set.insert(2); /// assert_eq!(set.last(), Some(&2)); /// ``` + #[must_use] #[unstable(feature = "map_first_last", issue = "62924")] pub fn last(&self) -> Option<&T> where diff --git a/library/alloc/src/collections/btree/set/tests.rs b/library/alloc/src/collections/btree/set/tests.rs index 0a87ae12d61a5..01cf62b32eccd 100644 --- a/library/alloc/src/collections/btree/set/tests.rs +++ b/library/alloc/src/collections/btree/set/tests.rs @@ -613,8 +613,8 @@ fn test_ord_absence() { set.is_empty(); set.len(); set.clear(); - set.iter(); - set.into_iter(); + let _ = set.iter(); + let _ = set.into_iter(); } fn set_debug(set: BTreeSet) { diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs index ea010c1f89d24..4c74113338774 100644 --- a/library/alloc/src/collections/linked_list.rs +++ b/library/alloc/src/collections/linked_list.rs @@ -64,6 +64,7 @@ struct Node { /// /// This `struct` is created by [`LinkedList::iter()`]. See its /// documentation for more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, T: 'a> { head: Option>>, @@ -99,6 +100,7 @@ impl Clone for Iter<'_, T> { /// /// This `struct` is created by [`LinkedList::iter_mut()`]. See its /// documentation for more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct IterMut<'a, T: 'a> { head: Option>>, @@ -529,6 +531,7 @@ impl LinkedList { /// /// The cursor is pointing to the "ghost" non-element if the list is empty. #[inline] + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn cursor_front(&self) -> Cursor<'_, T> { Cursor { index: 0, current: self.head, list: self } @@ -538,6 +541,7 @@ impl LinkedList { /// /// The cursor is pointing to the "ghost" non-element if the list is empty. #[inline] + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn cursor_front_mut(&mut self) -> CursorMut<'_, T> { CursorMut { index: 0, current: self.head, list: self } @@ -547,6 +551,7 @@ impl LinkedList { /// /// The cursor is pointing to the "ghost" non-element if the list is empty. #[inline] + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn cursor_back(&self) -> Cursor<'_, T> { Cursor { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self } @@ -556,6 +561,7 @@ impl LinkedList { /// /// The cursor is pointing to the "ghost" non-element if the list is empty. #[inline] + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn cursor_back_mut(&mut self) -> CursorMut<'_, T> { CursorMut { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self } @@ -678,6 +684,7 @@ impl LinkedList { /// assert_eq!(dl.front(), Some(&1)); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn front(&self) -> Option<&T> { unsafe { self.head.as_ref().map(|node| &node.as_ref().element) } @@ -706,6 +713,7 @@ impl LinkedList { /// assert_eq!(dl.front(), Some(&5)); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn front_mut(&mut self) -> Option<&mut T> { unsafe { self.head.as_mut().map(|node| &mut node.as_mut().element) } @@ -728,6 +736,7 @@ impl LinkedList { /// assert_eq!(dl.back(), Some(&1)); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn back(&self) -> Option<&T> { unsafe { self.tail.as_ref().map(|node| &node.as_ref().element) } @@ -1178,6 +1187,7 @@ impl<'a, T> Cursor<'a, T> { /// /// This returns `None` if the cursor is currently pointing to the /// "ghost" non-element. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn index(&self) -> Option { let _ = self.current?; @@ -1232,6 +1242,7 @@ impl<'a, T> Cursor<'a, T> { /// /// This returns `None` if the cursor is currently pointing to the /// "ghost" non-element. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn current(&self) -> Option<&'a T> { unsafe { self.current.map(|current| &(*current.as_ptr()).element) } @@ -1242,6 +1253,7 @@ impl<'a, T> Cursor<'a, T> { /// If the cursor is pointing to the "ghost" non-element then this returns /// the first element of the `LinkedList`. If it is pointing to the last /// element of the `LinkedList` then this returns `None`. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn peek_next(&self) -> Option<&'a T> { unsafe { @@ -1258,6 +1270,7 @@ impl<'a, T> Cursor<'a, T> { /// If the cursor is pointing to the "ghost" non-element then this returns /// the last element of the `LinkedList`. If it is pointing to the first /// element of the `LinkedList` then this returns `None`. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn peek_prev(&self) -> Option<&'a T> { unsafe { @@ -1271,6 +1284,7 @@ impl<'a, T> Cursor<'a, T> { /// Provides a reference to the front element of the cursor's parent list, /// or None if the list is empty. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn front(&self) -> Option<&'a T> { self.list.front() @@ -1278,6 +1292,7 @@ impl<'a, T> Cursor<'a, T> { /// Provides a reference to the back element of the cursor's parent list, /// or None if the list is empty. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn back(&self) -> Option<&'a T> { self.list.back() @@ -1289,6 +1304,7 @@ impl<'a, T> CursorMut<'a, T> { /// /// This returns `None` if the cursor is currently pointing to the /// "ghost" non-element. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn index(&self) -> Option { let _ = self.current?; @@ -1343,6 +1359,7 @@ impl<'a, T> CursorMut<'a, T> { /// /// This returns `None` if the cursor is currently pointing to the /// "ghost" non-element. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn current(&mut self) -> Option<&mut T> { unsafe { self.current.map(|current| &mut (*current.as_ptr()).element) } @@ -1631,6 +1648,7 @@ impl<'a, T> CursorMut<'a, T> { /// Provides a reference to the front element of the cursor's parent list, /// or None if the list is empty. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn front(&self) -> Option<&T> { self.list.front() @@ -1638,6 +1656,7 @@ impl<'a, T> CursorMut<'a, T> { /// Provides a mutable reference to the front element of the cursor's /// parent list, or None if the list is empty. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn front_mut(&mut self) -> Option<&mut T> { self.list.front_mut() @@ -1645,6 +1664,7 @@ impl<'a, T> CursorMut<'a, T> { /// Provides a reference to the back element of the cursor's parent list, /// or None if the list is empty. + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn back(&self) -> Option<&T> { self.list.back() @@ -1671,6 +1691,7 @@ impl<'a, T> CursorMut<'a, T> { /// assert_eq!(contents.next(), Some(0)); /// assert_eq!(contents.next(), None); /// ``` + #[must_use] #[unstable(feature = "linked_list_cursors", issue = "58533")] pub fn back_mut(&mut self) -> Option<&mut T> { self.list.back_mut() diff --git a/library/alloc/src/collections/mod.rs b/library/alloc/src/collections/mod.rs index 77d28bdfe6475..1ea135a2aed82 100644 --- a/library/alloc/src/collections/mod.rs +++ b/library/alloc/src/collections/mod.rs @@ -65,6 +65,7 @@ pub struct TryReserveError { impl TryReserveError { /// Details about the allocation that caused the error #[inline] + #[must_use] #[unstable( feature = "try_reserve_kind", reason = "Uncertain how much info should be exposed", diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index 878d8dc5502df..50e789d76b7f3 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -572,6 +572,7 @@ use crate::string; /// [`format_args!`]: core::format_args /// [`format!`]: crate::format #[cfg(not(no_global_oom_handling))] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn format(args: Arguments<'_>) -> string::String { let capacity = args.estimated_capacity(); diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 493cf3117edf6..2950ec421da9e 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -2246,6 +2246,7 @@ impl Weak { /// Gets the number of strong (`Rc`) pointers pointing to this allocation. /// /// If `self` was created using [`Weak::new`], this will return 0. + #[must_use] #[stable(feature = "weak_counts", since = "1.41.0")] pub fn strong_count(&self) -> usize { if let Some(inner) = self.inner() { inner.strong() } else { 0 } @@ -2254,6 +2255,7 @@ impl Weak { /// Gets the number of `Weak` pointers pointing to this allocation. /// /// If no strong pointers remain, this will return zero. + #[must_use] #[stable(feature = "weak_counts", since = "1.41.0")] pub fn weak_count(&self) -> usize { self.inner() @@ -2324,6 +2326,7 @@ impl Weak { /// assert!(!first.ptr_eq(&third)); /// ``` #[inline] + #[must_use] #[stable(feature = "weak_ptr_eq", since = "1.39.0")] pub fn ptr_eq(&self, other: &Self) -> bool { self.ptr.as_ptr() == other.ptr.as_ptr() diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index e1d0ee42f4e90..3b875477df3b8 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -243,6 +243,7 @@ impl str { /// assert_eq!(*boxed_bytes, *s.as_bytes()); /// ``` #[stable(feature = "str_box_extras", since = "1.20.0")] + #[must_use = "`self` will be dropped if the result is not used"] #[inline] pub fn into_boxed_bytes(self: Box) -> Box<[u8]> { self.into() @@ -484,6 +485,7 @@ impl str { /// assert_eq!(boxed_str.into_string(), string); /// ``` #[stable(feature = "box_str", since = "1.4.0")] + #[must_use = "`self` will be dropped if the result is not used"] #[inline] pub fn into_string(self: Box) -> String { let slice = Box::<[u8]>::from(self); @@ -508,9 +510,10 @@ impl str { /// /// ```should_panic /// // this will panic at runtime - /// "0123456789abcdef".repeat(usize::MAX); + /// let huge = "0123456789abcdef".repeat(usize::MAX); /// ``` #[cfg(not(no_global_oom_handling))] + #[must_use] #[stable(feature = "repeat_str", since = "1.16.0")] pub fn repeat(&self, n: usize) -> String { unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) } diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index f479bf231b376..14ebc4dfe8b90 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -898,6 +898,7 @@ impl String { /// assert!(s.capacity() >= 10); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn capacity(&self) -> usize { self.vec.capacity() @@ -1822,6 +1823,7 @@ impl FromUtf8Error { /// // the first byte is invalid here /// assert_eq!(1, error.valid_up_to()); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn utf8_error(&self) -> Utf8Error { self.error diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index b75e9a2f3c71e..039971a176548 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -953,6 +953,7 @@ impl Arc { /// assert_eq!(1, Arc::weak_count(&five)); /// ``` #[inline] + #[must_use] #[stable(feature = "arc_counts", since = "1.15.0")] pub fn weak_count(this: &Self) -> usize { let cnt = this.inner().weak.load(SeqCst); @@ -982,6 +983,7 @@ impl Arc { /// assert_eq!(2, Arc::strong_count(&five)); /// ``` #[inline] + #[must_use] #[stable(feature = "arc_counts", since = "1.15.0")] pub fn strong_count(this: &Self) -> usize { this.inner().strong.load(SeqCst) @@ -1079,8 +1081,6 @@ impl Arc { drop(Weak { ptr: self.ptr }); } - #[inline] - #[stable(feature = "ptr_eq", since = "1.17.0")] /// Returns `true` if the two `Arc`s point to the same allocation /// (in a vein similar to [`ptr::eq`]). /// @@ -1098,6 +1098,9 @@ impl Arc { /// ``` /// /// [`ptr::eq`]: core::ptr::eq "ptr::eq" + #[inline] + #[must_use] + #[stable(feature = "ptr_eq", since = "1.17.0")] pub fn ptr_eq(this: &Self, other: &Self) -> bool { this.ptr.as_ptr() == other.ptr.as_ptr() } @@ -1904,6 +1907,7 @@ impl Weak { /// Gets the number of strong (`Arc`) pointers pointing to this allocation. /// /// If `self` was created using [`Weak::new`], this will return 0. + #[must_use] #[stable(feature = "weak_counts", since = "1.41.0")] pub fn strong_count(&self) -> usize { if let Some(inner) = self.inner() { inner.strong.load(SeqCst) } else { 0 } @@ -1920,6 +1924,7 @@ impl Weak { /// Due to implementation details, the returned value can be off by 1 in /// either direction when other threads are manipulating any `Arc`s or /// `Weak`s pointing to the same allocation. + #[must_use] #[stable(feature = "weak_counts", since = "1.41.0")] pub fn weak_count(&self) -> usize { self.inner() @@ -1999,6 +2004,7 @@ impl Weak { /// /// [`ptr::eq`]: core::ptr::eq "ptr::eq" #[inline] + #[must_use] #[stable(feature = "weak_ptr_eq", since = "1.39.0")] pub fn ptr_eq(&self, other: &Self) -> bool { self.ptr.as_ptr() == other.ptr.as_ptr() diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index e643940d017ba..ff98091a0d2ab 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -60,6 +60,7 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> { /// Returns a reference to the underlying allocator. #[unstable(feature = "allocator_api", issue = "32838")] + #[must_use] #[inline] pub fn allocator(&self) -> &A { unsafe { self.vec.as_ref().allocator() }