Skip to content

Commit a26b1d2

Browse files
authored
Rollup merge of #89835 - jkugelman:must-use-expensive-computations, r=joshtriplett
Add #[must_use] to expensive computations The unifying theme for this commit is weak, admittedly. I put together a list of "expensive" functions when I originally proposed this whole effort, but nobody's cared about that criterion. Still, it's a decent way to bite off a not-too-big chunk of work. Given the grab bag nature of this commit, the messages I used vary quite a bit. I'm open to wording changes. For some reason clippy flagged four `BTreeSet` methods but didn't say boo about equivalent ones on `HashSet`. I stared at them for a while but I can't figure out the difference so I added the `HashSet` ones in. ```rust // Flagged by clippy. alloc::collections::btree_set::BTreeSet<T> fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T>; alloc::collections::btree_set::BTreeSet<T> fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>) -> SymmetricDifference<'a, T> alloc::collections::btree_set::BTreeSet<T> fn intersection<'a>(&'a self, other: &'a BTreeSet<T>) -> Intersection<'a, T>; alloc::collections::btree_set::BTreeSet<T> fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T>; // Ignored by clippy, but not by me. std::collections::HashSet<T, S> fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S>; std::collections::HashSet<T, S> fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, S>) -> SymmetricDifference<'a, T, S> std::collections::HashSet<T, S> fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S>; std::collections::HashSet<T, S> fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S>; ``` Parent issue: #89692 r? ```@joshtriplett```
2 parents 3cf3910 + 21f4677 commit a26b1d2

File tree

11 files changed

+32
-1
lines changed

11 files changed

+32
-1
lines changed

library/alloc/src/collections/btree/set.rs

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ pub struct Range<'a, T: 'a> {
137137
/// See its documentation for more.
138138
///
139139
/// [`difference`]: BTreeSet::difference
140+
#[must_use = "this returns the difference as an iterator, \
141+
without modifying either input set"]
140142
#[stable(feature = "rust1", since = "1.0.0")]
141143
pub struct Difference<'a, T: 'a> {
142144
inner: DifferenceInner<'a, T>,
@@ -169,6 +171,8 @@ impl<T: fmt::Debug> fmt::Debug for Difference<'_, T> {
169171
/// [`BTreeSet`]. See its documentation for more.
170172
///
171173
/// [`symmetric_difference`]: BTreeSet::symmetric_difference
174+
#[must_use = "this returns the difference as an iterator, \
175+
without modifying either input set"]
172176
#[stable(feature = "rust1", since = "1.0.0")]
173177
pub struct SymmetricDifference<'a, T: 'a>(MergeIterInner<Iter<'a, T>>);
174178

@@ -185,6 +189,8 @@ impl<T: fmt::Debug> fmt::Debug for SymmetricDifference<'_, T> {
185189
/// See its documentation for more.
186190
///
187191
/// [`intersection`]: BTreeSet::intersection
192+
#[must_use = "this returns the intersection as an iterator, \
193+
without modifying either input set"]
188194
#[stable(feature = "rust1", since = "1.0.0")]
189195
pub struct Intersection<'a, T: 'a> {
190196
inner: IntersectionInner<'a, T>,
@@ -217,6 +223,8 @@ impl<T: fmt::Debug> fmt::Debug for Intersection<'_, T> {
217223
/// See its documentation for more.
218224
///
219225
/// [`union`]: BTreeSet::union
226+
#[must_use = "this returns the union as an iterator, \
227+
without modifying either input set"]
220228
#[stable(feature = "rust1", since = "1.0.0")]
221229
pub struct Union<'a, T: 'a>(MergeIterInner<Iter<'a, T>>);
222230

library/alloc/src/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ impl String {
552552
///
553553
/// assert_eq!("Hello �World", output);
554554
/// ```
555+
#[must_use]
555556
#[cfg(not(no_global_oom_handling))]
556557
#[stable(feature = "rust1", since = "1.0.0")]
557558
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
@@ -646,6 +647,7 @@ impl String {
646647
/// String::from_utf16_lossy(v));
647648
/// ```
648649
#[cfg(not(no_global_oom_handling))]
650+
#[must_use]
649651
#[inline]
650652
#[stable(feature = "rust1", since = "1.0.0")]
651653
pub fn from_utf16_lossy(v: &[u16]) -> String {

library/core/src/slice/ascii.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::ops;
1111
impl [u8] {
1212
/// Checks if all bytes in this slice are within the ASCII range.
1313
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
14+
#[must_use]
1415
#[inline]
1516
pub fn is_ascii(&self) -> bool {
1617
is_ascii(self)
@@ -21,6 +22,7 @@ impl [u8] {
2122
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
2223
/// but without allocating and copying temporaries.
2324
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
25+
#[must_use]
2426
#[inline]
2527
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
2628
self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b))

library/core/src/slice/memchr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn repeat_byte(b: u8) -> usize {
3737
}
3838

3939
/// Returns the first index matching the byte `x` in `text`.
40+
#[must_use]
4041
#[inline]
4142
pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
4243
// Fast path for small slices
@@ -91,6 +92,7 @@ fn memchr_general_case(x: u8, text: &[u8]) -> Option<usize> {
9192
}
9293

9394
/// Returns the last index matching the byte `x` in `text`.
95+
#[must_use]
9496
pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
9597
// Scan for a single byte value by reading two `usize` words at a time.
9698
//

library/core/src/str/iter.rs

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use super::{IsAsciiWhitespace, IsNotEmpty, IsWhitespace};
2727
/// [`char`]: prim@char
2828
/// [`chars`]: str::chars
2929
#[derive(Clone)]
30+
#[must_use = "iterators are lazy and do nothing unless consumed"]
3031
#[stable(feature = "rust1", since = "1.0.0")]
3132
pub struct Chars<'a> {
3233
pub(super) iter: slice::Iter<'a, u8>,
@@ -125,6 +126,7 @@ impl<'a> Chars<'a> {
125126
/// [`char`]: prim@char
126127
/// [`char_indices`]: str::char_indices
127128
#[derive(Clone, Debug)]
129+
#[must_use = "iterators are lazy and do nothing unless consumed"]
128130
#[stable(feature = "rust1", since = "1.0.0")]
129131
pub struct CharIndices<'a> {
130132
pub(super) front_offset: usize,
@@ -1089,6 +1091,7 @@ generate_pattern_iterators! {
10891091
///
10901092
/// [`lines`]: str::lines
10911093
#[stable(feature = "rust1", since = "1.0.0")]
1094+
#[must_use = "iterators are lazy and do nothing unless consumed"]
10921095
#[derive(Clone, Debug)]
10931096
pub struct Lines<'a>(pub(super) Map<SplitTerminator<'a, char>, LinesAnyMap>);
10941097

@@ -1128,6 +1131,7 @@ impl FusedIterator for Lines<'_> {}
11281131
/// [`lines_any`]: str::lines_any
11291132
#[stable(feature = "rust1", since = "1.0.0")]
11301133
#[rustc_deprecated(since = "1.4.0", reason = "use lines()/Lines instead now")]
1134+
#[must_use = "iterators are lazy and do nothing unless consumed"]
11311135
#[derive(Clone, Debug)]
11321136
#[allow(deprecated)]
11331137
pub struct LinesAny<'a>(pub(super) Lines<'a>);

library/core/src/str/lossy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl Utf8Lossy {
2929
}
3030

3131
/// Iterator over lossy UTF-8 string
32+
#[must_use = "iterators are lazy and do nothing unless consumed"]
3233
#[unstable(feature = "str_internals", issue = "none")]
3334
#[allow(missing_debug_implementations)]
3435
pub struct Utf8LossyChunksIter<'a> {

library/core/src/str/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,7 @@ impl str {
22552255
/// assert!(!non_ascii.is_ascii());
22562256
/// ```
22572257
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2258+
#[must_use]
22582259
#[inline]
22592260
pub fn is_ascii(&self) -> bool {
22602261
// We can treat each byte as character here: all multibyte characters
@@ -2276,6 +2277,7 @@ impl str {
22762277
/// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
22772278
/// ```
22782279
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2280+
#[must_use]
22792281
#[inline]
22802282
pub fn eq_ignore_ascii_case(&self, other: &str) -> bool {
22812283
self.as_bytes().eq_ignore_ascii_case(other.as_bytes())

library/core/tests/ascii.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn test_eq_ignore_ascii_case() {
115115
#[test]
116116
fn inference_works() {
117117
let x = "a".to_string();
118-
x.eq_ignore_ascii_case("A");
118+
let _ = x.eq_ignore_ascii_case("A");
119119
}
120120

121121
// Shorthands used by the is_ascii_* tests.

library/std/src/collections/hash/set.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,8 @@ where
13201320
///
13211321
/// let mut intersection = a.intersection(&b);
13221322
/// ```
1323+
#[must_use = "this returns the intersection as an iterator, \
1324+
without modifying either input set"]
13231325
#[stable(feature = "rust1", since = "1.0.0")]
13241326
pub struct Intersection<'a, T: 'a, S: 'a> {
13251327
// iterator of the first set
@@ -1345,6 +1347,8 @@ pub struct Intersection<'a, T: 'a, S: 'a> {
13451347
///
13461348
/// let mut difference = a.difference(&b);
13471349
/// ```
1350+
#[must_use = "this returns the difference as an iterator, \
1351+
without modifying either input set"]
13481352
#[stable(feature = "rust1", since = "1.0.0")]
13491353
pub struct Difference<'a, T: 'a, S: 'a> {
13501354
// iterator of the first set
@@ -1370,6 +1374,8 @@ pub struct Difference<'a, T: 'a, S: 'a> {
13701374
///
13711375
/// let mut intersection = a.symmetric_difference(&b);
13721376
/// ```
1377+
#[must_use = "this returns the difference as an iterator, \
1378+
without modifying either input set"]
13731379
#[stable(feature = "rust1", since = "1.0.0")]
13741380
pub struct SymmetricDifference<'a, T: 'a, S: 'a> {
13751381
iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
@@ -1392,6 +1398,8 @@ pub struct SymmetricDifference<'a, T: 'a, S: 'a> {
13921398
///
13931399
/// let mut union_iter = a.union(&b);
13941400
/// ```
1401+
#[must_use = "this returns the union as an iterator, \
1402+
without modifying either input set"]
13951403
#[stable(feature = "rust1", since = "1.0.0")]
13961404
pub struct Union<'a, T: 'a, S: 'a> {
13971405
iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,

library/std/src/ffi/os_str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ impl OsStr {
829829
/// assert!(!non_ascii.is_ascii());
830830
/// ```
831831
#[stable(feature = "osstring_ascii", since = "1.53.0")]
832+
#[must_use]
832833
#[inline]
833834
pub fn is_ascii(&self) -> bool {
834835
self.inner.is_ascii()

library/std/src/io/stdio.rs

+1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ impl Stdin {
487487
/// println!("got a chunk: {}", String::from_utf8_lossy(&split.unwrap()));
488488
/// }
489489
/// ```
490+
#[must_use = "`self` will be dropped if the result is not used"]
490491
#[unstable(feature = "stdin_forwarders", issue = "87096")]
491492
pub fn split(self, byte: u8) -> Split<StdinLock<'static>> {
492493
self.into_locked().split(byte)

0 commit comments

Comments
 (0)