diff options
62 files changed, 223 insertions, 45 deletions
diff --git a/compiler/rustc_target/src/spec/hermit_kernel_base.rs b/compiler/rustc_target/src/spec/hermit_kernel_base.rs index 414b0f7ff23..ce3dad26458 100644 --- a/compiler/rustc_target/src/spec/hermit_kernel_base.rs +++ b/compiler/rustc_target/src/spec/hermit_kernel_base.rs @@ -8,7 +8,6 @@ pub fn opts() -> TargetOptions { ); TargetOptions { - os: "hermit".to_string(), linker_flavor: LinkerFlavor::Lld(LldFlavor::Ld), disable_redzone: true, linker: Some("rust-lld".to_owned()), diff --git a/library/alloc/benches/btree/map.rs b/library/alloc/benches/btree/map.rs index c304f748847..89c21929dbc 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 d7620c68f2c..4e991018bb4 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -512,6 +512,7 @@ impl<T: Ord> BinaryHeap<T> { /// 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<T> { let mut end = self.len(); @@ -850,7 +851,6 @@ impl<T> BinaryHeap<T> { /// /// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), 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<T> { IntoIterSorted { inner: self } @@ -877,6 +877,7 @@ impl<T> BinaryHeap<T> { /// # 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<T> BinaryHeap<T> { /// 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<T> 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<I> AsIntoIter for IntoIter<I> { } } +#[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<T> { diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index e1d5ec97678..224795876fe 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<marker::Immut<'a>, 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<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -359,6 +361,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> { /// 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<K: fmt::Debug, V> 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<K, V: fmt::Debug> 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<K, V: fmt::Debug> 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<K, V> { inner: IntoIter<K, V>, @@ -431,6 +437,7 @@ impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> { /// 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<K, V> { inner: IntoIter<K, V>, @@ -449,6 +456,7 @@ impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> { /// 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<marker::Immut<'a>, K, V>, @@ -467,6 +475,7 @@ impl<K: fmt::Debug, V: fmt::Debug> 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<marker::ValMut<'a>, K, V>, @@ -1265,7 +1274,6 @@ impl<K, V> BTreeMap<K, V> { /// 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<K, V> { IntoKeys { inner: self.into_iter() } @@ -1288,7 +1296,6 @@ impl<K, V> BTreeMap<K, V> { /// 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<K, V> { 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 3e9048b1768..5cef007a46f 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 173960341f8..17389657afb 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::<BTreeMap<_, _>>(); - 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<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) { - v.get(t); + let _ = v.get(t); } #[allow(dead_code)] fn get_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) { - v.get_mut(t); + let _ = v.get_mut(t); } #[allow(dead_code)] fn get_key_value<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) { - v.get_key_value(t); + let _ = v.get_key_value(t); } #[allow(dead_code)] fn contains_key<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) { - v.contains_key(t); + let _ = v.contains_key(t); } #[allow(dead_code)] fn range<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: T) { - v.range(t..); + let _ = v.range(t..); } #[allow(dead_code)] fn range_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, 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 237e0107f24..59f1ca76b0b 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -92,6 +92,7 @@ impl<T: Clone> Clone for BTreeSet<T> { /// 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<T> { /// 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> { @@ -135,6 +137,8 @@ pub struct Range<'a, T: 'a> { /// See its documentation for more. /// /// [`difference`]: BTreeSet::difference +#[must_use = "this returns the difference as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Difference<'a, T: 'a> { inner: DifferenceInner<'a, T>, @@ -167,6 +171,8 @@ impl<T: fmt::Debug> fmt::Debug for Difference<'_, T> { /// [`BTreeSet`]. See its documentation for more. /// /// [`symmetric_difference`]: BTreeSet::symmetric_difference +#[must_use = "this returns the difference as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct SymmetricDifference<'a, T: 'a>(MergeIterInner<Iter<'a, T>>); @@ -183,6 +189,8 @@ impl<T: fmt::Debug> fmt::Debug for SymmetricDifference<'_, T> { /// See its documentation for more. /// /// [`intersection`]: BTreeSet::intersection +#[must_use = "this returns the intersection as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Intersection<'a, T: 'a> { inner: IntersectionInner<'a, T>, @@ -215,6 +223,8 @@ impl<T: fmt::Debug> fmt::Debug for Intersection<'_, T> { /// See its documentation for more. /// /// [`union`]: BTreeSet::union +#[must_use = "this returns the union as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Union<'a, T: 'a>(MergeIterInner<Iter<'a, T>>); @@ -668,6 +678,7 @@ impl<T> BTreeSet<T> { /// 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 +705,7 @@ impl<T> BTreeSet<T> { /// 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 0a87ae12d61..01cf62b32ec 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<K: Debug>(set: BTreeSet<K>) { diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs index ea010c1f89d..4c741133387 100644 --- a/library/alloc/src/collections/linked_list.rs +++ b/library/alloc/src/collections/linked_list.rs @@ -64,6 +64,7 @@ struct Node<T> { /// /// 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<NonNull<Node<T>>>, @@ -99,6 +100,7 @@ impl<T> 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<NonNull<Node<T>>>, @@ -529,6 +531,7 @@ impl<T> LinkedList<T> { /// /// 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<T> LinkedList<T> { /// /// 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<T> LinkedList<T> { /// /// 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<T> LinkedList<T> { /// /// 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<T> LinkedList<T> { /// 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<T> LinkedList<T> { /// 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<T> LinkedList<T> { /// 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<usize> { 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<usize> { 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 77d28bdfe64..1ea135a2aed 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 878d8dc5502..50e789d76b7 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/lib.rs b/library/alloc/src/lib.rs index 5e6fe3b9b32..34d2cfc8e2c 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -112,7 +112,6 @@ #![feature(maybe_uninit_slice)] #![cfg_attr(test, feature(new_uninit))] #![feature(nonnull_slice_from_raw_parts)] -#![feature(option_result_unwrap_unchecked)] #![feature(pattern)] #![feature(ptr_internals)] #![feature(receiver_trait)] diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 493cf3117ed..c0ca068de9c 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -41,7 +41,7 @@ //! use std::rc::Rc; //! //! let my_rc = Rc::new(()); -//! Rc::downgrade(&my_rc); +//! let my_weak = Rc::downgrade(&my_rc); //! ``` //! //! `Rc<T>`'s implementations of traits like `Clone` may also be called using @@ -889,6 +889,8 @@ impl<T: ?Sized> Rc<T> { /// /// let weak_five = Rc::downgrade(&five); /// ``` + #[must_use = "this returns a new `Weak` pointer, \ + without modifying the original `Rc`"] #[stable(feature = "rc_weak", since = "1.4.0")] pub fn downgrade(this: &Self) -> Weak<T> { this.inner().inc_weak(); @@ -2246,6 +2248,7 @@ impl<T: ?Sized> Weak<T> { /// 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 +2257,7 @@ impl<T: ?Sized> Weak<T> { /// 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 +2328,7 @@ impl<T: ?Sized> Weak<T> { /// 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 e1d0ee42f4e..3b875477df3 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<str>) -> 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<str>) -> 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 85ee7ea9438..677942a1820 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -552,6 +552,7 @@ impl String { /// /// assert_eq!("Hello �World", output); /// ``` + #[must_use] #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> { @@ -646,6 +647,7 @@ impl String { /// String::from_utf16_lossy(v)); /// ``` #[cfg(not(no_global_oom_handling))] + #[must_use] #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf16_lossy(v: &[u16]) -> String { @@ -898,6 +900,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() @@ -1823,6 +1826,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 00bd69aad4c..b738337a2dd 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -954,6 +954,7 @@ impl<T: ?Sized> Arc<T> { /// 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); @@ -983,6 +984,7 @@ impl<T: ?Sized> Arc<T> { /// 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) @@ -1080,8 +1082,6 @@ impl<T: ?Sized> Arc<T> { 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`]). /// @@ -1099,6 +1099,9 @@ impl<T: ?Sized> Arc<T> { /// ``` /// /// [`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() } @@ -1905,6 +1908,7 @@ impl<T: ?Sized> Weak<T> { /// 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 } @@ -1921,6 +1925,7 @@ impl<T: ?Sized> Weak<T> { /// 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() @@ -2000,6 +2005,7 @@ impl<T: ?Sized> Weak<T> { /// /// [`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 e643940d017..ff98091a0d2 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() } diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs index d3a87c056cf..dc7d0bff9a4 100644 --- a/library/alloc/tests/str.rs +++ b/library/alloc/tests/str.rs @@ -1031,7 +1031,7 @@ fn test_split_at_mut() { #[should_panic] fn test_split_at_boundscheck() { let s = "ศไทย中华Việt Nam"; - s.split_at(1); + let _ = s.split_at(1); } #[test] diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index 780f82d8afa..cc32d5223b4 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -104,6 +104,7 @@ impl Layout { /// The minimum size in bytes for a memory block of this layout. #[stable(feature = "alloc_layout", since = "1.28.0")] #[rustc_const_stable(feature = "const_alloc_layout", since = "1.50.0")] + #[must_use] #[inline] pub const fn size(&self) -> usize { self.size_ @@ -137,6 +138,7 @@ impl Layout { /// allocate backing structure for `T` (which could be a trait /// or other unsized type like a slice). #[stable(feature = "alloc_layout", since = "1.28.0")] + #[must_use] #[inline] pub fn for_value<T: ?Sized>(t: &T) -> Self { let (size, align) = (mem::size_of_val(t), mem::align_of_val(t)); @@ -171,6 +173,7 @@ impl Layout { /// [trait object]: ../../book/ch17-02-trait-objects.html /// [extern type]: ../../unstable-book/language-features/extern-types.html #[unstable(feature = "layout_for_ptr", issue = "69835")] + #[must_use] pub unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self { // SAFETY: we pass along the prerequisites of these functions to the caller let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) }; @@ -187,6 +190,7 @@ impl Layout { /// some other means. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[rustc_const_unstable(feature = "alloc_layout_extra", issue = "55724")] + #[must_use] #[inline] pub const fn dangling(&self) -> NonNull<u8> { // SAFETY: align is guaranteed to be non-zero diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 19652106b3d..1fd5aa27fce 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -458,6 +458,7 @@ impl TypeId { /// assert_eq!(is_string(&0), false); /// assert_eq!(is_string(&"cookie monster".to_string()), true); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_type_id", issue = "77125")] pub const fn of<T: ?Sized + 'static>() -> TypeId { @@ -492,6 +493,7 @@ impl TypeId { /// "core::option::Option<alloc::string::String>", /// ); /// ``` +#[must_use] #[stable(feature = "type_name", since = "1.38.0")] #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] pub const fn type_name<T: ?Sized>() -> &'static str { @@ -534,6 +536,7 @@ pub const fn type_name<T: ?Sized>() -> &'static str { /// let y = 1.0; /// println!("{}", type_name_of_val(&y)); /// ``` +#[must_use] #[unstable(feature = "type_name_of_val", issue = "66359")] #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str { diff --git a/library/core/src/ascii.rs b/library/core/src/ascii.rs index 0a456ee1eb2..532208e41af 100644 --- a/library/core/src/ascii.rs +++ b/library/core/src/ascii.rs @@ -18,6 +18,7 @@ use crate::str::from_utf8_unchecked; /// /// This `struct` is created by the [`escape_default`] function. See its /// documentation for more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct EscapeDefault { diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index ed464700cd3..d154bb3583c 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -1335,6 +1335,7 @@ impl<'b, T: ?Sized> Ref<'b, T> { /// with the widespread use of `r.borrow().clone()` to clone the contents of /// a `RefCell`. #[stable(feature = "cell_extras", since = "1.15.0")] + #[must_use] #[inline] pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> { Ref { value: orig.value, borrow: orig.borrow.clone() } diff --git a/library/core/src/char/decode.rs b/library/core/src/char/decode.rs index 4784418f98c..5dd8c5ef789 100644 --- a/library/core/src/char/decode.rs +++ b/library/core/src/char/decode.rs @@ -128,6 +128,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> { impl DecodeUtf16Error { /// Returns the unpaired surrogate which caused this error. + #[must_use] #[stable(feature = "decode_utf16", since = "1.9.0")] pub fn unpaired_surrogate(&self) -> u16 { self.code diff --git a/library/core/src/default.rs b/library/core/src/default.rs index 0ee8cd59ba4..fb862f7df94 100644 --- a/library/core/src/default.rs +++ b/library/core/src/default.rs @@ -155,6 +155,7 @@ pub trait Default: Sized { /// } /// ``` #[unstable(feature = "default_free_fn", issue = "73014")] +#[must_use] #[inline] pub fn default<T: Default>() -> T { Default::default() diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 9258b4d0818..80d3270d73c 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -1604,6 +1604,7 @@ impl<'a> Formatter<'a> { } /// Flags for formatting + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated( since = "1.24.0", @@ -1641,6 +1642,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:G>3}", Foo), "GGG"); /// assert_eq!(&format!("{:t>6}", Foo), "tttttt"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn fill(&self) -> char { self.fill @@ -1677,6 +1679,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:^}", Foo), "center"); /// assert_eq!(&format!("{}", Foo), "into the void"); /// ``` + #[must_use] #[stable(feature = "fmt_flags_align", since = "1.28.0")] pub fn align(&self) -> Option<Alignment> { match self.align { @@ -1711,6 +1714,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:10}", Foo(23)), "Foo(23) "); /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn width(&self) -> Option<usize> { self.width @@ -1741,6 +1745,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:.4}", Foo(23.2)), "Foo(23.2000)"); /// assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn precision(&self) -> Option<usize> { self.precision @@ -1771,6 +1776,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)"); /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 @@ -1799,6 +1805,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:-}", Foo(23)), "-Foo(23)"); /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 @@ -1826,6 +1833,7 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:#}", Foo(23)), "Foo(23)"); /// assert_eq!(&format!("{}", Foo(23)), "23"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 @@ -1851,6 +1859,7 @@ impl<'a> Formatter<'a> { /// /// assert_eq!(&format!("{:04}", Foo(23)), "23"); /// ``` + #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_aware_zero_pad(&self) -> bool { self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0 diff --git a/library/core/src/future/mod.rs b/library/core/src/future/mod.rs index cdde0941470..7a3af70d6d9 100644 --- a/library/core/src/future/mod.rs +++ b/library/core/src/future/mod.rs @@ -90,6 +90,7 @@ where #[lang = "get_context"] #[doc(hidden)] #[unstable(feature = "gen_future", issue = "50547")] +#[must_use] #[inline] pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> { // SAFETY: the caller must guarantee that `cx.0` is a valid pointer diff --git a/library/core/src/iter/sources/empty.rs b/library/core/src/iter/sources/empty.rs index a7d4646f5c5..7abe01d17c9 100644 --- a/library/core/src/iter/sources/empty.rs +++ b/library/core/src/iter/sources/empty.rs @@ -25,6 +25,7 @@ pub const fn empty<T>() -> Empty<T> { /// An iterator that yields nothing. /// /// This `struct` is created by the [`empty()`] function. See its documentation for more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "iter_empty", since = "1.2.0")] pub struct Empty<T>(marker::PhantomData<T>); diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 894ae10e1b4..7d005666a74 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -296,6 +296,7 @@ pub fn forget_unsized<T: ?Sized>(t: T) { /// /// [alignment]: align_of #[inline(always)] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] #[rustc_const_stable(feature = "const_size_of", since = "1.24.0")] @@ -324,6 +325,7 @@ pub const fn size_of<T>() -> usize { /// assert_eq!(13, mem::size_of_val(y)); /// ``` #[inline] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")] #[cfg_attr(not(test), rustc_diagnostic_item = "mem_size_of_val")] @@ -373,6 +375,7 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize { /// assert_eq!(13, unsafe { mem::size_of_val_raw(y) }); /// ``` #[inline] +#[must_use] #[unstable(feature = "layout_for_ptr", issue = "69835")] #[rustc_const_unstable(feature = "const_size_of_val_raw", issue = "46571")] pub const unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize { @@ -397,6 +400,7 @@ pub const unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize { /// assert_eq!(4, mem::min_align_of::<i32>()); /// ``` #[inline] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated(reason = "use `align_of` instead", since = "1.2.0")] pub fn min_align_of<T>() -> usize { @@ -418,6 +422,7 @@ pub fn min_align_of<T>() -> usize { /// assert_eq!(4, mem::min_align_of_val(&5i32)); /// ``` #[inline] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated(reason = "use `align_of_val` instead", since = "1.2.0")] pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize { @@ -441,6 +446,7 @@ pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize { /// assert_eq!(4, mem::align_of::<i32>()); /// ``` #[inline(always)] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] #[rustc_const_stable(feature = "const_align_of", since = "1.24.0")] @@ -462,6 +468,7 @@ pub const fn align_of<T>() -> usize { /// assert_eq!(4, mem::align_of_val(&5i32)); /// ``` #[inline] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")] #[allow(deprecated)] @@ -507,6 +514,7 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize { /// assert_eq!(4, unsafe { mem::align_of_val_raw(&5i32) }); /// ``` #[inline] +#[must_use] #[unstable(feature = "layout_for_ptr", issue = "69835")] #[rustc_const_unstable(feature = "const_align_of_val_raw", issue = "46571")] pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize { @@ -571,6 +579,7 @@ pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize { /// } /// ``` #[inline] +#[must_use] #[stable(feature = "needs_drop", since = "1.21.0")] #[rustc_const_stable(feature = "const_needs_drop", since = "1.36.0")] #[rustc_diagnostic_item = "needs_drop"] @@ -618,6 +627,7 @@ pub const fn needs_drop<T>() -> bool { /// let _y: fn() = unsafe { mem::zeroed() }; // And again! /// ``` #[inline(always)] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated_in_future)] #[allow(deprecated)] @@ -653,6 +663,7 @@ pub unsafe fn zeroed<T>() -> T { /// [assume_init]: MaybeUninit::assume_init /// [inv]: MaybeUninit#initialization-invariant #[inline(always)] +#[must_use] #[rustc_deprecated(since = "1.39.0", reason = "use `mem::MaybeUninit` instead")] #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated_in_future)] @@ -938,6 +949,7 @@ pub fn drop<T>(_x: T) {} /// assert_eq!(foo_array, [10]); /// ``` #[inline] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_transmute_copy", issue = "83165")] pub const unsafe fn transmute_copy<T, U>(src: &T) -> U { @@ -1051,6 +1063,7 @@ pub const fn discriminant<T>(v: &T) -> Discriminant<T> { /// assert_eq!(mem::variant_count::<Result<!, !>>(), 2); /// ``` #[inline(always)] +#[must_use] #[unstable(feature = "variant_count", issue = "73662")] #[rustc_const_unstable(feature = "variant_count", issue = "73662")] #[rustc_diagnostic_item = "mem_variant_count"] diff --git a/library/core/src/num/error.rs b/library/core/src/num/error.rs index 2af61a07482..8a9ecbe98df 100644 --- a/library/core/src/num/error.rs +++ b/library/core/src/num/error.rs @@ -115,6 +115,7 @@ pub enum IntErrorKind { impl ParseIntError { /// Outputs the detailed cause of parsing an integer failing. + #[must_use] #[stable(feature = "int_error_matching", since = "1.55.0")] pub fn kind(&self) -> &IntErrorKind { &self.kind diff --git a/library/core/src/num/f32.rs b/library/core/src/num/f32.rs index 83a922ae348..905b0c42458 100644 --- a/library/core/src/num/f32.rs +++ b/library/core/src/num/f32.rs @@ -980,6 +980,7 @@ impl f32 { /// # .all(|(a, b)| a.to_bits() == b.to_bits())) /// ``` #[unstable(feature = "total_cmp", issue = "72599")] + #[must_use] #[inline] pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering { let mut left = self.to_bits() as i32; diff --git a/library/core/src/num/f64.rs b/library/core/src/num/f64.rs index 4267260eea3..112a239a145 100644 --- a/library/core/src/num/f64.rs +++ b/library/core/src/num/f64.rs @@ -996,6 +996,7 @@ impl f64 { /// # .all(|(a, b)| a.to_bits() == b.to_bits())) /// ``` #[unstable(feature = "total_cmp", issue = "72599")] + #[must_use] #[inline] pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering { let mut left = self.to_bits() as i64; diff --git a/library/core/src/ops/control_flow.rs b/library/core/src/ops/control_flow.rs index 776cea2ef79..b0c15898a1f 100644 --- a/library/core/src/ops/control_flow.rs +++ b/library/core/src/ops/control_flow.rs @@ -7,6 +7,10 @@ use crate::{convert, ops}; /// Having the enum makes it clearer -- no more wondering "wait, what did `false` /// mean again?" -- and allows including a value. /// +/// Similar to [`Option`] and [`Result`], this enum can be used with the `?` operator +/// to return immediately if the [`Break`] variant is present or otherwise continue normally +/// with the value inside the [`Continue`] variant. +/// /// # Examples /// /// Early-exiting from [`Iterator::try_for_each`]: @@ -71,6 +75,9 @@ use crate::{convert, ops}; /// assert_eq!(res, ControlFlow::Break(-1)); /// assert_eq!(sum, 6); /// ``` +/// +/// [`Break`]: ControlFlow::Break +/// [`Continue`]: ControlFlow::Continue #[stable(feature = "control_flow_enum_type", since = "1.55.0")] #[derive(Debug, Clone, Copy, PartialEq)] pub enum ControlFlow<B, C = ()> { diff --git a/library/core/src/ops/range.rs b/library/core/src/ops/range.rs index 347a346359f..b74ba92c76e 100644 --- a/library/core/src/ops/range.rs +++ b/library/core/src/ops/range.rs @@ -743,6 +743,7 @@ impl<T: Clone> Bound<&T> { /// assert_eq!((1..12).start_bound(), Included(&1)); /// assert_eq!((1..12).start_bound().cloned(), Included(1)); /// ``` + #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "bound_cloned", since = "1.55.0")] pub fn cloned(self) -> Bound<T> { match self { diff --git a/library/core/src/option.rs b/library/core/src/option.rs index f4ce7d1dfb3..baf9948857b 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -800,19 +800,17 @@ impl<T> Option<T> { /// # Examples /// /// ``` - /// #![feature(option_result_unwrap_unchecked)] /// let x = Some("air"); /// assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); /// ``` /// /// ```no_run - /// #![feature(option_result_unwrap_unchecked)] /// let x: Option<&str> = None; /// assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); // Undefined behavior! /// ``` #[inline] #[track_caller] - #[unstable(feature = "option_result_unwrap_unchecked", reason = "newly added", issue = "81383")] + #[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")] pub unsafe fn unwrap_unchecked(self) -> T { debug_assert!(self.is_some()); match self { @@ -1451,6 +1449,7 @@ impl<T: Copy> Option<&T> { /// let copied = opt_x.copied(); /// assert_eq!(copied, Some(12)); /// ``` + #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "copied", since = "1.35.0")] #[rustc_const_unstable(feature = "const_option", issue = "67441")] pub const fn copied(self) -> Option<T> { diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index a482414caaf..714e9b73c78 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -79,6 +79,7 @@ impl<'a> Location<'a> { /// assert_ne!(this_location.line(), another_location.line()); /// assert_ne!(this_location.column(), another_location.column()); /// ``` + #[must_use] #[stable(feature = "track_caller", since = "1.46.0")] #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")] #[track_caller] @@ -119,6 +120,7 @@ impl<'a> Location<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn file(&self) -> &str { self.file @@ -141,6 +143,7 @@ impl<'a> Location<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn line(&self) -> u32 { self.line @@ -163,6 +166,7 @@ impl<'a> Location<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_col", since = "1.25.0")] pub fn column(&self) -> u32 { self.col diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 649bc3e44ad..d8e421df5de 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -81,6 +81,7 @@ impl<'a> PanicInfo<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn payload(&self) -> &(dyn Any + Send) { self.payload @@ -89,6 +90,7 @@ impl<'a> PanicInfo<'a> { /// If the `panic!` macro from the `core` crate (not from `std`) /// was used with a formatting string and some additional arguments, /// returns that message ready to be used for example with [`fmt::write`] + #[must_use] #[unstable(feature = "panic_info_message", issue = "66745")] pub fn message(&self) -> Option<&fmt::Arguments<'_>> { self.message @@ -118,6 +120,7 @@ impl<'a> PanicInfo<'a> { /// /// panic!("Normal panic"); /// ``` + #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn location(&self) -> Option<&Location<'_>> { // NOTE: If this is changed to sometimes return None, diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 34fc874ada0..09fc6df5429 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -705,6 +705,7 @@ impl<'a, T: ?Sized> Pin<&'a T> { /// /// ["pinning projections"]: self#projections-and-structural-pinning #[inline(always)] + #[must_use] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin", since = "1.33.0")] pub const fn get_ref(self) -> &'a T { diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 014170604ec..8ab72e6aeea 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -204,6 +204,7 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) { /// assert!(p.is_null()); /// ``` #[inline(always)] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] #[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")] @@ -223,6 +224,7 @@ pub const fn null<T>() -> *const T { /// assert!(p.is_null()); /// ``` #[inline(always)] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] #[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")] diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 8bae66ca007..50dd451b5d1 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -83,6 +83,7 @@ impl<T: Sized> NonNull<T> { /// ``` #[stable(feature = "nonnull", since = "1.25.0")] #[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.36.0")] + #[must_use] #[inline] pub const fn dangling() -> Self { // SAFETY: mem::align_of() returns a non-zero usize which is then casted @@ -423,6 +424,7 @@ impl<T> NonNull<[T]> { /// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.) #[unstable(feature = "nonnull_slice_from_raw_parts", issue = "71941")] #[rustc_const_unstable(feature = "const_nonnull_slice_from_raw_parts", issue = "71941")] + #[must_use] #[inline] pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self { // SAFETY: `data` is a `NonNull` pointer which is necessarily non-null diff --git a/library/core/src/ptr/unique.rs b/library/core/src/ptr/unique.rs index f6eb48f2967..d650a6f974b 100644 --- a/library/core/src/ptr/unique.rs +++ b/library/core/src/ptr/unique.rs @@ -68,6 +68,7 @@ impl<T: Sized> Unique<T> { /// a `T`, which means this must not be used as a "not yet initialized" /// sentinel value. Types that lazily allocate must track initialization by /// some other means. + #[must_use] #[inline] pub const fn dangling() -> Self { // SAFETY: mem::align_of() returns a valid, non-null pointer. The diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 75f2c222ba8..8fec2e928aa 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1096,19 +1096,17 @@ impl<T, E> Result<T, E> { /// # Examples /// /// ``` - /// #![feature(option_result_unwrap_unchecked)] /// let x: Result<u32, &str> = Ok(2); /// assert_eq!(unsafe { x.unwrap_unchecked() }, 2); /// ``` /// /// ```no_run - /// #![feature(option_result_unwrap_unchecked)] /// let x: Result<u32, &str> = Err("emergency failure"); /// unsafe { x.unwrap_unchecked(); } // Undefined behavior! /// ``` #[inline] #[track_caller] - #[unstable(feature = "option_result_unwrap_unchecked", reason = "newly added", issue = "81383")] + #[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")] pub unsafe fn unwrap_unchecked(self) -> T { debug_assert!(self.is_ok()); match self { @@ -1130,19 +1128,17 @@ impl<T, E> Result<T, E> { /// # Examples /// /// ```no_run - /// #![feature(option_result_unwrap_unchecked)] /// let x: Result<u32, &str> = Ok(2); /// unsafe { x.unwrap_err_unchecked() }; // Undefined behavior! /// ``` /// /// ``` - /// #![feature(option_result_unwrap_unchecked)] /// let x: Result<u32, &str> = Err("emergency failure"); /// assert_eq!(unsafe { x.unwrap_err_unchecked() }, "emergency failure"); /// ``` #[inline] #[track_caller] - #[unstable(feature = "option_result_unwrap_unchecked", reason = "newly added", issue = "81383")] + #[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")] pub unsafe fn unwrap_err_unchecked(self) -> E { debug_assert!(self.is_err()); match self { diff --git a/library/core/src/slice/ascii.rs b/library/core/src/slice/ascii.rs index cbb5627cef9..080256f493f 100644 --- a/library/core/src/slice/ascii.rs +++ b/library/core/src/slice/ascii.rs @@ -11,6 +11,7 @@ use crate::ops; impl [u8] { /// Checks if all bytes in this slice are within the ASCII range. #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[must_use] #[inline] pub fn is_ascii(&self) -> bool { is_ascii(self) @@ -21,6 +22,7 @@ impl [u8] { /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`, /// but without allocating and copying temporaries. #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[must_use] #[inline] pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b)) diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 07ecae78303..ad1d6b8b846 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -1724,6 +1724,7 @@ impl<'a, T> ChunksExact<'a, T> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. The returned slice has at most `chunk_size-1` /// elements. + #[must_use] #[stable(feature = "chunks_exact", since = "1.31.0")] pub fn remainder(&self) -> &'a [T] { self.rem @@ -2153,6 +2154,7 @@ impl<'a, T, const N: usize> ArrayChunks<'a, T, N> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. The returned slice has at most `N-1` /// elements. + #[must_use] #[unstable(feature = "array_chunks", issue = "74985")] pub fn remainder(&self) -> &'a [T] { self.rem @@ -2728,6 +2730,7 @@ impl<'a, T> RChunksExact<'a, T> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. The returned slice has at most `chunk_size-1` /// elements. + #[must_use] #[stable(feature = "rchunks", since = "1.31.0")] pub fn remainder(&self) -> &'a [T] { self.rem diff --git a/library/core/src/slice/memchr.rs b/library/core/src/slice/memchr.rs index 08077c700da..6da99055f2d 100644 --- a/library/core/src/slice/memchr.rs +++ b/library/core/src/slice/memchr.rs @@ -37,6 +37,7 @@ fn repeat_byte(b: u8) -> usize { } /// Returns the first index matching the byte `x` in `text`. +#[must_use] #[inline] pub fn memchr(x: u8, text: &[u8]) -> Option<usize> { // Fast path for small slices @@ -91,6 +92,7 @@ fn memchr_general_case(x: u8, text: &[u8]) -> Option<usize> { } /// Returns the last index matching the byte `x` in `text`. +#[must_use] pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> { // Scan for a single byte value by reading two `usize` words at a time. // diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index a6370a4513b..65ed72cb0cd 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -3076,6 +3076,7 @@ impl<T> [T] { /// [`copy_from_slice`]: slice::copy_from_slice /// [`split_at_mut`]: slice::split_at_mut #[stable(feature = "clone_from_slice", since = "1.7.0")] + #[track_caller] pub fn clone_from_slice(&mut self, src: &[T]) where T: Clone, @@ -3139,6 +3140,7 @@ impl<T> [T] { /// [`split_at_mut`]: slice::split_at_mut #[doc(alias = "memcpy")] #[stable(feature = "copy_from_slice", since = "1.9.0")] + #[track_caller] pub fn copy_from_slice(&mut self, src: &[T]) where T: Copy, @@ -3259,6 +3261,7 @@ impl<T> [T] { /// /// [`split_at_mut`]: slice::split_at_mut #[stable(feature = "swap_with_slice", since = "1.27.0")] + #[track_caller] pub fn swap_with_slice(&mut self, other: &mut [T]) { assert!(self.len() == other.len(), "destination and source slices have different lengths"); // SAFETY: `self` is valid for `self.len()` elements by definition, and `src` was @@ -3581,6 +3584,7 @@ impl<T> CloneFromSpec<T> for [T] where T: Clone, { + #[track_caller] default fn spec_clone_from(&mut self, src: &[T]) { assert!(self.len() == src.len(), "destination and source slices have different lengths"); // NOTE: We need to explicitly slice them to the same length @@ -3598,6 +3602,7 @@ impl<T> CloneFromSpec<T> for [T] where T: Copy, { + #[track_caller] fn spec_clone_from(&mut self, src: &[T]) { self.copy_from_slice(src); } diff --git a/library/core/src/str/error.rs b/library/core/src/str/error.rs index aa735a14cbd..b6460d72fef 100644 --- a/library/core/src/str/error.rs +++ b/library/core/src/str/error.rs @@ -72,6 +72,7 @@ impl Utf8Error { /// assert_eq!(1, error.valid_up_to()); /// ``` #[stable(feature = "utf8_error", since = "1.5.0")] + #[must_use] #[inline] pub fn valid_up_to(&self) -> usize { self.valid_up_to @@ -93,6 +94,7 @@ impl Utf8Error { /// /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html #[stable(feature = "utf8_error_error_len", since = "1.20.0")] + #[must_use] #[inline] pub fn error_len(&self) -> Option<usize> { self.error_len.map(|len| len as usize) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 94cb81e9d41..94a534c6e79 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -27,6 +27,7 @@ use super::{IsAsciiWhitespace, IsNotEmpty, IsWhitespace}; /// [`char`]: prim@char /// [`chars`]: str::chars #[derive(Clone)] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Chars<'a> { pub(super) iter: slice::Iter<'a, u8>, @@ -125,6 +126,7 @@ impl<'a> Chars<'a> { /// [`char`]: prim@char /// [`char_indices`]: str::char_indices #[derive(Clone, Debug)] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct CharIndices<'a> { pub(super) front_offset: usize, @@ -211,6 +213,7 @@ impl<'a> CharIndices<'a> { /// assert_eq!(chars.next(), None); /// ``` #[inline] + #[must_use] #[unstable(feature = "char_indices_offset", issue = "83871")] pub fn offset(&self) -> usize { self.front_offset @@ -223,6 +226,7 @@ impl<'a> CharIndices<'a> { /// See its documentation for more. /// /// [`bytes`]: str::bytes +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone, Debug)] pub struct Bytes<'a>(pub(super) Copied<slice::Iter<'a, u8>>); @@ -1089,6 +1093,7 @@ generate_pattern_iterators! { /// /// [`lines`]: str::lines #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Clone, Debug)] pub struct Lines<'a>(pub(super) Map<SplitTerminator<'a, char>, LinesAnyMap>); @@ -1128,6 +1133,7 @@ impl FusedIterator for Lines<'_> {} /// [`lines_any`]: str::lines_any #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated(since = "1.4.0", reason = "use lines()/Lines instead now")] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Clone, Debug)] #[allow(deprecated)] pub struct LinesAny<'a>(pub(super) Lines<'a>); diff --git a/library/core/src/str/lossy.rs b/library/core/src/str/lossy.rs index d3c9d21c3c7..6c21a5e8020 100644 --- a/library/core/src/str/lossy.rs +++ b/library/core/src/str/lossy.rs @@ -29,6 +29,7 @@ impl Utf8Lossy { } /// Iterator over lossy UTF-8 string +#[must_use = "iterators are lazy and do nothing unless consumed"] #[unstable(feature = "str_internals", issue = "none")] #[allow(missing_debug_implementations)] pub struct Utf8LossyChunksIter<'a> { diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 7939ea3bb7f..8f02ecbd009 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -498,6 +498,7 @@ impl str { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked(begin..end)` instead")] + #[must_use] #[inline] pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str { // SAFETY: the caller must uphold the safety contract for `get_unchecked`; @@ -570,6 +571,7 @@ impl str { /// assert_eq!(" Martin-Löf", last); /// ``` #[inline] + #[must_use] #[stable(feature = "str_split_at", since = "1.4.0")] pub fn split_at(&self, mid: usize) -> (&str, &str) { // is_char_boundary checks that the index is in [0, .len()] @@ -613,6 +615,7 @@ impl str { /// assert_eq!("PER Martin-Löf", s); /// ``` #[inline] + #[must_use] #[stable(feature = "str_split_at", since = "1.4.0")] pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) { // is_char_boundary checks that the index is in [0, .len()] @@ -2255,6 +2258,7 @@ impl str { /// assert!(!non_ascii.is_ascii()); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[must_use] #[inline] pub fn is_ascii(&self) -> bool { // We can treat each byte as character here: all multibyte characters @@ -2276,6 +2280,7 @@ impl str { /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS")); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[must_use] #[inline] pub fn eq_ignore_ascii_case(&self, other: &str) -> bool { self.as_bytes().eq_ignore_ascii_case(other.as_bytes()) diff --git a/library/core/src/str/validations.rs b/library/core/src/str/validations.rs index f2d1c737808..9a1cf905e3b 100644 --- a/library/core/src/str/validations.rs +++ b/library/core/src/str/validations.rs @@ -251,6 +251,7 @@ static UTF8_CHAR_WIDTH: [u8; 256] = [ /// Given a first byte, determines how many bytes are in this UTF-8 character. #[unstable(feature = "str_internals", issue = "none")] +#[must_use] #[inline] pub fn utf8_char_width(b: u8) -> usize { UTF8_CHAR_WIDTH[b as usize] as usize diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 620bff53879..6cba781c2ed 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -167,6 +167,7 @@ impl<'a> Context<'a> { /// Returns a reference to the `Waker` for the current task. #[stable(feature = "futures_api", since = "1.36.0")] + #[must_use] #[inline] pub fn waker(&self) -> &'a Waker { &self.waker @@ -242,6 +243,7 @@ impl Waker { /// /// This function is primarily used for optimization purposes. #[inline] + #[must_use] #[stable(feature = "futures_api", since = "1.36.0")] pub fn will_wake(&self, other: &Waker) -> bool { self.waker == other.waker diff --git a/library/core/src/time.rs b/library/core/src/time.rs index 80220a1ecb2..a054d72a880 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -357,6 +357,7 @@ impl Duration { /// ``` #[stable(feature = "duration_extras", since = "1.27.0")] #[rustc_const_stable(feature = "duration_extras", since = "1.32.0")] + #[must_use] #[inline] pub const fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI @@ -379,6 +380,7 @@ impl Duration { /// ``` #[stable(feature = "duration_extras", since = "1.27.0")] #[rustc_const_stable(feature = "duration_extras", since = "1.32.0")] + #[must_use] #[inline] pub const fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO @@ -401,6 +403,7 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[rustc_const_stable(feature = "duration", since = "1.32.0")] + #[must_use] #[inline] pub const fn subsec_nanos(&self) -> u32 { self.nanos diff --git a/library/core/tests/ascii.rs b/library/core/tests/ascii.rs index 66c25e449df..6d2cf3e83bc 100644 --- a/library/core/tests/ascii.rs +++ b/library/core/tests/ascii.rs @@ -115,7 +115,7 @@ fn test_eq_ignore_ascii_case() { #[test] fn inference_works() { let x = "a".to_string(); - x.eq_ignore_ascii_case("A"); + let _ = x.eq_ignore_ascii_case("A"); } // Shorthands used by the is_ascii_* tests. diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 1c63070874a..ce40bac3f31 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -59,7 +59,6 @@ #![feature(const_raw_ptr_deref)] #![feature(never_type)] #![feature(unwrap_infallible)] -#![feature(option_result_unwrap_unchecked)] #![feature(result_into_ok_or_err)] #![feature(ptr_metadata)] #![feature(once_cell)] diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 546c43faecf..1fc1d39b181 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1320,6 +1320,8 @@ where /// /// let mut intersection = a.intersection(&b); /// ``` +#[must_use = "this returns the intersection as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Intersection<'a, T: 'a, S: 'a> { // iterator of the first set @@ -1345,6 +1347,8 @@ pub struct Intersection<'a, T: 'a, S: 'a> { /// /// let mut difference = a.difference(&b); /// ``` +#[must_use = "this returns the difference as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Difference<'a, T: 'a, S: 'a> { // iterator of the first set @@ -1370,6 +1374,8 @@ pub struct Difference<'a, T: 'a, S: 'a> { /// /// let mut intersection = a.symmetric_difference(&b); /// ``` +#[must_use = "this returns the difference as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct SymmetricDifference<'a, T: 'a, S: 'a> { iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>, @@ -1392,6 +1398,8 @@ pub struct SymmetricDifference<'a, T: 'a, S: 'a> { /// /// let mut union_iter = a.union(&b); /// ``` +#[must_use = "this returns the union as an iterator, \ + without modifying either input set"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Union<'a, T: 'a, S: 'a> { iter: Chain<Iter<'a, T>, Difference<'a, T, S>>, diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 46c9aa5e627..49e268eb99b 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -829,6 +829,7 @@ impl OsStr { /// assert!(!non_ascii.is_ascii()); /// ``` #[stable(feature = "osstring_ascii", since = "1.53.0")] + #[must_use] #[inline] pub fn is_ascii(&self) -> bool { self.inner.is_ascii() diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 9f45e89aa75..2b76a411a0f 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1046,7 +1046,6 @@ impl Metadata { /// #[cfg_attr(unix, doc = "```no_run")] #[cfg_attr(not(unix), doc = "```ignore")] - /// #![feature(is_symlink)] /// use std::fs; /// use std::path::Path; /// use std::os::unix::fs::symlink; @@ -1062,7 +1061,7 @@ impl Metadata { /// } /// ``` #[must_use] - #[unstable(feature = "is_symlink", issue = "85748")] + #[stable(feature = "is_symlink", since = "1.57.0")] pub fn is_symlink(&self) -> bool { self.file_type().is_symlink() } diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 9389501e012..96a9da24c7e 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -487,6 +487,7 @@ impl Stdin { /// println!("got a chunk: {}", String::from_utf8_lossy(&split.unwrap())); /// } /// ``` + #[must_use = "`self` will be dropped if the result is not used"] #[unstable(feature = "stdin_forwarders", issue = "87096")] pub fn split(self, byte: u8) -> Split<StdinLock<'static>> { self.into_locked().split(byte) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 8f00d2260e4..4e547a80258 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2751,7 +2751,7 @@ impl Path { fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false) } - /// Returns true if the path exists on disk and is pointing at a symbolic link. + /// Returns `true` if the path exists on disk and is pointing at a symbolic link. /// /// This function will not traverse symbolic links. /// In case of a broken symbolic link this will also return true. @@ -2763,7 +2763,6 @@ impl Path { /// #[cfg_attr(unix, doc = "```no_run")] #[cfg_attr(not(unix), doc = "```ignore")] - /// #![feature(is_symlink)] /// use std::path::Path; /// use std::os::unix::fs::symlink; /// @@ -2772,8 +2771,14 @@ impl Path { /// assert_eq!(link_path.is_symlink(), true); /// assert_eq!(link_path.exists(), false); /// ``` - #[unstable(feature = "is_symlink", issue = "85748")] + /// + /// # See Also + /// + /// This is a convenience function that coerces errors to false. If you want to + /// check errors, call [`fs::symlink_metadata`] and handle its [`Result`]. Then call + /// [`fs::Metadata::is_symlink`] if it was [`Ok`]. #[must_use] + #[stable(feature = "is_symlink", since = "1.57.0")] pub fn is_symlink(&self) -> bool { fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false) } diff --git a/library/std/src/sys/hermit/condvar.rs b/library/std/src/sys/hermit/condvar.rs index fa8ef8fc37a..b62f21a9dac 100644 --- a/library/std/src/sys/hermit/condvar.rs +++ b/library/std/src/sys/hermit/condvar.rs @@ -55,8 +55,20 @@ impl Condvar { mutex.lock(); } - pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool { - panic!("wait_timeout not supported on hermit"); + pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { + self.counter.fetch_add(1, SeqCst); + mutex.unlock(); + let millis = dur.as_millis().min(u32::MAX as u128) as u32; + + let res = if millis > 0 { + abi::sem_timedwait(self.sem1, millis) + } else { + abi::sem_trywait(self.sem1) + }; + + abi::sem_post(self.sem2); + mutex.lock(); + res == 0 } pub unsafe fn destroy(&self) { diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 1a5cf5ab822..0c1ffeb1a79 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -257,6 +257,7 @@ pub const fn require_unstable_const_init_thread_local() {} /// [`unwrap`]: crate::result::Result::unwrap /// [naming-threads]: ./index.html#naming-threads /// [stack-size]: ./index.html#stack-size +#[must_use = "must eventually spawn the thread"] #[stable(feature = "rust1", since = "1.0.0")] #[derive(Debug)] pub struct Builder { diff --git a/src/test/ui/issues/issue-31299.rs b/src/test/ui/issues/issue-31299.rs index abed18d81f8..d93ffcb2262 100644 --- a/src/test/ui/issues/issue-31299.rs +++ b/src/test/ui/issues/issue-31299.rs @@ -29,6 +29,7 @@ struct PtrBack<T: Front>(Vec<T::Back>); struct M(PtrBack<Vec<M>>); +#[allow(unused_must_use)] fn main() { std::mem::size_of::<M>(); } |
