Skip to content

Commit 6745e8d

Browse files
committed
Add #[must_use] to len and is_empty
1 parent 86d6d2b commit 6745e8d

File tree

14 files changed

+62
-42
lines changed

14 files changed

+62
-42
lines changed

library/alloc/src/collections/binary_heap.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ impl<T> BinaryHeap<T> {
10471047
///
10481048
/// assert_eq!(heap.len(), 2);
10491049
/// ```
1050+
#[must_use]
10501051
#[stable(feature = "rust1", since = "1.0.0")]
10511052
pub fn len(&self) -> usize {
10521053
self.data.len()
@@ -1070,6 +1071,7 @@ impl<T> BinaryHeap<T> {
10701071
///
10711072
/// assert!(!heap.is_empty());
10721073
/// ```
1074+
#[must_use]
10731075
#[stable(feature = "rust1", since = "1.0.0")]
10741076
pub fn is_empty(&self) -> bool {
10751077
self.len() == 0

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

+2
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,7 @@ impl<K, V> BTreeMap<K, V> {
22042204
/// a.insert(1, "a");
22052205
/// assert_eq!(a.len(), 1);
22062206
/// ```
2207+
#[must_use]
22072208
#[stable(feature = "rust1", since = "1.0.0")]
22082209
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
22092210
pub const fn len(&self) -> usize {
@@ -2224,6 +2225,7 @@ impl<K, V> BTreeMap<K, V> {
22242225
/// a.insert(1, "a");
22252226
/// assert!(!a.is_empty());
22262227
/// ```
2228+
#[must_use]
22272229
#[stable(feature = "rust1", since = "1.0.0")]
22282230
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
22292231
pub const fn is_empty(&self) -> bool {

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

+2
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ impl<T> BTreeSet<T> {
10331033
/// v.insert(1);
10341034
/// assert_eq!(v.len(), 1);
10351035
/// ```
1036+
#[must_use]
10361037
#[stable(feature = "rust1", since = "1.0.0")]
10371038
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10381039
pub const fn len(&self) -> usize {
@@ -1051,6 +1052,7 @@ impl<T> BTreeSet<T> {
10511052
/// v.insert(1);
10521053
/// assert!(!v.is_empty());
10531054
/// ```
1055+
#[must_use]
10541056
#[stable(feature = "rust1", since = "1.0.0")]
10551057
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10561058
pub const fn is_empty(&self) -> bool {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,11 @@ 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();
616-
set.iter();
617-
set.into_iter();
616+
let _ = set.iter();
617+
let _ = set.into_iter();
618618
}
619619

620620
fn set_debug<K: Debug>(set: BTreeSet<K>) {

library/alloc/src/collections/linked_list.rs

+2
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ impl<T> LinkedList<T> {
576576
/// assert!(!dl.is_empty());
577577
/// ```
578578
#[inline]
579+
#[must_use]
579580
#[stable(feature = "rust1", since = "1.0.0")]
580581
pub fn is_empty(&self) -> bool {
581582
self.head.is_none()
@@ -602,6 +603,7 @@ impl<T> LinkedList<T> {
602603
/// assert_eq!(dl.len(), 3);
603604
/// ```
604605
#[inline]
606+
#[must_use]
605607
#[stable(feature = "rust1", since = "1.0.0")]
606608
pub fn len(&self) -> usize {
607609
self.len

library/alloc/src/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ impl String {
15371537
/// assert_eq!(fancy_f.chars().count(), 3);
15381538
/// ```
15391539
#[inline]
1540+
#[must_use]
15401541
#[stable(feature = "rust1", since = "1.0.0")]
15411542
pub fn len(&self) -> usize {
15421543
self.vec.len()
@@ -1556,6 +1557,7 @@ impl String {
15561557
/// assert!(!v.is_empty());
15571558
/// ```
15581559
#[inline]
1560+
#[must_use]
15591561
#[stable(feature = "rust1", since = "1.0.0")]
15601562
pub fn is_empty(&self) -> bool {
15611563
self.len() == 0

library/core/src/ptr/non_null.rs

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ impl<T> NonNull<[T]> {
438438
/// ```
439439
#[unstable(feature = "slice_ptr_len", issue = "71146")]
440440
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
441+
#[must_use]
441442
#[inline]
442443
pub const fn len(self) -> usize {
443444
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
@@ -660,6 +660,7 @@ impl OsStr {
660660
/// assert!(!os_str.is_empty());
661661
/// ```
662662
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
663+
#[must_use]
663664
#[inline]
664665
pub fn is_empty(&self) -> bool {
665666
self.inner.inner.is_empty()
@@ -691,6 +692,7 @@ impl OsStr {
691692
/// assert_eq!(os_str.len(), 3);
692693
/// ```
693694
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
695+
#[must_use]
694696
#[inline]
695697
pub fn len(&self) -> usize {
696698
self.inner.inner.len()

library/std/src/fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ impl Metadata {
10771077
/// Ok(())
10781078
/// }
10791079
/// ```
1080+
#[must_use]
10801081
#[stable(feature = "rust1", since = "1.0.0")]
10811082
pub fn len(&self) -> u64 {
10821083
self.0.size()

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

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

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

438439
/// Returns the number of used bytes.
440+
#[must_use]
439441
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
440442
pub fn len(&self) -> usize {
441443
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)