Skip to content

Commit 88e5ae2

Browse files
authored
Rollup merge of #89786 - jkugelman:must-use-len-and-is_empty, r=joshtriplett
Add #[must_use] to len and is_empty Parent issue: #89692 r? `@joshtriplett`
2 parents 6c5aa76 + 6745e8d commit 88e5ae2

File tree

14 files changed

+60
-40
lines changed

14 files changed

+60
-40
lines changed

library/alloc/src/collections/binary_heap.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ impl<T> BinaryHeap<T> {
10521052
///
10531053
/// assert_eq!(heap.len(), 2);
10541054
/// ```
1055+
#[must_use]
10551056
#[stable(feature = "rust1", since = "1.0.0")]
10561057
pub fn len(&self) -> usize {
10571058
self.data.len()
@@ -1075,6 +1076,7 @@ impl<T> BinaryHeap<T> {
10751076
///
10761077
/// assert!(!heap.is_empty());
10771078
/// ```
1079+
#[must_use]
10781080
#[stable(feature = "rust1", since = "1.0.0")]
10791081
pub fn is_empty(&self) -> bool {
10801082
self.len() == 0

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

+2
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,7 @@ impl<K, V> BTreeMap<K, V> {
22122212
/// a.insert(1, "a");
22132213
/// assert_eq!(a.len(), 1);
22142214
/// ```
2215+
#[must_use]
22152216
#[stable(feature = "rust1", since = "1.0.0")]
22162217
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
22172218
pub const fn len(&self) -> usize {
@@ -2232,6 +2233,7 @@ impl<K, V> BTreeMap<K, V> {
22322233
/// a.insert(1, "a");
22332234
/// assert!(!a.is_empty());
22342235
/// ```
2236+
#[must_use]
22352237
#[stable(feature = "rust1", since = "1.0.0")]
22362238
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
22372239
pub const fn is_empty(&self) -> bool {

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

+2
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ impl<T> BTreeSet<T> {
10461046
/// v.insert(1);
10471047
/// assert_eq!(v.len(), 1);
10481048
/// ```
1049+
#[must_use]
10491050
#[stable(feature = "rust1", since = "1.0.0")]
10501051
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10511052
pub const fn len(&self) -> usize {
@@ -1064,6 +1065,7 @@ impl<T> BTreeSet<T> {
10641065
/// v.insert(1);
10651066
/// assert!(!v.is_empty());
10661067
/// ```
1068+
#[must_use]
10671069
#[stable(feature = "rust1", since = "1.0.0")]
10681070
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10691071
pub const fn is_empty(&self) -> bool {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,8 @@ fn test_send() {
610610
#[test]
611611
fn test_ord_absence() {
612612
fn set<K>(mut set: BTreeSet<K>) {
613-
set.is_empty();
614-
set.len();
613+
let _ = set.is_empty();
614+
let _ = set.len();
615615
set.clear();
616616
let _ = set.iter();
617617
let _ = set.into_iter();

library/alloc/src/collections/linked_list.rs

+2
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ impl<T> LinkedList<T> {
583583
/// assert!(!dl.is_empty());
584584
/// ```
585585
#[inline]
586+
#[must_use]
586587
#[stable(feature = "rust1", since = "1.0.0")]
587588
pub fn is_empty(&self) -> bool {
588589
self.head.is_none()
@@ -609,6 +610,7 @@ impl<T> LinkedList<T> {
609610
/// assert_eq!(dl.len(), 3);
610611
/// ```
611612
#[inline]
613+
#[must_use]
612614
#[stable(feature = "rust1", since = "1.0.0")]
613615
pub fn len(&self) -> usize {
614616
self.len

library/alloc/src/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,7 @@ impl String {
15471547
/// assert_eq!(fancy_f.chars().count(), 3);
15481548
/// ```
15491549
#[inline]
1550+
#[must_use]
15501551
#[stable(feature = "rust1", since = "1.0.0")]
15511552
pub fn len(&self) -> usize {
15521553
self.vec.len()
@@ -1566,6 +1567,7 @@ impl String {
15661567
/// assert!(!v.is_empty());
15671568
/// ```
15681569
#[inline]
1570+
#[must_use]
15691571
#[stable(feature = "rust1", since = "1.0.0")]
15701572
pub fn is_empty(&self) -> bool {
15711573
self.len() == 0

library/core/src/ptr/non_null.rs

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ impl<T> NonNull<[T]> {
449449
/// ```
450450
#[unstable(feature = "slice_ptr_len", issue = "71146")]
451451
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
452+
#[must_use]
452453
#[inline]
453454
pub const fn len(self) -> usize {
454455
self.as_ptr().len()

library/core/src/str/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl str {
140140
/// ```
141141
#[stable(feature = "rust1", since = "1.0.0")]
142142
#[rustc_const_stable(feature = "const_str_len", since = "1.39.0")]
143+
#[must_use]
143144
#[inline]
144145
pub const fn len(&self) -> usize {
145146
self.as_bytes().len()
@@ -158,9 +159,10 @@ impl str {
158159
/// let s = "not empty";
159160
/// assert!(!s.is_empty());
160161
/// ```
161-
#[inline]
162162
#[stable(feature = "rust1", since = "1.0.0")]
163163
#[rustc_const_stable(feature = "const_str_is_empty", since = "1.39.0")]
164+
#[must_use]
165+
#[inline]
164166
pub const fn is_empty(&self) -> bool {
165167
self.len() == 0
166168
}

library/std/src/ffi/os_str.rs

+2
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ impl OsStr {
669669
/// assert!(!os_str.is_empty());
670670
/// ```
671671
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
672+
#[must_use]
672673
#[inline]
673674
pub fn is_empty(&self) -> bool {
674675
self.inner.inner.is_empty()
@@ -700,6 +701,7 @@ impl OsStr {
700701
/// assert_eq!(os_str.len(), 3);
701702
/// ```
702703
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
704+
#[must_use]
703705
#[inline]
704706
pub fn len(&self) -> usize {
705707
self.inner.inner.len()

library/std/src/fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ impl Metadata {
10801080
/// Ok(())
10811081
/// }
10821082
/// ```
1083+
#[must_use]
10831084
#[stable(feature = "rust1", since = "1.0.0")]
10841085
pub fn len(&self) -> u64 {
10851086
self.0.size()

library/std/src/os/unix/net/ancillary.rs

+2
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,14 @@ impl<'a> SocketAncillary<'a> {
431431
}
432432

433433
/// Returns `true` if the ancillary data is empty.
434+
#[must_use]
434435
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
435436
pub fn is_empty(&self) -> bool {
436437
self.length == 0
437438
}
438439

439440
/// Returns the number of used bytes.
441+
#[must_use]
440442
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
441443
pub fn len(&self) -> usize {
442444
self.length

src/tools/clippy/tests/ui/iter_count.fixed

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl HasIter {
3333
}
3434
}
3535

36+
#[allow(unused_must_use)]
3637
fn main() {
3738
let mut vec = vec![0, 1, 2, 3];
3839
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
@@ -50,7 +51,7 @@ fn main() {
5051
linked_list.push_back(1);
5152
binary_heap.push(1);
5253

53-
let _ = &vec[..].len();
54+
&vec[..].len();
5455
vec.len();
5556
boxed_slice.len();
5657
vec_deque.len();
@@ -62,13 +63,13 @@ fn main() {
6263
binary_heap.len();
6364

6465
vec.len();
65-
let _ = &vec[..].len();
66+
&vec[..].len();
6667
vec_deque.len();
6768
hash_map.len();
6869
b_tree_map.len();
6970
linked_list.len();
7071

71-
let _ = &vec[..].len();
72+
&vec[..].len();
7273
vec.len();
7374
vec_deque.len();
7475
hash_set.len();

src/tools/clippy/tests/ui/iter_count.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl HasIter {
3333
}
3434
}
3535

36+
#[allow(unused_must_use)]
3637
fn main() {
3738
let mut vec = vec![0, 1, 2, 3];
3839
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
@@ -50,7 +51,7 @@ fn main() {
5051
linked_list.push_back(1);
5152
binary_heap.push(1);
5253

53-
let _ = &vec[..].iter().count();
54+
&vec[..].iter().count();
5455
vec.iter().count();
5556
boxed_slice.iter().count();
5657
vec_deque.iter().count();
@@ -62,13 +63,13 @@ fn main() {
6263
binary_heap.iter().count();
6364

6465
vec.iter_mut().count();
65-
let _ = &vec[..].iter_mut().count();
66+
&vec[..].iter_mut().count();
6667
vec_deque.iter_mut().count();
6768
hash_map.iter_mut().count();
6869
b_tree_map.iter_mut().count();
6970
linked_list.iter_mut().count();
7071

71-
let _ = &vec[..].into_iter().count();
72+
&vec[..].into_iter().count();
7273
vec.into_iter().count();
7374
vec_deque.into_iter().count();
7475
hash_set.into_iter().count();

0 commit comments

Comments
 (0)