Skip to content

Commit 67c9ef2

Browse files
committed
add #[must_use] to functions of slice and its iterators.
1 parent 72e74d7 commit 67c9ef2

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

library/core/src/slice/ascii.rs

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl_fn_for_zst! {
9595
/// documentation for more information.
9696
#[unstable(feature = "inherent_ascii_escape", issue = "77174")]
9797
#[derive(Clone)]
98+
#[must_use = "iterators are lazy and do nothing unless consumed"]
9899
pub struct EscapeAscii<'a> {
99100
inner: iter::FlatMap<super::Iter<'a, u8>, ascii::EscapeDefault, EscapeByte>,
100101
}

library/core/src/slice/index.rs

+1
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeToInclusive<usize> {
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,
@@ -1539,6 +1553,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Chunks<'a, T> {
15391553
/// [slices]: slice
15401554
#[derive(Debug)]
15411555
#[stable(feature = "rust1", since = "1.0.0")]
1556+
#[must_use = "iterators are lazy and do nothing unless consumed"]
15421557
pub struct ChunksMut<'a, T: 'a> {
15431558
v: &'a mut [T],
15441559
chunk_size: usize,
@@ -1707,6 +1722,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksMut<'a, T> {
17071722
/// [slices]: slice
17081723
#[derive(Debug)]
17091724
#[stable(feature = "chunks_exact", since = "1.31.0")]
1725+
#[must_use = "iterators are lazy and do nothing unless consumed"]
17101726
pub struct ChunksExact<'a, T: 'a> {
17111727
v: &'a [T],
17121728
rem: &'a [T],
@@ -1866,6 +1882,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExact<'a, T> {
18661882
/// [slices]: slice
18671883
#[derive(Debug)]
18681884
#[stable(feature = "chunks_exact", since = "1.31.0")]
1885+
#[must_use = "iterators are lazy and do nothing unless consumed"]
18691886
pub struct ChunksExactMut<'a, T: 'a> {
18701887
v: &'a mut [T],
18711888
rem: &'a mut [T],
@@ -2019,6 +2036,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExactMut<'a, T> {
20192036
/// [slices]: slice
20202037
#[derive(Debug, Clone, Copy)]
20212038
#[unstable(feature = "array_windows", issue = "75027")]
2039+
#[must_use = "iterators are lazy and do nothing unless consumed"]
20222040
pub struct ArrayWindows<'a, T: 'a, const N: usize> {
20232041
slice_head: *const T,
20242042
num: usize,
@@ -2141,6 +2159,7 @@ impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
21412159
/// [slices]: slice
21422160
#[derive(Debug)]
21432161
#[unstable(feature = "array_chunks", issue = "74985")]
2162+
#[must_use = "iterators are lazy and do nothing unless consumed"]
21442163
pub struct ArrayChunks<'a, T: 'a, const N: usize> {
21452164
iter: Iter<'a, [T; N]>,
21462165
rem: &'a [T],
@@ -2267,6 +2286,7 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunks<'
22672286
/// [slices]: slice
22682287
#[derive(Debug)]
22692288
#[unstable(feature = "array_chunks", issue = "74985")]
2289+
#[must_use = "iterators are lazy and do nothing unless consumed"]
22702290
pub struct ArrayChunksMut<'a, T: 'a, const N: usize> {
22712291
iter: IterMut<'a, [T; N]>,
22722292
rem: &'a mut [T],
@@ -2381,6 +2401,7 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunksMu
23812401
/// [slices]: slice
23822402
#[derive(Debug)]
23832403
#[stable(feature = "rchunks", since = "1.31.0")]
2404+
#[must_use = "iterators are lazy and do nothing unless consumed"]
23842405
pub struct RChunks<'a, T: 'a> {
23852406
v: &'a [T],
23862407
chunk_size: usize,
@@ -2547,6 +2568,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunks<'a, T> {
25472568
/// [slices]: slice
25482569
#[derive(Debug)]
25492570
#[stable(feature = "rchunks", since = "1.31.0")]
2571+
#[must_use = "iterators are lazy and do nothing unless consumed"]
25502572
pub struct RChunksMut<'a, T: 'a> {
25512573
v: &'a mut [T],
25522574
chunk_size: usize,
@@ -2714,6 +2736,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksMut<'a, T> {
27142736
/// [slices]: slice
27152737
#[derive(Debug)]
27162738
#[stable(feature = "rchunks", since = "1.31.0")]
2739+
#[must_use = "iterators are lazy and do nothing unless consumed"]
27172740
pub struct RChunksExact<'a, T: 'a> {
27182741
v: &'a [T],
27192742
rem: &'a [T],
@@ -2877,6 +2900,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksExact<'a, T> {
28772900
/// [slices]: slice
28782901
#[derive(Debug)]
28792902
#[stable(feature = "rchunks", since = "1.31.0")]
2903+
#[must_use = "iterators are lazy and do nothing unless consumed"]
28802904
pub struct RChunksExactMut<'a, T: 'a> {
28812905
v: &'a mut [T],
28822906
rem: &'a mut [T],
@@ -3043,6 +3067,7 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for IterMut<'a, T> {
30433067
/// [`group_by`]: slice::group_by
30443068
/// [slices]: slice
30453069
#[unstable(feature = "slice_group_by", issue = "80552")]
3070+
#[must_use = "iterators are lazy and do nothing unless consumed"]
30463071
pub struct GroupBy<'a, T: 'a, P> {
30473072
slice: &'a [T],
30483073
predicate: P,
@@ -3129,6 +3154,7 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for GroupBy<'a, T, P> {
31293154
/// [`group_by_mut`]: slice::group_by_mut
31303155
/// [slices]: slice
31313156
#[unstable(feature = "slice_group_by", issue = "80552")]
3157+
#[must_use = "iterators are lazy and do nothing unless consumed"]
31323158
pub struct GroupByMut<'a, T: 'a, P> {
31333159
slice: &'a mut [T],
31343160
predicate: P,

0 commit comments

Comments
 (0)