diff --git a/library/core/src/slice/ascii.rs b/library/core/src/slice/ascii.rs index 1dba24dd14907..c02a6f2d78c64 100644 --- a/library/core/src/slice/ascii.rs +++ b/library/core/src/slice/ascii.rs @@ -172,6 +172,7 @@ impl_fn_for_zst! { /// documentation for more information. #[stable(feature = "inherent_ascii_escape", since = "1.60.0")] #[derive(Clone)] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct EscapeAscii<'a> { inner: iter::FlatMap, ascii::EscapeDefault, EscapeByte>, } diff --git a/library/core/src/slice/index.rs b/library/core/src/slice/index.rs index 0298bba8d329e..3d781c9591977 100644 --- a/library/core/src/slice/index.rs +++ b/library/core/src/slice/index.rs @@ -498,7 +498,7 @@ unsafe impl SliceIndex<[T]> for ops::RangeToInclusive { /// /// use std::slice; /// -/// slice::range(2..1, ..3); +/// let _ = slice::range(2..1, ..3); /// ``` /// /// ```should_panic @@ -506,7 +506,7 @@ unsafe impl SliceIndex<[T]> for ops::RangeToInclusive { /// /// use std::slice; /// -/// slice::range(1..4, ..3); +/// let _ = slice::range(1..4, ..3); /// ``` /// /// ```should_panic @@ -514,12 +514,13 @@ unsafe impl SliceIndex<[T]> for ops::RangeToInclusive { /// /// use std::slice; /// -/// slice::range(1..=usize::MAX, ..3); +/// let _ = slice::range(1..=usize::MAX, ..3); /// ``` /// /// [`Index::index`]: ops::Index::index #[track_caller] #[unstable(feature = "slice_range", issue = "76393")] +#[must_use] pub fn range(range: R, bounds: ops::RangeTo) -> ops::Range where R: ops::RangeBounds, diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index d260cc69469cf..c89cb6794b282 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -62,6 +62,7 @@ fn size_from_ptr(_: *const T) -> usize { /// [`iter`]: slice::iter /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct Iter<'a, T: 'a> { ptr: NonNull, end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that @@ -182,6 +183,7 @@ impl AsRef<[T]> for Iter<'_, T> { /// [`iter_mut`]: slice::iter_mut /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct IterMut<'a, T: 'a> { ptr: NonNull, end: *mut T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that @@ -339,6 +341,7 @@ pub(super) trait SplitIter: DoubleEndedIterator { /// [`split`]: slice::split /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct Split<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -469,6 +472,7 @@ impl FusedIterator for Split<'_, T, P> where P: FnMut(&T) -> bool {} /// [`split_inclusive`]: slice::split_inclusive /// [slices]: slice #[stable(feature = "split_inclusive", since = "1.51.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct SplitInclusive<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -589,6 +593,7 @@ impl FusedIterator for SplitInclusive<'_, T, P> where P: FnMut(&T) -> bool /// [`split_mut`]: slice::split_mut /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct SplitMut<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -718,6 +723,7 @@ impl FusedIterator for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {} /// [`split_inclusive_mut`]: slice::split_inclusive_mut /// [slices]: slice #[stable(feature = "split_inclusive", since = "1.51.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct SplitInclusiveMut<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -841,6 +847,7 @@ impl FusedIterator for SplitInclusiveMut<'_, T, P> where P: FnMut(&T) -> b /// [`rsplit`]: slice::rsplit /// [slices]: slice #[stable(feature = "slice_rsplit", since = "1.27.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RSplit<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -937,6 +944,7 @@ impl FusedIterator for RSplit<'_, T, P> where P: FnMut(&T) -> bool {} /// [`rsplit_mut`]: slice::rsplit_mut /// [slices]: slice #[stable(feature = "slice_rsplit", since = "1.27.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RSplitMut<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -1059,6 +1067,7 @@ impl> Iterator for GenericSplitN { /// [`splitn`]: slice::splitn /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct SplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -1099,6 +1108,7 @@ where /// [`rsplitn`]: slice::rsplitn /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RSplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -1138,6 +1148,7 @@ where /// [`splitn_mut`]: slice::splitn_mut /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct SplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -1178,6 +1189,7 @@ where /// [`rsplitn_mut`]: slice::rsplitn_mut /// [slices]: slice #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RSplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool, @@ -1222,6 +1234,7 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] } /// [slices]: slice #[derive(Debug)] #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct Windows<'a, T: 'a> { v: &'a [T], size: NonZeroUsize, @@ -1370,6 +1383,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Windows<'a, T> { /// [slices]: slice #[derive(Debug)] #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct Chunks<'a, T: 'a> { v: &'a [T], chunk_size: usize, @@ -1553,6 +1567,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Chunks<'a, T> { /// [slices]: slice #[derive(Debug)] #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct ChunksMut<'a, T: 'a> { v: &'a mut [T], chunk_size: usize, @@ -1722,6 +1737,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksMut<'a, T> { /// [slices]: slice #[derive(Debug)] #[stable(feature = "chunks_exact", since = "1.31.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct ChunksExact<'a, T: 'a> { v: &'a [T], rem: &'a [T], @@ -1881,6 +1897,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExact<'a, T> { /// [slices]: slice #[derive(Debug)] #[stable(feature = "chunks_exact", since = "1.31.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct ChunksExactMut<'a, T: 'a> { v: &'a mut [T], rem: &'a mut [T], @@ -2034,6 +2051,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExactMut<'a, T> { /// [slices]: slice #[derive(Debug, Clone, Copy)] #[unstable(feature = "array_windows", issue = "75027")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct ArrayWindows<'a, T: 'a, const N: usize> { slice_head: *const T, num: usize, @@ -2156,6 +2174,7 @@ impl ExactSizeIterator for ArrayWindows<'_, T, N> { /// [slices]: slice #[derive(Debug)] #[unstable(feature = "array_chunks", issue = "74985")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct ArrayChunks<'a, T: 'a, const N: usize> { iter: Iter<'a, [T; N]>, rem: &'a [T], @@ -2282,6 +2301,7 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunks<' /// [slices]: slice #[derive(Debug)] #[unstable(feature = "array_chunks", issue = "74985")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct ArrayChunksMut<'a, T: 'a, const N: usize> { iter: IterMut<'a, [T; N]>, rem: &'a mut [T], @@ -2396,6 +2416,7 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunksMu /// [slices]: slice #[derive(Debug)] #[stable(feature = "rchunks", since = "1.31.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RChunks<'a, T: 'a> { v: &'a [T], chunk_size: usize, @@ -2569,6 +2590,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunks<'a, T> { /// [slices]: slice #[derive(Debug)] #[stable(feature = "rchunks", since = "1.31.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RChunksMut<'a, T: 'a> { v: &'a mut [T], chunk_size: usize, @@ -2742,6 +2764,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksMut<'a, T> { /// [slices]: slice #[derive(Debug)] #[stable(feature = "rchunks", since = "1.31.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RChunksExact<'a, T: 'a> { v: &'a [T], rem: &'a [T], @@ -2905,6 +2928,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksExact<'a, T> { /// [slices]: slice #[derive(Debug)] #[stable(feature = "rchunks", since = "1.31.0")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct RChunksExactMut<'a, T: 'a> { v: &'a mut [T], rem: &'a mut [T], @@ -3071,6 +3095,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for IterMut<'a, T> { /// [`group_by`]: slice::group_by /// [slices]: slice #[unstable(feature = "slice_group_by", issue = "80552")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct GroupBy<'a, T: 'a, P> { slice: &'a [T], predicate: P, @@ -3157,6 +3182,7 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for GroupBy<'a, T, P> { /// [`group_by_mut`]: slice::group_by_mut /// [slices]: slice #[unstable(feature = "slice_group_by", issue = "80552")] +#[must_use = "iterators are lazy and do nothing unless consumed"] pub struct GroupByMut<'a, T: 'a, P> { slice: &'a mut [T], predicate: P, diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 08b95d791a38c..5a46a99b5a4d6 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -125,6 +125,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_slice_len", since = "1.39.0")] #[inline] + #[must_use] // SAFETY: const sound because we transmute out the length field as a usize (which it must be) pub const fn len(&self) -> usize { // FIXME: Replace with `crate::ptr::metadata(self)` when that is const-stable. @@ -148,6 +149,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_slice_is_empty", since = "1.39.0")] #[inline] + #[must_use] pub const fn is_empty(&self) -> bool { self.len() == 0 } @@ -166,6 +168,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")] #[inline] + #[must_use] pub const fn first(&self) -> Option<&T> { if let [first, ..] = self { Some(first) } else { None } } @@ -185,6 +188,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")] #[inline] + #[must_use] pub const fn first_mut(&mut self) -> Option<&mut T> { if let [first, ..] = self { Some(first) } else { None } } @@ -204,6 +208,7 @@ impl [T] { #[stable(feature = "slice_splits", since = "1.5.0")] #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")] #[inline] + #[must_use] pub const fn split_first(&self) -> Option<(&T, &[T])> { if let [first, tail @ ..] = self { Some((first, tail)) } else { None } } @@ -225,6 +230,7 @@ impl [T] { #[stable(feature = "slice_splits", since = "1.5.0")] #[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")] #[inline] + #[must_use] pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> { if let [first, tail @ ..] = self { Some((first, tail)) } else { None } } @@ -244,6 +250,7 @@ impl [T] { #[stable(feature = "slice_splits", since = "1.5.0")] #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")] #[inline] + #[must_use] pub const fn split_last(&self) -> Option<(&T, &[T])> { if let [init @ .., last] = self { Some((last, init)) } else { None } } @@ -265,6 +272,7 @@ impl [T] { #[stable(feature = "slice_splits", since = "1.5.0")] #[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")] #[inline] + #[must_use] pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> { if let [init @ .., last] = self { Some((last, init)) } else { None } } @@ -283,6 +291,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")] #[inline] + #[must_use] pub const fn last(&self) -> Option<&T> { if let [.., last] = self { Some(last) } else { None } } @@ -302,6 +311,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")] #[inline] + #[must_use] pub const fn last_mut(&mut self) -> Option<&mut T> { if let [.., last] = self { Some(last) } else { None } } @@ -325,6 +335,7 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[must_use] pub fn get(&self, index: I) -> Option<&I::Output> where I: SliceIndex, @@ -349,6 +360,7 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[must_use] pub fn get_mut(&mut self, index: I) -> Option<&mut I::Output> where I: SliceIndex, @@ -380,6 +392,7 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[must_use] pub unsafe fn get_unchecked(&self, index: I) -> &I::Output where I: SliceIndex, @@ -416,6 +429,7 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[must_use] pub unsafe fn get_unchecked_mut(&mut self, index: I) -> &mut I::Output where I: SliceIndex, @@ -455,6 +469,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")] #[inline] + #[must_use] pub const fn as_ptr(&self) -> *const T { self as *const [T] as *const T } @@ -483,6 +498,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")] #[inline] + #[must_use] pub const fn as_mut_ptr(&mut self) -> *mut T { self as *mut [T] as *mut T } @@ -518,6 +534,7 @@ impl [T] { #[stable(feature = "slice_ptr_range", since = "1.48.0")] #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")] #[inline] + #[must_use] pub const fn as_ptr_range(&self) -> Range<*const T> { let start = self.as_ptr(); // SAFETY: The `add` here is safe, because: @@ -560,6 +577,7 @@ impl [T] { #[stable(feature = "slice_ptr_range", since = "1.48.0")] #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")] #[inline] + #[must_use] pub const fn as_mut_ptr_range(&mut self) -> Range<*mut T> { let start = self.as_mut_ptr(); // SAFETY: See as_ptr_range() above for why `add` here is safe. @@ -945,6 +963,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_as_chunks", issue = "74985")] #[inline] + #[must_use] pub unsafe fn as_chunks_unchecked(&self) -> &[[T; N]] { debug_assert_ne!(N, 0); debug_assert_eq!(self.len() % N, 0); @@ -976,6 +995,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_as_chunks", issue = "74985")] #[inline] + #[must_use] pub fn as_chunks(&self) -> (&[[T; N]], &[T]) { assert_ne!(N, 0); let len = self.len() / N; @@ -1006,6 +1026,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_as_chunks", issue = "74985")] #[inline] + #[must_use] pub fn as_rchunks(&self) -> (&[T], &[[T; N]]) { assert_ne!(N, 0); let len = self.len() / N; @@ -1081,6 +1102,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_as_chunks", issue = "74985")] #[inline] + #[must_use] pub unsafe fn as_chunks_unchecked_mut(&mut self) -> &mut [[T; N]] { debug_assert_ne!(N, 0); debug_assert_eq!(self.len() % N, 0); @@ -1118,6 +1140,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_as_chunks", issue = "74985")] #[inline] + #[must_use] pub fn as_chunks_mut(&mut self) -> (&mut [[T; N]], &mut [T]) { assert_ne!(N, 0); let len = self.len() / N; @@ -1154,6 +1177,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_as_chunks", issue = "74985")] #[inline] + #[must_use] pub fn as_rchunks_mut(&mut self) -> (&mut [T], &mut [[T; N]]) { assert_ne!(N, 0); let len = self.len() / N; @@ -1512,6 +1536,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[inline] #[track_caller] + #[must_use] pub fn split_at(&self, mid: usize) -> (&[T], &[T]) { assert!(mid <= self.len()); // SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which @@ -1543,6 +1568,7 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[inline] #[track_caller] + #[must_use] pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) { assert!(mid <= self.len()); // SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which @@ -1594,6 +1620,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_split_at_unchecked", reason = "new API", issue = "76014")] #[inline] + #[must_use] pub unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) { // SAFETY: Caller has to check that `0 <= mid <= self.len()` unsafe { (self.get_unchecked(..mid), self.get_unchecked(mid..)) } @@ -1634,6 +1661,7 @@ impl [T] { /// ``` #[unstable(feature = "slice_split_at_unchecked", reason = "new API", issue = "76014")] #[inline] + #[must_use] pub unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut [T], &mut [T]) { let len = self.len(); let ptr = self.as_mut_ptr(); @@ -1683,6 +1711,7 @@ impl [T] { #[unstable(feature = "split_array", reason = "new API", issue = "90091")] #[inline] #[track_caller] + #[must_use] pub fn split_array_ref(&self) -> (&[T; N], &[T]) { let (a, b) = self.split_at(N); // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at) @@ -1715,6 +1744,7 @@ impl [T] { #[unstable(feature = "split_array", reason = "new API", issue = "90091")] #[inline] #[track_caller] + #[must_use] pub fn split_array_mut(&mut self) -> (&mut [T; N], &mut [T]) { let (a, b) = self.split_at_mut(N); // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at_mut) @@ -1759,6 +1789,7 @@ impl [T] { /// ``` #[unstable(feature = "split_array", reason = "new API", issue = "90091")] #[inline] + #[must_use] pub fn rsplit_array_ref(&self) -> (&[T], &[T; N]) { assert!(N <= self.len()); let (a, b) = self.split_at(self.len() - N); @@ -1792,6 +1823,7 @@ impl [T] { /// ``` #[unstable(feature = "split_array", reason = "new API", issue = "90091")] #[inline] + #[must_use] pub fn rsplit_array_mut(&mut self) -> (&mut [T], &mut [T; N]) { assert!(N <= self.len()); let (a, b) = self.split_at_mut(self.len() - N); @@ -2123,6 +2155,7 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[must_use] pub fn contains(&self, x: &T) -> bool where T: PartialEq, @@ -2151,6 +2184,7 @@ impl [T] { /// assert!(v.starts_with(&[])); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn starts_with(&self, needle: &[T]) -> bool where T: PartialEq, @@ -2180,6 +2214,7 @@ impl [T] { /// assert!(v.ends_with(&[])); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq, @@ -3387,6 +3422,7 @@ impl [T] { /// } /// ``` #[stable(feature = "slice_align_to", since = "1.30.0")] + #[must_use] pub unsafe fn align_to(&self) -> (&[T], &[U], &[T]) { // Note that most of this function will be constant-evaluated, if mem::size_of::() == 0 || mem::size_of::() == 0 { @@ -3447,6 +3483,7 @@ impl [T] { /// } /// ``` #[stable(feature = "slice_align_to", since = "1.30.0")] + #[must_use] pub unsafe fn align_to_mut(&mut self) -> (&mut [T], &mut [U], &mut [T]) { // Note that most of this function will be constant-evaluated, if mem::size_of::() == 0 || mem::size_of::() == 0 { @@ -3541,6 +3578,7 @@ impl [T] { /// ``` #[unstable(feature = "portable_simd", issue = "86656")] #[cfg(not(miri))] // Miri does not support all SIMD intrinsics + #[must_use] pub fn as_simd(&self) -> (&[T], &[Simd], &[T]) where Simd: AsRef<[T; LANES]>, @@ -3585,6 +3623,7 @@ impl [T] { /// method for something like `LANES == 3`. #[unstable(feature = "portable_simd", issue = "86656")] #[cfg(not(miri))] // Miri does not support all SIMD intrinsics + #[must_use] pub fn as_simd_mut(&mut self) -> (&mut [T], &mut [Simd], &mut [T]) where Simd: AsMut<[T; LANES]>, @@ -3624,6 +3663,7 @@ impl [T] { /// ``` #[inline] #[unstable(feature = "is_sorted", reason = "new API", issue = "53485")] + #[must_use] pub fn is_sorted(&self) -> bool where T: PartialOrd, @@ -3639,6 +3679,7 @@ impl [T] { /// /// [`is_sorted`]: slice::is_sorted #[unstable(feature = "is_sorted", reason = "new API", issue = "53485")] + #[must_use] pub fn is_sorted_by(&self, mut compare: F) -> bool where F: FnMut(&T, &T) -> Option, @@ -3664,6 +3705,7 @@ impl [T] { /// ``` #[inline] #[unstable(feature = "is_sorted", reason = "new API", issue = "53485")] + #[must_use] pub fn is_sorted_by_key(&self, f: F) -> bool where F: FnMut(&T) -> K, @@ -3701,6 +3743,7 @@ impl [T] { /// assert!(v[i..].iter().all(|&x| !(x < 5))); /// ``` #[stable(feature = "partition_point", since = "1.52.0")] + #[must_use] pub fn partition_point

(&self, mut pred: P) -> usize where P: FnMut(&T) -> bool, diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index 39c8d68e4bf34..6744400c304a1 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -85,6 +85,7 @@ use crate::ptr; #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")] +#[must_use] pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { debug_check_data_len(data, len); @@ -124,6 +125,7 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")] +#[must_use] pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] { debug_check_data_len(data as _, len); @@ -168,6 +170,7 @@ const fn debug_check_data_len(_data: *const T, _len: usize) {} /// Converts a reference to T into a slice of length 1 (without copying). #[stable(feature = "from_ref", since = "1.28.0")] #[rustc_const_unstable(feature = "const_slice_from_ref", issue = "90206")] +#[must_use] pub const fn from_ref(s: &T) -> &[T] { array::from_ref(s) } @@ -175,6 +178,7 @@ pub const fn from_ref(s: &T) -> &[T] { /// Converts a reference to T into a slice of length 1 (without copying). #[stable(feature = "from_ref", since = "1.28.0")] #[rustc_const_unstable(feature = "const_slice_from_ref", issue = "90206")] +#[must_use] pub const fn from_mut(s: &mut T) -> &mut [T] { array::from_mut(s) } diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs index 06a30d7096c77..fc0acc8048081 100644 --- a/library/core/tests/slice.rs +++ b/library/core/tests/slice.rs @@ -2344,7 +2344,7 @@ fn slice_rsplit_array_mut() { fn slice_split_array_ref_out_of_bounds() { let v = &[1, 2, 3, 4, 5, 6][..]; - v.split_array_ref::<7>(); + let _ = v.split_array_ref::<7>(); } #[should_panic] @@ -2352,7 +2352,7 @@ fn slice_split_array_ref_out_of_bounds() { fn slice_split_array_mut_out_of_bounds() { let v = &mut [1, 2, 3, 4, 5, 6][..]; - v.split_array_mut::<7>(); + let _ = v.split_array_mut::<7>(); } #[should_panic] @@ -2360,7 +2360,7 @@ fn slice_split_array_mut_out_of_bounds() { fn slice_rsplit_array_ref_out_of_bounds() { let v = &[1, 2, 3, 4, 5, 6][..]; - v.rsplit_array_ref::<7>(); + let _ = v.rsplit_array_ref::<7>(); } #[should_panic] @@ -2368,7 +2368,7 @@ fn slice_rsplit_array_ref_out_of_bounds() { fn slice_rsplit_array_mut_out_of_bounds() { let v = &mut [1, 2, 3, 4, 5, 6][..]; - v.rsplit_array_mut::<7>(); + let _ = v.rsplit_array_mut::<7>(); } macro_rules! take_tests { diff --git a/src/test/ui/iterators/into-iter-on-arrays-lint.fixed b/src/test/ui/iterators/into-iter-on-arrays-lint.fixed index 0055758a6a442..6e02a7024b949 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-lint.fixed +++ b/src/test/ui/iterators/into-iter-on-arrays-lint.fixed @@ -2,6 +2,7 @@ // run-rustfix // rustfix-only-machine-applicable +#[allow(unused_must_use)] fn main() { let small = [1, 2]; let big = [0u8; 33]; diff --git a/src/test/ui/iterators/into-iter-on-arrays-lint.rs b/src/test/ui/iterators/into-iter-on-arrays-lint.rs index 01857c78a730c..582d5cadd0658 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-lint.rs +++ b/src/test/ui/iterators/into-iter-on-arrays-lint.rs @@ -2,6 +2,7 @@ // run-rustfix // rustfix-only-machine-applicable +#[allow(unused_must_use)] fn main() { let small = [1, 2]; let big = [0u8; 33]; diff --git a/src/test/ui/iterators/into-iter-on-arrays-lint.stderr b/src/test/ui/iterators/into-iter-on-arrays-lint.stderr index 634728096ed69..e32d35d8638d7 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-lint.stderr +++ b/src/test/ui/iterators/into-iter-on-arrays-lint.stderr @@ -1,5 +1,5 @@ warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:10:11 + --> $DIR/into-iter-on-arrays-lint.rs:11:11 | LL | small.into_iter(); | ^^^^^^^^^ @@ -17,7 +17,7 @@ LL | IntoIterator::into_iter(small); | ++++++++++++++++++++++++ ~ warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:13:12 + --> $DIR/into-iter-on-arrays-lint.rs:14:12 | LL | [1, 2].into_iter(); | ^^^^^^^^^ @@ -34,7 +34,7 @@ LL | IntoIterator::into_iter([1, 2]); | ++++++++++++++++++++++++ ~ warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:16:9 + --> $DIR/into-iter-on-arrays-lint.rs:17:9 | LL | big.into_iter(); | ^^^^^^^^^ @@ -51,7 +51,7 @@ LL | IntoIterator::into_iter(big); | ++++++++++++++++++++++++ ~ warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:19:15 + --> $DIR/into-iter-on-arrays-lint.rs:20:15 | LL | [0u8; 33].into_iter(); | ^^^^^^^^^ @@ -68,7 +68,7 @@ LL | IntoIterator::into_iter([0u8; 33]); | ++++++++++++++++++++++++ ~ warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:23:21 + --> $DIR/into-iter-on-arrays-lint.rs:24:21 | LL | Box::new(small).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -77,7 +77,7 @@ LL | Box::new(small).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:26:22 + --> $DIR/into-iter-on-arrays-lint.rs:27:22 | LL | Box::new([1, 2]).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -86,7 +86,7 @@ LL | Box::new([1, 2]).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:29:19 + --> $DIR/into-iter-on-arrays-lint.rs:30:19 | LL | Box::new(big).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -95,7 +95,7 @@ LL | Box::new(big).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:32:25 + --> $DIR/into-iter-on-arrays-lint.rs:33:25 | LL | Box::new([0u8; 33]).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -104,7 +104,7 @@ LL | Box::new([0u8; 33]).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:36:31 + --> $DIR/into-iter-on-arrays-lint.rs:37:31 | LL | Box::new(Box::new(small)).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -113,7 +113,7 @@ LL | Box::new(Box::new(small)).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:39:32 + --> $DIR/into-iter-on-arrays-lint.rs:40:32 | LL | Box::new(Box::new([1, 2])).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -122,7 +122,7 @@ LL | Box::new(Box::new([1, 2])).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:42:29 + --> $DIR/into-iter-on-arrays-lint.rs:43:29 | LL | Box::new(Box::new(big)).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -131,7 +131,7 @@ LL | Box::new(Box::new(big)).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:45:35 + --> $DIR/into-iter-on-arrays-lint.rs:46:35 | LL | Box::new(Box::new([0u8; 33])).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`