Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b9fcbc3

Browse files
authoredMar 26, 2022
Rollup merge of rust-lang#95274 - jendrikw:slice-must-use, r=Dylan-DPC
add #[must_use] to functions of slice and its iterators. Continuation of rust-lang#92853. Tracking issue: rust-lang#89692.
2 parents db49745 + dcdde01 commit b9fcbc3

File tree

8 files changed

+92
-15
lines changed

8 files changed

+92
-15
lines changed
 

‎library/core/src/slice/ascii.rs

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ impl_fn_for_zst! {
172172
/// documentation for more information.
173173
#[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
174174
#[derive(Clone)]
175+
#[must_use = "iterators are lazy and do nothing unless consumed"]
175176
pub struct EscapeAscii<'a> {
176177
inner: iter::FlatMap<super::Iter<'a, u8>, ascii::EscapeDefault, EscapeByte>,
177178
}

‎library/core/src/slice/index.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -549,28 +549,29 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeToInclusive<usize> {
549549
///
550550
/// use std::slice;
551551
///
552-
/// slice::range(2..1, ..3);
552+
/// let _ = slice::range(2..1, ..3);
553553
/// ```
554554
///
555555
/// ```should_panic
556556
/// #![feature(slice_range)]
557557
///
558558
/// use std::slice;
559559
///
560-
/// slice::range(1..4, ..3);
560+
/// let _ = slice::range(1..4, ..3);
561561
/// ```
562562
///
563563
/// ```should_panic
564564
/// #![feature(slice_range)]
565565
///
566566
/// use std::slice;
567567
///
568-
/// slice::range(1..=usize::MAX, ..3);
568+
/// let _ = slice::range(1..=usize::MAX, ..3);
569569
/// ```
570570
///
571571
/// [`Index::index`]: ops::Index::index
572572
#[track_caller]
573573
#[unstable(feature = "slice_range", issue = "76393")]
574+
#[must_use]
574575
pub fn range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
575576
where
576577
R: ops::RangeBounds<usize>,

‎library/core/src/slice/iter.rs

+26
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ fn size_from_ptr<T>(_: *const T) -> usize {
6262
/// [`iter`]: slice::iter
6363
/// [slices]: slice
6464
#[stable(feature = "rust1", since = "1.0.0")]
65+
#[must_use = "iterators are lazy and do nothing unless consumed"]
6566
pub struct Iter<'a, T: 'a> {
6667
ptr: NonNull<T>,
6768
end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
@@ -182,6 +183,7 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
182183
/// [`iter_mut`]: slice::iter_mut
183184
/// [slices]: slice
184185
#[stable(feature = "rust1", since = "1.0.0")]
186+
#[must_use = "iterators are lazy and do nothing unless consumed"]
185187
pub struct IterMut<'a, T: 'a> {
186188
ptr: NonNull<T>,
187189
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 {
339341
/// [`split`]: slice::split
340342
/// [slices]: slice
341343
#[stable(feature = "rust1", since = "1.0.0")]
344+
#[must_use = "iterators are lazy and do nothing unless consumed"]
342345
pub struct Split<'a, T: 'a, P>
343346
where
344347
P: FnMut(&T) -> bool,
@@ -469,6 +472,7 @@ impl<T, P> FusedIterator for Split<'_, T, P> where P: FnMut(&T) -> bool {}
469472
/// [`split_inclusive`]: slice::split_inclusive
470473
/// [slices]: slice
471474
#[stable(feature = "split_inclusive", since = "1.51.0")]
475+
#[must_use = "iterators are lazy and do nothing unless consumed"]
472476
pub struct SplitInclusive<'a, T: 'a, P>
473477
where
474478
P: FnMut(&T) -> bool,
@@ -589,6 +593,7 @@ impl<T, P> FusedIterator for SplitInclusive<'_, T, P> where P: FnMut(&T) -> bool
589593
/// [`split_mut`]: slice::split_mut
590594
/// [slices]: slice
591595
#[stable(feature = "rust1", since = "1.0.0")]
596+
#[must_use = "iterators are lazy and do nothing unless consumed"]
592597
pub struct SplitMut<'a, T: 'a, P>
593598
where
594599
P: FnMut(&T) -> bool,
@@ -718,6 +723,7 @@ impl<T, P> FusedIterator for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {}
718723
/// [`split_inclusive_mut`]: slice::split_inclusive_mut
719724
/// [slices]: slice
720725
#[stable(feature = "split_inclusive", since = "1.51.0")]
726+
#[must_use = "iterators are lazy and do nothing unless consumed"]
721727
pub struct SplitInclusiveMut<'a, T: 'a, P>
722728
where
723729
P: FnMut(&T) -> bool,
@@ -841,6 +847,7 @@ impl<T, P> FusedIterator for SplitInclusiveMut<'_, T, P> where P: FnMut(&T) -> b
841847
/// [`rsplit`]: slice::rsplit
842848
/// [slices]: slice
843849
#[stable(feature = "slice_rsplit", since = "1.27.0")]
850+
#[must_use = "iterators are lazy and do nothing unless consumed"]
844851
pub struct RSplit<'a, T: 'a, P>
845852
where
846853
P: FnMut(&T) -> bool,
@@ -937,6 +944,7 @@ impl<T, P> FusedIterator for RSplit<'_, T, P> where P: FnMut(&T) -> bool {}
937944
/// [`rsplit_mut`]: slice::rsplit_mut
938945
/// [slices]: slice
939946
#[stable(feature = "slice_rsplit", since = "1.27.0")]
947+
#[must_use = "iterators are lazy and do nothing unless consumed"]
940948
pub struct RSplitMut<'a, T: 'a, P>
941949
where
942950
P: FnMut(&T) -> bool,
@@ -1059,6 +1067,7 @@ impl<T, I: SplitIter<Item = T>> Iterator for GenericSplitN<I> {
10591067
/// [`splitn`]: slice::splitn
10601068
/// [slices]: slice
10611069
#[stable(feature = "rust1", since = "1.0.0")]
1070+
#[must_use = "iterators are lazy and do nothing unless consumed"]
10621071
pub struct SplitN<'a, T: 'a, P>
10631072
where
10641073
P: FnMut(&T) -> bool,
@@ -1099,6 +1108,7 @@ where
10991108
/// [`rsplitn`]: slice::rsplitn
11001109
/// [slices]: slice
11011110
#[stable(feature = "rust1", since = "1.0.0")]
1111+
#[must_use = "iterators are lazy and do nothing unless consumed"]
11021112
pub struct RSplitN<'a, T: 'a, P>
11031113
where
11041114
P: FnMut(&T) -> bool,
@@ -1138,6 +1148,7 @@ where
11381148
/// [`splitn_mut`]: slice::splitn_mut
11391149
/// [slices]: slice
11401150
#[stable(feature = "rust1", since = "1.0.0")]
1151+
#[must_use = "iterators are lazy and do nothing unless consumed"]
11411152
pub struct SplitNMut<'a, T: 'a, P>
11421153
where
11431154
P: FnMut(&T) -> bool,
@@ -1178,6 +1189,7 @@ where
11781189
/// [`rsplitn_mut`]: slice::rsplitn_mut
11791190
/// [slices]: slice
11801191
#[stable(feature = "rust1", since = "1.0.0")]
1192+
#[must_use = "iterators are lazy and do nothing unless consumed"]
11811193
pub struct RSplitNMut<'a, T: 'a, P>
11821194
where
11831195
P: FnMut(&T) -> bool,
@@ -1222,6 +1234,7 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] }
12221234
/// [slices]: slice
12231235
#[derive(Debug)]
12241236
#[stable(feature = "rust1", since = "1.0.0")]
1237+
#[must_use = "iterators are lazy and do nothing unless consumed"]
12251238
pub struct Windows<'a, T: 'a> {
12261239
v: &'a [T],
12271240
size: NonZeroUsize,
@@ -1370,6 +1383,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Windows<'a, T> {
13701383
/// [slices]: slice
13711384
#[derive(Debug)]
13721385
#[stable(feature = "rust1", since = "1.0.0")]
1386+
#[must_use = "iterators are lazy and do nothing unless consumed"]
13731387
pub struct Chunks<'a, T: 'a> {
13741388
v: &'a [T],
13751389
chunk_size: usize,
@@ -1553,6 +1567,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Chunks<'a, T> {
15531567
/// [slices]: slice
15541568
#[derive(Debug)]
15551569
#[stable(feature = "rust1", since = "1.0.0")]
1570+
#[must_use = "iterators are lazy and do nothing unless consumed"]
15561571
pub struct ChunksMut<'a, T: 'a> {
15571572
v: &'a mut [T],
15581573
chunk_size: usize,
@@ -1722,6 +1737,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksMut<'a, T> {
17221737
/// [slices]: slice
17231738
#[derive(Debug)]
17241739
#[stable(feature = "chunks_exact", since = "1.31.0")]
1740+
#[must_use = "iterators are lazy and do nothing unless consumed"]
17251741
pub struct ChunksExact<'a, T: 'a> {
17261742
v: &'a [T],
17271743
rem: &'a [T],
@@ -1881,6 +1897,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExact<'a, T> {
18811897
/// [slices]: slice
18821898
#[derive(Debug)]
18831899
#[stable(feature = "chunks_exact", since = "1.31.0")]
1900+
#[must_use = "iterators are lazy and do nothing unless consumed"]
18841901
pub struct ChunksExactMut<'a, T: 'a> {
18851902
v: &'a mut [T],
18861903
rem: &'a mut [T],
@@ -2034,6 +2051,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExactMut<'a, T> {
20342051
/// [slices]: slice
20352052
#[derive(Debug, Clone, Copy)]
20362053
#[unstable(feature = "array_windows", issue = "75027")]
2054+
#[must_use = "iterators are lazy and do nothing unless consumed"]
20372055
pub struct ArrayWindows<'a, T: 'a, const N: usize> {
20382056
slice_head: *const T,
20392057
num: usize,
@@ -2156,6 +2174,7 @@ impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
21562174
/// [slices]: slice
21572175
#[derive(Debug)]
21582176
#[unstable(feature = "array_chunks", issue = "74985")]
2177+
#[must_use = "iterators are lazy and do nothing unless consumed"]
21592178
pub struct ArrayChunks<'a, T: 'a, const N: usize> {
21602179
iter: Iter<'a, [T; N]>,
21612180
rem: &'a [T],
@@ -2282,6 +2301,7 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunks<'
22822301
/// [slices]: slice
22832302
#[derive(Debug)]
22842303
#[unstable(feature = "array_chunks", issue = "74985")]
2304+
#[must_use = "iterators are lazy and do nothing unless consumed"]
22852305
pub struct ArrayChunksMut<'a, T: 'a, const N: usize> {
22862306
iter: IterMut<'a, [T; N]>,
22872307
rem: &'a mut [T],
@@ -2396,6 +2416,7 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunksMu
23962416
/// [slices]: slice
23972417
#[derive(Debug)]
23982418
#[stable(feature = "rchunks", since = "1.31.0")]
2419+
#[must_use = "iterators are lazy and do nothing unless consumed"]
23992420
pub struct RChunks<'a, T: 'a> {
24002421
v: &'a [T],
24012422
chunk_size: usize,
@@ -2569,6 +2590,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunks<'a, T> {
25692590
/// [slices]: slice
25702591
#[derive(Debug)]
25712592
#[stable(feature = "rchunks", since = "1.31.0")]
2593+
#[must_use = "iterators are lazy and do nothing unless consumed"]
25722594
pub struct RChunksMut<'a, T: 'a> {
25732595
v: &'a mut [T],
25742596
chunk_size: usize,
@@ -2742,6 +2764,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksMut<'a, T> {
27422764
/// [slices]: slice
27432765
#[derive(Debug)]
27442766
#[stable(feature = "rchunks", since = "1.31.0")]
2767+
#[must_use = "iterators are lazy and do nothing unless consumed"]
27452768
pub struct RChunksExact<'a, T: 'a> {
27462769
v: &'a [T],
27472770
rem: &'a [T],
@@ -2905,6 +2928,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksExact<'a, T> {
29052928
/// [slices]: slice
29062929
#[derive(Debug)]
29072930
#[stable(feature = "rchunks", since = "1.31.0")]
2931+
#[must_use = "iterators are lazy and do nothing unless consumed"]
29082932
pub struct RChunksExactMut<'a, T: 'a> {
29092933
v: &'a mut [T],
29102934
rem: &'a mut [T],
@@ -3071,6 +3095,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for IterMut<'a, T> {
30713095
/// [`group_by`]: slice::group_by
30723096
/// [slices]: slice
30733097
#[unstable(feature = "slice_group_by", issue = "80552")]
3098+
#[must_use = "iterators are lazy and do nothing unless consumed"]
30743099
pub struct GroupBy<'a, T: 'a, P> {
30753100
slice: &'a [T],
30763101
predicate: P,
@@ -3157,6 +3182,7 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for GroupBy<'a, T, P> {
31573182
/// [`group_by_mut`]: slice::group_by_mut
31583183
/// [slices]: slice
31593184
#[unstable(feature = "slice_group_by", issue = "80552")]
3185+
#[must_use = "iterators are lazy and do nothing unless consumed"]
31603186
pub struct GroupByMut<'a, T: 'a, P> {
31613187
slice: &'a mut [T],
31623188
predicate: P,

‎library/core/src/slice/mod.rs

+43
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl<T> [T] {
124124
#[stable(feature = "rust1", since = "1.0.0")]
125125
#[rustc_const_stable(feature = "const_slice_len", since = "1.39.0")]
126126
#[inline]
127+
#[must_use]
127128
// SAFETY: const sound because we transmute out the length field as a usize (which it must be)
128129
pub const fn len(&self) -> usize {
129130
// FIXME: Replace with `crate::ptr::metadata(self)` when that is const-stable.
@@ -147,6 +148,7 @@ impl<T> [T] {
147148
#[stable(feature = "rust1", since = "1.0.0")]
148149
#[rustc_const_stable(feature = "const_slice_is_empty", since = "1.39.0")]
149150
#[inline]
151+
#[must_use]
150152
pub const fn is_empty(&self) -> bool {
151153
self.len() == 0
152154
}
@@ -165,6 +167,7 @@ impl<T> [T] {
165167
#[stable(feature = "rust1", since = "1.0.0")]
166168
#[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
167169
#[inline]
170+
#[must_use]
168171
pub const fn first(&self) -> Option<&T> {
169172
if let [first, ..] = self { Some(first) } else { None }
170173
}
@@ -184,6 +187,7 @@ impl<T> [T] {
184187
#[stable(feature = "rust1", since = "1.0.0")]
185188
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
186189
#[inline]
190+
#[must_use]
187191
pub const fn first_mut(&mut self) -> Option<&mut T> {
188192
if let [first, ..] = self { Some(first) } else { None }
189193
}
@@ -203,6 +207,7 @@ impl<T> [T] {
203207
#[stable(feature = "slice_splits", since = "1.5.0")]
204208
#[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
205209
#[inline]
210+
#[must_use]
206211
pub const fn split_first(&self) -> Option<(&T, &[T])> {
207212
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
208213
}
@@ -224,6 +229,7 @@ impl<T> [T] {
224229
#[stable(feature = "slice_splits", since = "1.5.0")]
225230
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
226231
#[inline]
232+
#[must_use]
227233
pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
228234
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
229235
}
@@ -243,6 +249,7 @@ impl<T> [T] {
243249
#[stable(feature = "slice_splits", since = "1.5.0")]
244250
#[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
245251
#[inline]
252+
#[must_use]
246253
pub const fn split_last(&self) -> Option<(&T, &[T])> {
247254
if let [init @ .., last] = self { Some((last, init)) } else { None }
248255
}
@@ -264,6 +271,7 @@ impl<T> [T] {
264271
#[stable(feature = "slice_splits", since = "1.5.0")]
265272
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
266273
#[inline]
274+
#[must_use]
267275
pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
268276
if let [init @ .., last] = self { Some((last, init)) } else { None }
269277
}
@@ -282,6 +290,7 @@ impl<T> [T] {
282290
#[stable(feature = "rust1", since = "1.0.0")]
283291
#[rustc_const_stable(feature = "const_slice_first_last_not_mut", since = "1.56.0")]
284292
#[inline]
293+
#[must_use]
285294
pub const fn last(&self) -> Option<&T> {
286295
if let [.., last] = self { Some(last) } else { None }
287296
}
@@ -301,6 +310,7 @@ impl<T> [T] {
301310
#[stable(feature = "rust1", since = "1.0.0")]
302311
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
303312
#[inline]
313+
#[must_use]
304314
pub const fn last_mut(&mut self) -> Option<&mut T> {
305315
if let [.., last] = self { Some(last) } else { None }
306316
}
@@ -325,6 +335,7 @@ impl<T> [T] {
325335
#[stable(feature = "rust1", since = "1.0.0")]
326336
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
327337
#[inline]
338+
#[must_use]
328339
pub const fn get<I>(&self, index: I) -> Option<&I::Output>
329340
where
330341
I: ~const SliceIndex<Self>,
@@ -350,6 +361,7 @@ impl<T> [T] {
350361
#[stable(feature = "rust1", since = "1.0.0")]
351362
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
352363
#[inline]
364+
#[must_use]
353365
pub const fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
354366
where
355367
I: ~const SliceIndex<Self>,
@@ -382,6 +394,7 @@ impl<T> [T] {
382394
#[stable(feature = "rust1", since = "1.0.0")]
383395
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
384396
#[inline]
397+
#[must_use]
385398
pub const unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
386399
where
387400
I: ~const SliceIndex<Self>,
@@ -419,6 +432,7 @@ impl<T> [T] {
419432
#[stable(feature = "rust1", since = "1.0.0")]
420433
#[rustc_const_unstable(feature = "const_slice_index", issue = "none")]
421434
#[inline]
435+
#[must_use]
422436
pub const unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
423437
where
424438
I: ~const SliceIndex<Self>,
@@ -458,6 +472,7 @@ impl<T> [T] {
458472
#[stable(feature = "rust1", since = "1.0.0")]
459473
#[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")]
460474
#[inline]
475+
#[must_use]
461476
pub const fn as_ptr(&self) -> *const T {
462477
self as *const [T] as *const T
463478
}
@@ -486,6 +501,7 @@ impl<T> [T] {
486501
#[stable(feature = "rust1", since = "1.0.0")]
487502
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
488503
#[inline]
504+
#[must_use]
489505
pub const fn as_mut_ptr(&mut self) -> *mut T {
490506
self as *mut [T] as *mut T
491507
}
@@ -521,6 +537,7 @@ impl<T> [T] {
521537
#[stable(feature = "slice_ptr_range", since = "1.48.0")]
522538
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
523539
#[inline]
540+
#[must_use]
524541
pub const fn as_ptr_range(&self) -> Range<*const T> {
525542
let start = self.as_ptr();
526543
// SAFETY: The `add` here is safe, because:
@@ -563,6 +580,7 @@ impl<T> [T] {
563580
#[stable(feature = "slice_ptr_range", since = "1.48.0")]
564581
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
565582
#[inline]
583+
#[must_use]
566584
pub const fn as_mut_ptr_range(&mut self) -> Range<*mut T> {
567585
let start = self.as_mut_ptr();
568586
// SAFETY: See as_ptr_range() above for why `add` here is safe.
@@ -948,6 +966,7 @@ impl<T> [T] {
948966
/// ```
949967
#[unstable(feature = "slice_as_chunks", issue = "74985")]
950968
#[inline]
969+
#[must_use]
951970
pub unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
952971
debug_assert_ne!(N, 0);
953972
debug_assert_eq!(self.len() % N, 0);
@@ -979,6 +998,7 @@ impl<T> [T] {
979998
/// ```
980999
#[unstable(feature = "slice_as_chunks", issue = "74985")]
9811000
#[inline]
1001+
#[must_use]
9821002
pub fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
9831003
assert_ne!(N, 0);
9841004
let len = self.len() / N;
@@ -1009,6 +1029,7 @@ impl<T> [T] {
10091029
/// ```
10101030
#[unstable(feature = "slice_as_chunks", issue = "74985")]
10111031
#[inline]
1032+
#[must_use]
10121033
pub fn as_rchunks<const N: usize>(&self) -> (&[T], &[[T; N]]) {
10131034
assert_ne!(N, 0);
10141035
let len = self.len() / N;
@@ -1084,6 +1105,7 @@ impl<T> [T] {
10841105
/// ```
10851106
#[unstable(feature = "slice_as_chunks", issue = "74985")]
10861107
#[inline]
1108+
#[must_use]
10871109
pub unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
10881110
debug_assert_ne!(N, 0);
10891111
debug_assert_eq!(self.len() % N, 0);
@@ -1121,6 +1143,7 @@ impl<T> [T] {
11211143
/// ```
11221144
#[unstable(feature = "slice_as_chunks", issue = "74985")]
11231145
#[inline]
1146+
#[must_use]
11241147
pub fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
11251148
assert_ne!(N, 0);
11261149
let len = self.len() / N;
@@ -1157,6 +1180,7 @@ impl<T> [T] {
11571180
/// ```
11581181
#[unstable(feature = "slice_as_chunks", issue = "74985")]
11591182
#[inline]
1183+
#[must_use]
11601184
pub fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut [T], &mut [[T; N]]) {
11611185
assert_ne!(N, 0);
11621186
let len = self.len() / N;
@@ -1515,6 +1539,7 @@ impl<T> [T] {
15151539
#[stable(feature = "rust1", since = "1.0.0")]
15161540
#[inline]
15171541
#[track_caller]
1542+
#[must_use]
15181543
pub fn split_at(&self, mid: usize) -> (&[T], &[T]) {
15191544
assert!(mid <= self.len());
15201545
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
@@ -1546,6 +1571,7 @@ impl<T> [T] {
15461571
#[stable(feature = "rust1", since = "1.0.0")]
15471572
#[inline]
15481573
#[track_caller]
1574+
#[must_use]
15491575
pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
15501576
assert!(mid <= self.len());
15511577
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
@@ -1597,6 +1623,7 @@ impl<T> [T] {
15971623
/// ```
15981624
#[unstable(feature = "slice_split_at_unchecked", reason = "new API", issue = "76014")]
15991625
#[inline]
1626+
#[must_use]
16001627
pub unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
16011628
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
16021629
unsafe { (self.get_unchecked(..mid), self.get_unchecked(mid..)) }
@@ -1637,6 +1664,7 @@ impl<T> [T] {
16371664
/// ```
16381665
#[unstable(feature = "slice_split_at_unchecked", reason = "new API", issue = "76014")]
16391666
#[inline]
1667+
#[must_use]
16401668
pub unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
16411669
let len = self.len();
16421670
let ptr = self.as_mut_ptr();
@@ -1686,6 +1714,7 @@ impl<T> [T] {
16861714
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
16871715
#[inline]
16881716
#[track_caller]
1717+
#[must_use]
16891718
pub fn split_array_ref<const N: usize>(&self) -> (&[T; N], &[T]) {
16901719
let (a, b) = self.split_at(N);
16911720
// SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
@@ -1718,6 +1747,7 @@ impl<T> [T] {
17181747
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
17191748
#[inline]
17201749
#[track_caller]
1750+
#[must_use]
17211751
pub fn split_array_mut<const N: usize>(&mut self) -> (&mut [T; N], &mut [T]) {
17221752
let (a, b) = self.split_at_mut(N);
17231753
// SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
@@ -1762,6 +1792,7 @@ impl<T> [T] {
17621792
/// ```
17631793
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
17641794
#[inline]
1795+
#[must_use]
17651796
pub fn rsplit_array_ref<const N: usize>(&self) -> (&[T], &[T; N]) {
17661797
assert!(N <= self.len());
17671798
let (a, b) = self.split_at(self.len() - N);
@@ -1795,6 +1826,7 @@ impl<T> [T] {
17951826
/// ```
17961827
#[unstable(feature = "split_array", reason = "new API", issue = "90091")]
17971828
#[inline]
1829+
#[must_use]
17981830
pub fn rsplit_array_mut<const N: usize>(&mut self) -> (&mut [T], &mut [T; N]) {
17991831
assert!(N <= self.len());
18001832
let (a, b) = self.split_at_mut(self.len() - N);
@@ -2126,6 +2158,7 @@ impl<T> [T] {
21262158
/// ```
21272159
#[stable(feature = "rust1", since = "1.0.0")]
21282160
#[inline]
2161+
#[must_use]
21292162
pub fn contains(&self, x: &T) -> bool
21302163
where
21312164
T: PartialEq,
@@ -2154,6 +2187,7 @@ impl<T> [T] {
21542187
/// assert!(v.starts_with(&[]));
21552188
/// ```
21562189
#[stable(feature = "rust1", since = "1.0.0")]
2190+
#[must_use]
21572191
pub fn starts_with(&self, needle: &[T]) -> bool
21582192
where
21592193
T: PartialEq,
@@ -2183,6 +2217,7 @@ impl<T> [T] {
21832217
/// assert!(v.ends_with(&[]));
21842218
/// ```
21852219
#[stable(feature = "rust1", since = "1.0.0")]
2220+
#[must_use]
21862221
pub fn ends_with(&self, needle: &[T]) -> bool
21872222
where
21882223
T: PartialEq,
@@ -3390,6 +3425,7 @@ impl<T> [T] {
33903425
/// }
33913426
/// ```
33923427
#[stable(feature = "slice_align_to", since = "1.30.0")]
3428+
#[must_use]
33933429
pub unsafe fn align_to<U>(&self) -> (&[T], &[U], &[T]) {
33943430
// Note that most of this function will be constant-evaluated,
33953431
if mem::size_of::<U>() == 0 || mem::size_of::<T>() == 0 {
@@ -3450,6 +3486,7 @@ impl<T> [T] {
34503486
/// }
34513487
/// ```
34523488
#[stable(feature = "slice_align_to", since = "1.30.0")]
3489+
#[must_use]
34533490
pub unsafe fn align_to_mut<U>(&mut self) -> (&mut [T], &mut [U], &mut [T]) {
34543491
// Note that most of this function will be constant-evaluated,
34553492
if mem::size_of::<U>() == 0 || mem::size_of::<T>() == 0 {
@@ -3543,6 +3580,7 @@ impl<T> [T] {
35433580
/// assert_eq!(basic_simd_sum(&numbers[1..99]), 4949.0);
35443581
/// ```
35453582
#[unstable(feature = "portable_simd", issue = "86656")]
3583+
#[must_use]
35463584
pub fn as_simd<const LANES: usize>(&self) -> (&[T], &[Simd<T, LANES>], &[T])
35473585
where
35483586
Simd<T, LANES>: AsRef<[T; LANES]>,
@@ -3586,6 +3624,7 @@ impl<T> [T] {
35863624
/// be lifted in a way that would make it possible to see panics from this
35873625
/// method for something like `LANES == 3`.
35883626
#[unstable(feature = "portable_simd", issue = "86656")]
3627+
#[must_use]
35893628
pub fn as_simd_mut<const LANES: usize>(&mut self) -> (&mut [T], &mut [Simd<T, LANES>], &mut [T])
35903629
where
35913630
Simd<T, LANES>: AsMut<[T; LANES]>,
@@ -3625,6 +3664,7 @@ impl<T> [T] {
36253664
/// ```
36263665
#[inline]
36273666
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
3667+
#[must_use]
36283668
pub fn is_sorted(&self) -> bool
36293669
where
36303670
T: PartialOrd,
@@ -3640,6 +3680,7 @@ impl<T> [T] {
36403680
///
36413681
/// [`is_sorted`]: slice::is_sorted
36423682
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
3683+
#[must_use]
36433684
pub fn is_sorted_by<F>(&self, mut compare: F) -> bool
36443685
where
36453686
F: FnMut(&T, &T) -> Option<Ordering>,
@@ -3665,6 +3706,7 @@ impl<T> [T] {
36653706
/// ```
36663707
#[inline]
36673708
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
3709+
#[must_use]
36683710
pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool
36693711
where
36703712
F: FnMut(&T) -> K,
@@ -3702,6 +3744,7 @@ impl<T> [T] {
37023744
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
37033745
/// ```
37043746
#[stable(feature = "partition_point", since = "1.52.0")]
3747+
#[must_use]
37053748
pub fn partition_point<P>(&self, mut pred: P) -> usize
37063749
where
37073750
P: FnMut(&T) -> bool,

‎library/core/src/slice/raw.rs

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use crate::ptr;
8585
#[inline]
8686
#[stable(feature = "rust1", since = "1.0.0")]
8787
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
88+
#[must_use]
8889
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
8990
debug_check_data_len(data, len);
9091

@@ -124,6 +125,7 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
124125
#[inline]
125126
#[stable(feature = "rust1", since = "1.0.0")]
126127
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
128+
#[must_use]
127129
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
128130
debug_check_data_len(data as _, len);
129131

@@ -168,13 +170,15 @@ const fn debug_check_data_len<T>(_data: *const T, _len: usize) {}
168170
/// Converts a reference to T into a slice of length 1 (without copying).
169171
#[stable(feature = "from_ref", since = "1.28.0")]
170172
#[rustc_const_unstable(feature = "const_slice_from_ref", issue = "90206")]
173+
#[must_use]
171174
pub const fn from_ref<T>(s: &T) -> &[T] {
172175
array::from_ref(s)
173176
}
174177

175178
/// Converts a reference to T into a slice of length 1 (without copying).
176179
#[stable(feature = "from_ref", since = "1.28.0")]
177180
#[rustc_const_unstable(feature = "const_slice_from_ref", issue = "90206")]
181+
#[must_use]
178182
pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
179183
array::from_mut(s)
180184
}

‎src/test/ui/iterators/into-iter-on-arrays-lint.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// run-rustfix
33
// rustfix-only-machine-applicable
44

5+
#[allow(unused_must_use)]
56
fn main() {
67
let small = [1, 2];
78
let big = [0u8; 33];

‎src/test/ui/iterators/into-iter-on-arrays-lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// run-rustfix
33
// rustfix-only-machine-applicable
44

5+
#[allow(unused_must_use)]
56
fn main() {
67
let small = [1, 2];
78
let big = [0u8; 33];

‎src/test/ui/iterators/into-iter-on-arrays-lint.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
2-
--> $DIR/into-iter-on-arrays-lint.rs:10:11
2+
--> $DIR/into-iter-on-arrays-lint.rs:11:11
33
|
44
LL | small.into_iter();
55
| ^^^^^^^^^
@@ -17,7 +17,7 @@ LL | IntoIterator::into_iter(small);
1717
| ++++++++++++++++++++++++ ~
1818

1919
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
20-
--> $DIR/into-iter-on-arrays-lint.rs:13:12
20+
--> $DIR/into-iter-on-arrays-lint.rs:14:12
2121
|
2222
LL | [1, 2].into_iter();
2323
| ^^^^^^^^^
@@ -34,7 +34,7 @@ LL | IntoIterator::into_iter([1, 2]);
3434
| ++++++++++++++++++++++++ ~
3535

3636
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
37-
--> $DIR/into-iter-on-arrays-lint.rs:16:9
37+
--> $DIR/into-iter-on-arrays-lint.rs:17:9
3838
|
3939
LL | big.into_iter();
4040
| ^^^^^^^^^
@@ -51,7 +51,7 @@ LL | IntoIterator::into_iter(big);
5151
| ++++++++++++++++++++++++ ~
5252

5353
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
54-
--> $DIR/into-iter-on-arrays-lint.rs:19:15
54+
--> $DIR/into-iter-on-arrays-lint.rs:20:15
5555
|
5656
LL | [0u8; 33].into_iter();
5757
| ^^^^^^^^^
@@ -68,7 +68,7 @@ LL | IntoIterator::into_iter([0u8; 33]);
6868
| ++++++++++++++++++++++++ ~
6969

7070
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
71-
--> $DIR/into-iter-on-arrays-lint.rs:23:21
71+
--> $DIR/into-iter-on-arrays-lint.rs:24:21
7272
|
7373
LL | Box::new(small).into_iter();
7474
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
@@ -77,7 +77,7 @@ LL | Box::new(small).into_iter();
7777
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
7878

7979
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
80-
--> $DIR/into-iter-on-arrays-lint.rs:26:22
80+
--> $DIR/into-iter-on-arrays-lint.rs:27:22
8181
|
8282
LL | Box::new([1, 2]).into_iter();
8383
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
@@ -86,7 +86,7 @@ LL | Box::new([1, 2]).into_iter();
8686
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
8787

8888
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
89-
--> $DIR/into-iter-on-arrays-lint.rs:29:19
89+
--> $DIR/into-iter-on-arrays-lint.rs:30:19
9090
|
9191
LL | Box::new(big).into_iter();
9292
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
@@ -95,7 +95,7 @@ LL | Box::new(big).into_iter();
9595
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
9696

9797
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
98-
--> $DIR/into-iter-on-arrays-lint.rs:32:25
98+
--> $DIR/into-iter-on-arrays-lint.rs:33:25
9999
|
100100
LL | Box::new([0u8; 33]).into_iter();
101101
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
@@ -104,7 +104,7 @@ LL | Box::new([0u8; 33]).into_iter();
104104
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
105105

106106
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
107-
--> $DIR/into-iter-on-arrays-lint.rs:36:31
107+
--> $DIR/into-iter-on-arrays-lint.rs:37:31
108108
|
109109
LL | Box::new(Box::new(small)).into_iter();
110110
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
@@ -113,7 +113,7 @@ LL | Box::new(Box::new(small)).into_iter();
113113
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
114114

115115
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
116-
--> $DIR/into-iter-on-arrays-lint.rs:39:32
116+
--> $DIR/into-iter-on-arrays-lint.rs:40:32
117117
|
118118
LL | Box::new(Box::new([1, 2])).into_iter();
119119
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
@@ -122,7 +122,7 @@ LL | Box::new(Box::new([1, 2])).into_iter();
122122
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
123123

124124
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
125-
--> $DIR/into-iter-on-arrays-lint.rs:42:29
125+
--> $DIR/into-iter-on-arrays-lint.rs:43:29
126126
|
127127
LL | Box::new(Box::new(big)).into_iter();
128128
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
@@ -131,7 +131,7 @@ LL | Box::new(Box::new(big)).into_iter();
131131
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
132132

133133
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
134-
--> $DIR/into-iter-on-arrays-lint.rs:45:35
134+
--> $DIR/into-iter-on-arrays-lint.rs:46:35
135135
|
136136
LL | Box::new(Box::new([0u8; 33])).into_iter();
137137
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`

0 commit comments

Comments
 (0)
Please sign in to comment.