Skip to content

Commit d04357a

Browse files
authored
Rollup merge of rust-lang#89786 - jkugelman:must-use-len-and-is_empty, r=joshtriplett
Add #[must_use] to len and is_empty Parent issue: rust-lang#89692 r? `@joshtriplett`
2 parents 88df718 + 64bb2c1 commit d04357a

File tree

11 files changed

+23
-5
lines changed

11 files changed

+23
-5
lines changed

library/alloc/src/collections/binary_heap.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ impl<T> BinaryHeap<T> {
10501050
///
10511051
/// assert_eq!(heap.len(), 2);
10521052
/// ```
1053+
#[must_use]
10531054
#[stable(feature = "rust1", since = "1.0.0")]
10541055
pub fn len(&self) -> usize {
10551056
self.data.len()
@@ -1073,6 +1074,7 @@ impl<T> BinaryHeap<T> {
10731074
///
10741075
/// assert!(!heap.is_empty());
10751076
/// ```
1077+
#[must_use]
10761078
#[stable(feature = "rust1", since = "1.0.0")]
10771079
pub fn is_empty(&self) -> bool {
10781080
self.len() == 0

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

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ impl<T> BTreeSet<T> {
10341034
/// v.insert(1);
10351035
/// assert_eq!(v.len(), 1);
10361036
/// ```
1037+
#[must_use]
10371038
#[stable(feature = "rust1", since = "1.0.0")]
10381039
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10391040
pub const fn len(&self) -> usize {
@@ -1052,6 +1053,7 @@ impl<T> BTreeSet<T> {
10521053
/// v.insert(1);
10531054
/// assert!(!v.is_empty());
10541055
/// ```
1056+
#[must_use]
10551057
#[stable(feature = "rust1", since = "1.0.0")]
10561058
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10571059
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
@@ -577,6 +577,7 @@ impl<T> LinkedList<T> {
577577
/// assert!(!dl.is_empty());
578578
/// ```
579579
#[inline]
580+
#[must_use]
580581
#[stable(feature = "rust1", since = "1.0.0")]
581582
pub fn is_empty(&self) -> bool {
582583
self.head.is_none()
@@ -603,6 +604,7 @@ impl<T> LinkedList<T> {
603604
/// assert_eq!(dl.len(), 3);
604605
/// ```
605606
#[inline]
607+
#[must_use]
606608
#[stable(feature = "rust1", since = "1.0.0")]
607609
pub fn len(&self) -> usize {
608610
self.len

library/alloc/src/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,7 @@ impl String {
15431543
/// assert_eq!(fancy_f.chars().count(), 3);
15441544
/// ```
15451545
#[inline]
1546+
#[must_use]
15461547
#[stable(feature = "rust1", since = "1.0.0")]
15471548
pub fn len(&self) -> usize {
15481549
self.vec.len()
@@ -1562,6 +1563,7 @@ impl String {
15621563
/// assert!(!v.is_empty());
15631564
/// ```
15641565
#[inline]
1566+
#[must_use]
15651567
#[stable(feature = "rust1", since = "1.0.0")]
15661568
pub fn is_empty(&self) -> bool {
15671569
self.len() == 0

library/core/src/ptr/non_null.rs

+1
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ impl<T> NonNull<[T]> {
443443
/// ```
444444
#[unstable(feature = "slice_ptr_len", issue = "71146")]
445445
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
446+
#[must_use]
446447
#[inline]
447448
pub const fn len(self) -> usize {
448449
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
@@ -663,6 +663,7 @@ impl OsStr {
663663
/// assert!(!os_str.is_empty());
664664
/// ```
665665
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
666+
#[must_use]
666667
#[inline]
667668
pub fn is_empty(&self) -> bool {
668669
self.inner.inner.is_empty()
@@ -694,6 +695,7 @@ impl OsStr {
694695
/// assert_eq!(os_str.len(), 3);
695696
/// ```
696697
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
698+
#[must_use]
697699
#[inline]
698700
pub fn len(&self) -> usize {
699701
self.inner.inner.len()

library/std/src/fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ impl Metadata {
10781078
/// Ok(())
10791079
/// }
10801080
/// ```
1081+
#[must_use]
10811082
#[stable(feature = "rust1", since = "1.0.0")]
10821083
pub fn len(&self) -> u64 {
10831084
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

0 commit comments

Comments
 (0)