Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #[must_use] to len and is_empty #89786

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
@@ -1047,6 +1047,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()
@@ -1070,6 +1071,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
2 changes: 2 additions & 0 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
@@ -2204,6 +2204,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 {
@@ -2224,6 +2225,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 {
2 changes: 2 additions & 0 deletions library/alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
@@ -1033,6 +1033,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 {
@@ -1051,6 +1052,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 {
8 changes: 4 additions & 4 deletions library/alloc/src/collections/btree/set/tests.rs
Original file line number Diff line number Diff line change
@@ -610,11 +610,11 @@ 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();
set.iter();
set.into_iter();
let _ = set.iter();
let _ = set.into_iter();
}

fn set_debug<K: Debug>(set: BTreeSet<K>) {
2 changes: 2 additions & 0 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
@@ -576,6 +576,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()
@@ -602,6 +603,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
2 changes: 2 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
@@ -1537,6 +1537,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()
@@ -1556,6 +1557,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
1 change: 1 addition & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
@@ -438,6 +438,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()
4 changes: 3 additions & 1 deletion library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
@@ -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()
@@ -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
}
2 changes: 2 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
@@ -660,6 +660,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()
@@ -691,6 +692,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()
1 change: 1 addition & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1077,6 +1077,7 @@ impl Metadata {
/// Ok(())
/// }
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> u64 {
self.0.size()
2 changes: 2 additions & 0 deletions library/std/src/os/unix/net/ancillary.rs
Original file line number Diff line number Diff line change
@@ -430,12 +430,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
7 changes: 4 additions & 3 deletions src/tools/clippy/tests/ui/iter_count.fixed
Original file line number Diff line number Diff line change
@@ -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]);
@@ -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();
@@ -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();
7 changes: 4 additions & 3 deletions src/tools/clippy/tests/ui/iter_count.rs
Original file line number Diff line number Diff line change
@@ -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]);
@@ -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();
@@ -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();
Loading
Oops, something went wrong.