Skip to content

Commit

Permalink
Rollup merge of #89786 - jkugelman:must-use-len-and-is_empty, r=josht…
Browse files Browse the repository at this point in the history
…riplett

Add #[must_use] to len and is_empty

Parent issue: #89692

r? `@joshtriplett`
  • Loading branch information
matthiaskrgr authored Oct 31, 2021
2 parents 6c5aa76 + 6745e8d commit 88e5ae2
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 40 deletions.
2 changes: 2 additions & 0 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ impl<T> BinaryHeap<T> {
///
/// assert_eq!(heap.len(), 2);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.data.len()
Expand All @@ -1075,6 +1076,7 @@ impl<T> BinaryHeap<T> {
///
/// assert!(!heap.is_empty());
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool {
self.len() == 0
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,7 @@ impl<K, V> BTreeMap<K, V> {
/// a.insert(1, "a");
/// assert_eq!(a.len(), 1);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn len(&self) -> usize {
Expand All @@ -2232,6 +2233,7 @@ impl<K, V> BTreeMap<K, V> {
/// a.insert(1, "a");
/// assert!(!a.is_empty());
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn is_empty(&self) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ impl<T> BTreeSet<T> {
/// v.insert(1);
/// assert_eq!(v.len(), 1);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn len(&self) -> usize {
Expand All @@ -1064,6 +1065,7 @@ impl<T> BTreeSet<T> {
/// v.insert(1);
/// assert!(!v.is_empty());
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
pub const fn is_empty(&self) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/btree/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ fn test_send() {
#[test]
fn test_ord_absence() {
fn set<K>(mut set: BTreeSet<K>) {
set.is_empty();
set.len();
let _ = set.is_empty();
let _ = set.len();
set.clear();
let _ = set.iter();
let _ = set.into_iter();
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ impl<T> LinkedList<T> {
/// assert!(!dl.is_empty());
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool {
self.head.is_none()
Expand All @@ -609,6 +610,7 @@ impl<T> LinkedList<T> {
/// assert_eq!(dl.len(), 3);
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.len
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,7 @@ impl String {
/// assert_eq!(fancy_f.chars().count(), 3);
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.vec.len()
Expand All @@ -1566,6 +1567,7 @@ impl String {
/// assert!(!v.is_empty());
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool {
self.len() == 0
Expand Down
1 change: 1 addition & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ impl<T> NonNull<[T]> {
/// ```
#[unstable(feature = "slice_ptr_len", issue = "71146")]
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
#[must_use]
#[inline]
pub const fn len(self) -> usize {
self.as_ptr().len()
Expand Down
4 changes: 3 additions & 1 deletion library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl str {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_str_len", since = "1.39.0")]
#[must_use]
#[inline]
pub const fn len(&self) -> usize {
self.as_bytes().len()
Expand All @@ -158,9 +159,10 @@ impl str {
/// let s = "not empty";
/// assert!(!s.is_empty());
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_str_is_empty", since = "1.39.0")]
#[must_use]
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ impl OsStr {
/// assert!(!os_str.is_empty());
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[must_use]
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.inner.is_empty()
Expand Down Expand Up @@ -700,6 +701,7 @@ impl OsStr {
/// assert_eq!(os_str.len(), 3);
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
#[must_use]
#[inline]
pub fn len(&self) -> usize {
self.inner.inner.len()
Expand Down
1 change: 1 addition & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ impl Metadata {
/// Ok(())
/// }
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> u64 {
self.0.size()
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/os/unix/net/ancillary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,14 @@ impl<'a> SocketAncillary<'a> {
}

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

/// Returns the number of used bytes.
#[must_use]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn len(&self) -> usize {
self.length
Expand Down
7 changes: 4 additions & 3 deletions src/tools/clippy/tests/ui/iter_count.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl HasIter {
}
}

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

let _ = &vec[..].len();
&vec[..].len();
vec.len();
boxed_slice.len();
vec_deque.len();
Expand All @@ -62,13 +63,13 @@ fn main() {
binary_heap.len();

vec.len();
let _ = &vec[..].len();
&vec[..].len();
vec_deque.len();
hash_map.len();
b_tree_map.len();
linked_list.len();

let _ = &vec[..].len();
&vec[..].len();
vec.len();
vec_deque.len();
hash_set.len();
Expand Down
7 changes: 4 additions & 3 deletions src/tools/clippy/tests/ui/iter_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl HasIter {
}
}

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

let _ = &vec[..].iter().count();
&vec[..].iter().count();
vec.iter().count();
boxed_slice.iter().count();
vec_deque.iter().count();
Expand All @@ -62,13 +63,13 @@ fn main() {
binary_heap.iter().count();

vec.iter_mut().count();
let _ = &vec[..].iter_mut().count();
&vec[..].iter_mut().count();
vec_deque.iter_mut().count();
hash_map.iter_mut().count();
b_tree_map.iter_mut().count();
linked_list.iter_mut().count();

let _ = &vec[..].into_iter().count();
&vec[..].into_iter().count();
vec.into_iter().count();
vec_deque.into_iter().count();
hash_set.into_iter().count();
Expand Down
Loading

0 comments on commit 88e5ae2

Please sign in to comment.