Skip to content

Commit e7b16b6

Browse files
authored
Rollup merge of rust-lang#92853 - jendrikw:slice-must-use, r=joshtriplett
add #[must_use] to functions of slice and its iterators. See rust-lang#89692.
2 parents 8fa5d74 + cdf22c3 commit e7b16b6

File tree

9 files changed

+96
-19
lines changed

9 files changed

+96
-19
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
@@ -498,28 +498,29 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeToInclusive<usize> {
498498
///
499499
/// use std::slice;
500500
///
501-
/// slice::range(2..1, ..3);
501+
/// let _ = slice::range(2..1, ..3);
502502
/// ```
503503
///
504504
/// ```should_panic
505505
/// #![feature(slice_range)]
506506
///
507507
/// use std::slice;
508508
///
509-
/// slice::range(1..4, ..3);
509+
/// let _ = slice::range(1..4, ..3);
510510
/// ```
511511
///
512512
/// ```should_panic
513513
/// #![feature(slice_range)]
514514
///
515515
/// use std::slice;
516516
///
517-
/// slice::range(1..=usize::MAX, ..3);
517+
/// let _ = slice::range(1..=usize::MAX, ..3);
518518
/// ```
519519
///
520520
/// [`Index::index`]: ops::Index::index
521521
#[track_caller]
522522
#[unstable(feature = "slice_range", issue = "76393")]
523+
#[must_use]
523524
pub fn range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
524525
where
525526
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,

0 commit comments

Comments
 (0)