From 17f418155e0bdb84c89b1c28eb6c86503d4cbefd Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 18 Sep 2021 14:47:57 +0200 Subject: [PATCH 1/7] Use rtabort! instead of rtprintpanic! + abort_internal --- library/std/src/rt.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index 4d72aff0116..88ee6f9b129 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -128,8 +128,7 @@ fn lang_start_internal( let ret_code = panic::catch_unwind(move || panic::catch_unwind(main).unwrap_or(101) as isize) .map_err(move |e| { mem::forget(e); - rtprintpanic!("drop of the panic payload panicked"); - sys::abort_internal() + rtabort!("drop of the panic payload panicked"); }); panic::catch_unwind(cleanup).map_err(rt_abort)?; ret_code From bf9e6e5598fb714a2b2ba971f8a7404a877d7568 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 18 Sep 2021 14:48:32 +0200 Subject: [PATCH 2/7] Re-export io::stdio::cleanup instead of wrap it --- library/std/src/io/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index f8ebbe1cba7..399445b121f 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -299,9 +299,7 @@ mod util; const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; -pub(crate) fn cleanup() { - stdio::cleanup() -} +pub(crate) use stdio::cleanup; struct Guard<'a> { buf: &'a mut Vec, From d2c83774d333a94bde537e049d05a98c954857ee Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 8 Oct 2021 13:29:03 +0200 Subject: [PATCH 3/7] Let stack_overflow::imp::cleanup call drop_handler directly instead of through the Drop impl for Handler --- library/std/src/sys/unix/stack_overflow.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/library/std/src/sys/unix/stack_overflow.rs b/library/std/src/sys/unix/stack_overflow.rs index e8747e39bcb..fa3791566b7 100644 --- a/library/std/src/sys/unix/stack_overflow.rs +++ b/library/std/src/sys/unix/stack_overflow.rs @@ -6,7 +6,7 @@ pub use self::imp::cleanup; pub use self::imp::init; pub struct Handler { - _data: *mut libc::c_void, + data: *mut libc::c_void, } impl Handler { @@ -15,14 +15,14 @@ impl Handler { } fn null() -> Handler { - Handler { _data: crate::ptr::null_mut() } + Handler { data: crate::ptr::null_mut() } } } impl Drop for Handler { fn drop(&mut self) { unsafe { - drop_handler(self); + drop_handler(self.data); } } } @@ -134,12 +134,12 @@ mod imp { } let handler = make_handler(); - MAIN_ALTSTACK.store(handler._data, Ordering::Relaxed); + MAIN_ALTSTACK.store(handler.data, Ordering::Relaxed); mem::forget(handler); } pub unsafe fn cleanup() { - Handler { _data: MAIN_ALTSTACK.load(Ordering::Relaxed) }; + drop_handler(MAIN_ALTSTACK.load(Ordering::Relaxed)); } unsafe fn get_stackp() -> *mut libc::c_void { @@ -175,14 +175,14 @@ mod imp { if stack.ss_flags & SS_DISABLE != 0 { stack = get_stack(); sigaltstack(&stack, ptr::null_mut()); - Handler { _data: stack.ss_sp as *mut libc::c_void } + Handler { data: stack.ss_sp as *mut libc::c_void } } else { Handler::null() } } - pub unsafe fn drop_handler(handler: &mut Handler) { - if !handler._data.is_null() { + pub unsafe fn drop_handler(data: *mut libc::c_void) { + if !data.is_null() { let stack = libc::stack_t { ss_sp: ptr::null_mut(), ss_flags: SS_DISABLE, @@ -195,7 +195,7 @@ mod imp { sigaltstack(&stack, ptr::null_mut()); // We know from `get_stackp` that the alternate stack we installed is part of a mapping // that started one page earlier, so walk back a page and unmap from there. - munmap(handler._data.sub(page_size()), SIGSTKSZ + page_size()); + munmap(data.sub(page_size()), SIGSTKSZ + page_size()); } } } @@ -219,5 +219,5 @@ mod imp { super::Handler::null() } - pub unsafe fn drop_handler(_handler: &mut super::Handler) {} + pub unsafe fn drop_handler(_data: *mut libc::c_void) {} } From 0cc4cce057c0272e1997fc1d684c0b5d417eac4d Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 8 Oct 2021 13:56:47 +0200 Subject: [PATCH 4/7] Update test expectation --- src/test/ui/rt-explody-panic-payloads.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/rt-explody-panic-payloads.rs b/src/test/ui/rt-explody-panic-payloads.rs index 1d3a2ff8284..dc193582c6a 100644 --- a/src/test/ui/rt-explody-panic-payloads.rs +++ b/src/test/ui/rt-explody-panic-payloads.rs @@ -25,6 +25,6 @@ fn main() { println!("{:#?}", output); let stderr = std::str::from_utf8(&output.stderr); assert!(stderr.map(|v| { - v.ends_with("drop of the panic payload panicked") + v.ends_with("fatal runtime error: drop of the panic payload panicked\n") }).unwrap_or(false)); } From 6745e8da06e1fd313d2ff20ff9fe8d8dc4714479 Mon Sep 17 00:00:00 2001 From: John Kugelman Date: Mon, 11 Oct 2021 16:15:50 -0400 Subject: [PATCH 5/7] Add #[must_use] to len and is_empty --- library/alloc/src/collections/binary_heap.rs | 2 + library/alloc/src/collections/btree/map.rs | 2 + library/alloc/src/collections/btree/set.rs | 2 + .../alloc/src/collections/btree/set/tests.rs | 8 +-- library/alloc/src/collections/linked_list.rs | 2 + library/alloc/src/string.rs | 2 + library/core/src/ptr/non_null.rs | 1 + library/core/src/str/mod.rs | 4 +- library/std/src/ffi/os_str.rs | 2 + library/std/src/fs.rs | 1 + library/std/src/os/unix/net/ancillary.rs | 2 + src/tools/clippy/tests/ui/iter_count.fixed | 7 ++- src/tools/clippy/tests/ui/iter_count.rs | 7 ++- src/tools/clippy/tests/ui/iter_count.stderr | 62 +++++++++---------- 14 files changed, 62 insertions(+), 42 deletions(-) diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index 75720a970a3..5881f6227a0 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -1047,6 +1047,7 @@ impl BinaryHeap { /// /// 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 BinaryHeap { /// /// assert!(!heap.is_empty()); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn is_empty(&self) -> bool { self.len() == 0 diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index e9265262c8b..a6ec5a83f14 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -2204,6 +2204,7 @@ impl BTreeMap { /// 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 BTreeMap { /// 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 { diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index f4c10100982..2ddebd416cb 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -1033,6 +1033,7 @@ impl BTreeSet { /// 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 BTreeSet { /// 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 { diff --git a/library/alloc/src/collections/btree/set/tests.rs b/library/alloc/src/collections/btree/set/tests.rs index 0a87ae12d61..2fc17a7c860 100644 --- a/library/alloc/src/collections/btree/set/tests.rs +++ b/library/alloc/src/collections/btree/set/tests.rs @@ -610,11 +610,11 @@ fn test_send() { #[test] fn test_ord_absence() { fn set(mut set: BTreeSet) { - 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(set: BTreeSet) { diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs index a769c558b4f..bd00a56ac6e 100644 --- a/library/alloc/src/collections/linked_list.rs +++ b/library/alloc/src/collections/linked_list.rs @@ -576,6 +576,7 @@ impl LinkedList { /// 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 LinkedList { /// assert_eq!(dl.len(), 3); /// ``` #[inline] + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn len(&self) -> usize { self.len diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index d00792b9c3e..651e55cc094 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -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 diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 87c8674af0d..1821954edd5 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -438,6 +438,7 @@ impl 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() diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 14ce94c178c..a6ba019f368 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -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 } diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index f5cef60e126..ca0f27b285f 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -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() diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 726c855c4fd..54b998d12ac 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1077,6 +1077,7 @@ impl Metadata { /// Ok(()) /// } /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn len(&self) -> u64 { self.0.size() diff --git a/library/std/src/os/unix/net/ancillary.rs b/library/std/src/os/unix/net/ancillary.rs index 1f9d42812ec..0ed3018c802 100644 --- a/library/std/src/os/unix/net/ancillary.rs +++ b/library/std/src/os/unix/net/ancillary.rs @@ -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 diff --git a/src/tools/clippy/tests/ui/iter_count.fixed b/src/tools/clippy/tests/ui/iter_count.fixed index 97c5929783d..90a6eef7526 100644 --- a/src/tools/clippy/tests/ui/iter_count.fixed +++ b/src/tools/clippy/tests/ui/iter_count.fixed @@ -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(); diff --git a/src/tools/clippy/tests/ui/iter_count.rs b/src/tools/clippy/tests/ui/iter_count.rs index 70bb734763f..6681a480a28 100644 --- a/src/tools/clippy/tests/ui/iter_count.rs +++ b/src/tools/clippy/tests/ui/iter_count.rs @@ -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(); diff --git a/src/tools/clippy/tests/ui/iter_count.stderr b/src/tools/clippy/tests/ui/iter_count.stderr index 1d2c22f9dfa..2e3d7fc35de 100644 --- a/src/tools/clippy/tests/ui/iter_count.stderr +++ b/src/tools/clippy/tests/ui/iter_count.stderr @@ -1,151 +1,151 @@ error: called `.iter().count()` on a `slice` - --> $DIR/iter_count.rs:53:14 + --> $DIR/iter_count.rs:54:6 | -LL | let _ = &vec[..].iter().count(); - | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` +LL | &vec[..].iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` | = note: `-D clippy::iter-count` implied by `-D warnings` error: called `.iter().count()` on a `Vec` - --> $DIR/iter_count.rs:54:5 + --> $DIR/iter_count.rs:55:5 | LL | vec.iter().count(); | ^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.iter().count()` on a `slice` - --> $DIR/iter_count.rs:55:5 + --> $DIR/iter_count.rs:56:5 | LL | boxed_slice.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `boxed_slice.len()` error: called `.iter().count()` on a `VecDeque` - --> $DIR/iter_count.rs:56:5 + --> $DIR/iter_count.rs:57:5 | LL | vec_deque.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` error: called `.iter().count()` on a `HashSet` - --> $DIR/iter_count.rs:57:5 + --> $DIR/iter_count.rs:58:5 | LL | hash_set.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_set.len()` error: called `.iter().count()` on a `HashMap` - --> $DIR/iter_count.rs:58:5 + --> $DIR/iter_count.rs:59:5 | LL | hash_map.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()` error: called `.iter().count()` on a `BTreeMap` - --> $DIR/iter_count.rs:59:5 + --> $DIR/iter_count.rs:60:5 | LL | b_tree_map.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()` error: called `.iter().count()` on a `BTreeSet` - --> $DIR/iter_count.rs:60:5 + --> $DIR/iter_count.rs:61:5 | LL | b_tree_set.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_set.len()` error: called `.iter().count()` on a `LinkedList` - --> $DIR/iter_count.rs:61:5 + --> $DIR/iter_count.rs:62:5 | LL | linked_list.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()` error: called `.iter().count()` on a `BinaryHeap` - --> $DIR/iter_count.rs:62:5 + --> $DIR/iter_count.rs:63:5 | LL | binary_heap.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `binary_heap.len()` error: called `.iter_mut().count()` on a `Vec` - --> $DIR/iter_count.rs:64:5 + --> $DIR/iter_count.rs:65:5 | LL | vec.iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.iter_mut().count()` on a `slice` - --> $DIR/iter_count.rs:65:14 + --> $DIR/iter_count.rs:66:6 | -LL | let _ = &vec[..].iter_mut().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` +LL | &vec[..].iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` error: called `.iter_mut().count()` on a `VecDeque` - --> $DIR/iter_count.rs:66:5 + --> $DIR/iter_count.rs:67:5 | LL | vec_deque.iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` error: called `.iter_mut().count()` on a `HashMap` - --> $DIR/iter_count.rs:67:5 + --> $DIR/iter_count.rs:68:5 | LL | hash_map.iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()` error: called `.iter_mut().count()` on a `BTreeMap` - --> $DIR/iter_count.rs:68:5 + --> $DIR/iter_count.rs:69:5 | LL | b_tree_map.iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()` error: called `.iter_mut().count()` on a `LinkedList` - --> $DIR/iter_count.rs:69:5 + --> $DIR/iter_count.rs:70:5 | LL | linked_list.iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()` error: called `.into_iter().count()` on a `slice` - --> $DIR/iter_count.rs:71:14 + --> $DIR/iter_count.rs:72:6 | -LL | let _ = &vec[..].into_iter().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` +LL | &vec[..].into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` error: called `.into_iter().count()` on a `Vec` - --> $DIR/iter_count.rs:72:5 + --> $DIR/iter_count.rs:73:5 | LL | vec.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.into_iter().count()` on a `VecDeque` - --> $DIR/iter_count.rs:73:5 + --> $DIR/iter_count.rs:74:5 | LL | vec_deque.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` error: called `.into_iter().count()` on a `HashSet` - --> $DIR/iter_count.rs:74:5 + --> $DIR/iter_count.rs:75:5 | LL | hash_set.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_set.len()` error: called `.into_iter().count()` on a `HashMap` - --> $DIR/iter_count.rs:75:5 + --> $DIR/iter_count.rs:76:5 | LL | hash_map.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()` error: called `.into_iter().count()` on a `BTreeMap` - --> $DIR/iter_count.rs:76:5 + --> $DIR/iter_count.rs:77:5 | LL | b_tree_map.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()` error: called `.into_iter().count()` on a `BTreeSet` - --> $DIR/iter_count.rs:77:5 + --> $DIR/iter_count.rs:78:5 | LL | b_tree_set.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_set.len()` error: called `.into_iter().count()` on a `LinkedList` - --> $DIR/iter_count.rs:78:5 + --> $DIR/iter_count.rs:79:5 | LL | linked_list.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()` error: called `.into_iter().count()` on a `BinaryHeap` - --> $DIR/iter_count.rs:79:5 + --> $DIR/iter_count.rs:80:5 | LL | binary_heap.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `binary_heap.len()` From a81d4b18ea7ee03733e983974400816684f78ebe Mon Sep 17 00:00:00 2001 From: John Kugelman Date: Sat, 30 Oct 2021 23:37:32 -0400 Subject: [PATCH 6/7] Add #[must_use] to remaining std functions (O-Z) --- library/std/src/os/unix/net/ancillary.rs | 6 ++++++ library/std/src/os/unix/net/listener.rs | 1 + library/std/src/os/unix/process.rs | 1 + library/std/src/panicking.rs | 3 +++ library/std/src/path.rs | 15 +++++++++++++++ library/std/src/process.rs | 13 +++++++++++++ library/std/src/sync/condvar.rs | 1 + library/std/src/sync/mpsc/mod.rs | 2 ++ library/std/src/sys_common/process.rs | 1 + library/std/src/thread/mod.rs | 5 +++++ library/std/src/time.rs | 7 +++++++ library/std/src/time/tests.rs | 4 ++-- 12 files changed, 57 insertions(+), 2 deletions(-) diff --git a/library/std/src/os/unix/net/ancillary.rs b/library/std/src/os/unix/net/ancillary.rs index 57bb61903c1..00f973b5e46 100644 --- a/library/std/src/os/unix/net/ancillary.rs +++ b/library/std/src/os/unix/net/ancillary.rs @@ -201,6 +201,7 @@ impl SocketCred { } /// Get the current PID. + #[must_use] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] pub fn get_pid(&self) -> libc::pid_t { self.0.pid @@ -213,6 +214,7 @@ impl SocketCred { } /// Get the current UID. + #[must_use] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] pub fn get_uid(&self) -> libc::uid_t { self.0.uid @@ -225,6 +227,7 @@ impl SocketCred { } /// Get the current GID. + #[must_use] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] pub fn get_gid(&self) -> libc::gid_t { self.0.gid @@ -330,6 +333,7 @@ impl<'a> AncillaryData<'a> { } /// This struct is used to iterate through the control messages. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] pub struct Messages<'a> { buffer: &'a [u8], @@ -425,6 +429,7 @@ impl<'a> SocketAncillary<'a> { } /// Returns the capacity of the buffer. + #[must_use] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] pub fn capacity(&self) -> usize { self.buffer.len() @@ -471,6 +476,7 @@ impl<'a> SocketAncillary<'a> { /// Ok(()) /// } /// ``` + #[must_use] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] pub fn truncated(&self) -> bool { self.truncated diff --git a/library/std/src/os/unix/net/listener.rs b/library/std/src/os/unix/net/listener.rs index 97348afe7de..b23dd6062f6 100644 --- a/library/std/src/os/unix/net/listener.rs +++ b/library/std/src/os/unix/net/listener.rs @@ -365,6 +365,7 @@ impl<'a> IntoIterator for &'a UnixListener { /// } /// ``` #[derive(Debug)] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "unix_socket", since = "1.10.0")] pub struct Incoming<'a> { listener: &'a UnixListener, diff --git a/library/std/src/os/unix/process.rs b/library/std/src/os/unix/process.rs index 9b94615d247..286a7c3b386 100644 --- a/library/std/src/os/unix/process.rs +++ b/library/std/src/os/unix/process.rs @@ -436,6 +436,7 @@ impl From for OwnedFd { } /// Returns the OS-assigned process identifier associated with this process's parent. +#[must_use] #[stable(feature = "unix_ppid", since = "1.27.0")] pub fn parent_id() -> u32 { crate::sys::os::getppid() diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 437fcbb3176..6fc6b8daec0 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -160,6 +160,7 @@ pub fn set_hook(hook: Box) + 'static + Sync + Send>) { /// /// panic!("Normal panic"); /// ``` +#[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn take_hook() -> Box) + 'static + Sync + Send> { if thread::panicking() { @@ -284,11 +285,13 @@ pub mod panic_count { } // Disregards ALWAYS_ABORT_FLAG + #[must_use] pub fn get_count() -> usize { LOCAL_PANIC_COUNT.with(|c| c.get()) } // Disregards ALWAYS_ABORT_FLAG + #[must_use] #[inline] pub fn count_is_zero() -> bool { if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) & !ALWAYS_ABORT_FLAG == 0 { diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 8f00d2260e4..4672fea1fdd 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -422,6 +422,7 @@ impl<'a> PrefixComponent<'a> { /// See [`Prefix`]'s documentation for more information on the different /// kinds of prefixes. #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub fn kind(&self) -> Prefix<'a> { self.parsed @@ -583,6 +584,7 @@ impl AsRef for Component<'_> { /// /// [`components`]: Path::components #[derive(Clone)] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Components<'a> { // The path left to parse components from @@ -609,6 +611,7 @@ pub struct Components<'a> { /// /// [`iter`]: Path::iter #[derive(Clone)] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a> { inner: Components<'a>, @@ -1051,6 +1054,7 @@ fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cm /// /// [`ancestors`]: Path::ancestors #[derive(Copy, Clone, Debug)] +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "path_ancestors", since = "1.28.0")] pub struct Ancestors<'a> { next: Option<&'a Path>, @@ -1459,6 +1463,7 @@ impl PathBuf { /// /// [`capacity`]: OsString::capacity #[stable(feature = "path_buf_capacity", since = "1.44.0")] + #[must_use] #[inline] pub fn capacity(&self) -> usize { self.inner.capacity() @@ -2103,6 +2108,7 @@ impl Path { /// assert_eq!(grand_parent.parent(), None); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn parent(&self) -> Option<&Path> { let mut comps = self.components(); let comp = comps.next_back(); @@ -2169,6 +2175,7 @@ impl Path { /// assert_eq!(None, Path::new("/").file_name()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn file_name(&self) -> Option<&OsStr> { self.components().next_back().and_then(|p| match p { Component::Normal(p) => Some(p), @@ -2241,6 +2248,7 @@ impl Path { /// assert!(!Path::new("/etc/foo.rs").starts_with("/etc/foo")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn starts_with>(&self, base: P) -> bool { self._starts_with(base.as_ref()) } @@ -2268,6 +2276,7 @@ impl Path { /// assert!(!path.ends_with("conf")); // use .extension() instead /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn ends_with>(&self, child: P) -> bool { self._ends_with(child.as_ref()) } @@ -2303,6 +2312,7 @@ impl Path { /// [`Path::file_prefix`]: Path::file_prefix /// #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn file_stem(&self) -> Option<&OsStr> { self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after)) } @@ -2336,6 +2346,7 @@ impl Path { /// [`Path::file_stem`]: Path::file_stem /// #[unstable(feature = "path_file_prefix", issue = "86319")] + #[must_use] pub fn file_prefix(&self) -> Option<&OsStr> { self.file_name().map(split_file_at_dot).and_then(|(before, _after)| Some(before)) } @@ -2360,6 +2371,7 @@ impl Path { /// assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn extension(&self) -> Option<&OsStr> { self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.and(after)) } @@ -2403,6 +2415,7 @@ impl Path { /// assert_eq!(path.with_file_name("var"), PathBuf::from("/var")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn with_file_name>(&self, file_name: S) -> PathBuf { self._with_file_name(file_name.as_ref()) } @@ -2660,6 +2673,7 @@ impl Path { /// This is a convenience function that coerces errors to false. If you want to /// check errors, call [`fs::metadata`]. #[stable(feature = "path_ext", since = "1.5.0")] + #[must_use] #[inline] pub fn exists(&self) -> bool { fs::metadata(self).is_ok() @@ -2781,6 +2795,7 @@ impl Path { /// Converts a [`Box`](Box) into a [`PathBuf`] without copying or /// allocating. #[stable(feature = "into_boxed_path", since = "1.20.0")] + #[must_use = "`self` will be dropped if the result is not used"] pub fn into_path_buf(self: Box) -> PathBuf { let rw = Box::into_raw(self) as *mut OsStr; let inner = unsafe { Box::from_raw(rw) }; diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 4bd06475e27..9cc7fc2f035 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -948,6 +948,7 @@ impl Command { /// let cmd = Command::new("echo"); /// assert_eq!(cmd.get_program(), "echo"); /// ``` + #[must_use] #[stable(feature = "command_access", since = "1.57.0")] pub fn get_program(&self) -> &OsStr { self.inner.get_program() @@ -1021,6 +1022,7 @@ impl Command { /// cmd.current_dir("/bin"); /// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin"))); /// ``` + #[must_use] #[stable(feature = "command_access", since = "1.57.0")] pub fn get_current_dir(&self) -> Option<&Path> { self.inner.get_current_dir() @@ -1053,6 +1055,7 @@ impl AsInnerMut for Command { /// /// This struct is created by [`Command::get_args`]. See its documentation for /// more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "command_access", since = "1.57.0")] #[derive(Debug)] pub struct CommandArgs<'a> { @@ -1183,6 +1186,7 @@ impl Stdio { /// its entire stdin before writing more than a pipe buffer's worth of output. /// The size of a pipe buffer varies on different targets. /// + #[must_use] #[stable(feature = "process", since = "1.0.0")] pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) @@ -1222,6 +1226,7 @@ impl Stdio { /// print!("You piped in the reverse of: "); /// io::stdout().write_all(&output.stdout).unwrap(); /// ``` + #[must_use] #[stable(feature = "process", since = "1.0.0")] pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) @@ -1261,6 +1266,7 @@ impl Stdio { /// assert_eq!(String::from_utf8_lossy(&output.stdout), ""); /// // Ignores any piped-in input /// ``` + #[must_use] #[stable(feature = "process", since = "1.0.0")] pub fn null() -> Stdio { Stdio(imp::Stdio::Null) @@ -1462,6 +1468,7 @@ impl ExitStatus { /// println!("failed to create 'projects/' directory: {}", status); /// } /// ``` + #[must_use] #[stable(feature = "process", since = "1.0.0")] pub fn success(&self) -> bool { self.0.exit_ok().is_ok() @@ -1493,6 +1500,7 @@ impl ExitStatus { /// None => println!("Process terminated by signal") /// } /// ``` + #[must_use] #[stable(feature = "process", since = "1.0.0")] pub fn code(&self) -> Option { self.0.code() @@ -1580,6 +1588,7 @@ impl ExitStatusError { /// assert_eq!(bad.code(), Some(1)); /// # } // #[cfg(unix)] /// ``` + #[must_use] pub fn code(&self) -> Option { self.code_nonzero().map(Into::into) } @@ -1605,11 +1614,13 @@ impl ExitStatusError { /// assert_eq!(bad.code_nonzero().unwrap(), NonZeroI32::try_from(1).unwrap()); /// # } // cfg!(unix) /// ``` + #[must_use] pub fn code_nonzero(&self) -> Option { self.0.code() } /// Converts an `ExitStatusError` (back) to an `ExitStatus`. + #[must_use] pub fn into_status(&self) -> ExitStatus { ExitStatus(self.0.into()) } @@ -1718,6 +1729,7 @@ impl Child { /// println!("ls command didn't start"); /// } /// ``` + #[must_use] #[stable(feature = "process_id", since = "1.3.0")] pub fn id(&self) -> u32 { self.handle.id() @@ -1988,6 +2000,7 @@ pub fn abort() -> ! { /// ``` /// /// +#[must_use] #[stable(feature = "getpid", since = "1.26.0")] pub fn id() -> u32 { crate::sys::os::getpid() diff --git a/library/std/src/sync/condvar.rs b/library/std/src/sync/condvar.rs index d8aca9651b8..b41918ec1cf 100644 --- a/library/std/src/sync/condvar.rs +++ b/library/std/src/sync/condvar.rs @@ -61,6 +61,7 @@ impl WaitTimeoutResult { /// } /// } /// ``` + #[must_use] #[stable(feature = "wait_timeout", since = "1.5.0")] pub fn timed_out(&self) -> bool { self.0 diff --git a/library/std/src/sync/mpsc/mod.rs b/library/std/src/sync/mpsc/mod.rs index b4f4456537b..2cf678ef69b 100644 --- a/library/std/src/sync/mpsc/mod.rs +++ b/library/std/src/sync/mpsc/mod.rs @@ -707,6 +707,7 @@ impl UnsafeFlavor for Receiver { /// // Let's see what that answer was /// println!("{:?}", receiver.recv().unwrap()); /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn channel() -> (Sender, Receiver) { let a = Arc::new(oneshot::Packet::new()); @@ -755,6 +756,7 @@ pub fn channel() -> (Sender, Receiver) { /// assert_eq!(receiver.recv().unwrap(), 1); /// assert_eq!(receiver.recv().unwrap(), 2); /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn sync_channel(bound: usize) -> (SyncSender, Receiver) { let a = Arc::new(sync::Packet::new(bound)); diff --git a/library/std/src/sys_common/process.rs b/library/std/src/sys_common/process.rs index 3d71219756a..2cd1e29f6c4 100644 --- a/library/std/src/sys_common/process.rs +++ b/library/std/src/sys_common/process.rs @@ -106,6 +106,7 @@ impl CommandEnv { /// This struct is created by /// [`Command::get_envs`][crate::process::Command::get_envs]. See its /// documentation for more. +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "command_access", since = "1.57.0")] #[derive(Debug)] pub struct CommandEnvs<'a> { diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 1a5cf5ab822..ac71c9664a6 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -649,6 +649,7 @@ where /// /// handler.join().unwrap(); /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn current() -> Thread { thread_info::current_thread().expect( @@ -737,6 +738,7 @@ pub fn yield_now() { /// /// [Mutex]: crate::sync::Mutex #[inline] +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn panicking() -> bool { panicking::panicking() @@ -1131,6 +1133,7 @@ impl Thread { /// assert!(thread::current().id() != other_thread_id); /// ``` #[stable(feature = "thread_id", since = "1.19.0")] + #[must_use] pub fn id(&self) -> ThreadId { self.inner.id } @@ -1173,6 +1176,7 @@ impl Thread { /// /// [naming-threads]: ./index.html#naming-threads #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn name(&self) -> Option<&str> { self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) }) } @@ -1360,6 +1364,7 @@ impl JoinHandle { /// println!("thread id: {:?}", thread.id()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] pub fn thread(&self) -> &Thread { &self.0.thread } diff --git a/library/std/src/time.rs b/library/std/src/time.rs index 3e27e3a4297..a5e3bd0c290 100644 --- a/library/std/src/time.rs +++ b/library/std/src/time.rs @@ -239,6 +239,7 @@ impl Instant { /// /// let now = Instant::now(); /// ``` + #[must_use] #[stable(feature = "time2", since = "1.8.0")] pub fn now() -> Instant { let os_now = time::Instant::now(); @@ -306,6 +307,7 @@ impl Instant { /// let new_now = Instant::now(); /// println!("{:?}", new_now.duration_since(now)); /// ``` + #[must_use] #[stable(feature = "time2", since = "1.8.0")] pub fn duration_since(&self, earlier: Instant) -> Duration { self.0.checked_sub_instant(&earlier.0).expect("supplied instant is later than self") @@ -326,6 +328,7 @@ impl Instant { /// println!("{:?}", new_now.checked_duration_since(now)); /// println!("{:?}", now.checked_duration_since(new_now)); // None /// ``` + #[must_use] #[stable(feature = "checked_duration_since", since = "1.39.0")] pub fn checked_duration_since(&self, earlier: Instant) -> Option { self.0.checked_sub_instant(&earlier.0) @@ -346,6 +349,7 @@ impl Instant { /// println!("{:?}", new_now.saturating_duration_since(now)); /// println!("{:?}", now.saturating_duration_since(new_now)); // 0ns /// ``` + #[must_use] #[stable(feature = "checked_duration_since", since = "1.39.0")] pub fn saturating_duration_since(&self, earlier: Instant) -> Duration { self.checked_duration_since(earlier).unwrap_or_default() @@ -370,6 +374,7 @@ impl Instant { /// sleep(three_secs); /// assert!(instant.elapsed() >= three_secs); /// ``` + #[must_use] #[stable(feature = "time2", since = "1.8.0")] pub fn elapsed(&self) -> Duration { Instant::now() - *self @@ -476,6 +481,7 @@ impl SystemTime { /// /// let sys_time = SystemTime::now(); /// ``` + #[must_use] #[stable(feature = "time2", since = "1.8.0")] pub fn now() -> SystemTime { SystemTime(time::SystemTime::now()) @@ -644,6 +650,7 @@ impl SystemTimeError { /// Err(e) => println!("SystemTimeError difference: {:?}", e.duration()), /// } /// ``` + #[must_use] #[stable(feature = "time2", since = "1.8.0")] pub fn duration(&self) -> Duration { self.0 diff --git a/library/std/src/time/tests.rs b/library/std/src/time/tests.rs index dc44c9346b6..7279925a6d0 100644 --- a/library/std/src/time/tests.rs +++ b/library/std/src/time/tests.rs @@ -48,7 +48,7 @@ fn instant_monotonic_concurrent() -> crate::thread::Result<()> { #[test] fn instant_elapsed() { let a = Instant::now(); - a.elapsed(); + let _ = a.elapsed(); } #[test] @@ -93,7 +93,7 @@ fn instant_math_is_associative() { #[should_panic] fn instant_duration_since_panic() { let a = Instant::now(); - (a - Duration::SECOND).duration_since(a); + let _ = (a - Duration::SECOND).duration_since(a); } #[test] From e129d49f88a69d4bb8cb0f45a0a674c17393f4e9 Mon Sep 17 00:00:00 2001 From: John Kugelman Date: Sat, 30 Oct 2021 22:58:27 -0400 Subject: [PATCH 7/7] Add #[must_use] to remaining std functions (A-N) --- library/std/src/backtrace.rs | 3 +++ library/std/src/collections/hash/map.rs | 5 +++++ library/std/src/env.rs | 8 ++++++++ library/std/src/ffi/c_str.rs | 3 +++ library/std/src/ffi/os_str.rs | 2 ++ library/std/src/fs.rs | 6 ++++++ library/std/src/io/error.rs | 5 +++++ library/std/src/io/mod.rs | 3 +++ library/std/src/io/stdio.rs | 3 +++ library/std/src/io/util.rs | 3 +++ library/std/src/net/addr.rs | 8 ++++++++ library/std/src/net/ip.rs | 4 ++++ library/std/src/net/tcp.rs | 1 + src/test/ui/rust-2018/uniform-paths/redundant.rs | 8 ++++---- src/test/ui/uniform-paths/basic-nested.rs | 8 ++++---- src/test/ui/uniform-paths/basic.rs | 4 ++-- src/test/ui/uniform-paths/macros-nested.rs | 6 +++--- src/test/ui/uniform-paths/macros.rs | 2 +- 18 files changed, 68 insertions(+), 14 deletions(-) diff --git a/library/std/src/backtrace.rs b/library/std/src/backtrace.rs index 9ace3e1b600..0b86b4f30b9 100644 --- a/library/std/src/backtrace.rs +++ b/library/std/src/backtrace.rs @@ -110,6 +110,7 @@ use crate::vec::Vec; /// previous point in time. In some instances the `Backtrace` type may /// internally be empty due to configuration. For more information see /// `Backtrace::capture`. +#[must_use] pub struct Backtrace { inner: Inner, } @@ -355,6 +356,7 @@ impl Backtrace { /// Returns the status of this backtrace, indicating whether this backtrace /// request was unsupported, disabled, or a stack trace was actually /// captured. + #[must_use] pub fn status(&self) -> BacktraceStatus { match self.inner { Inner::Unsupported => BacktraceStatus::Unsupported, @@ -366,6 +368,7 @@ impl Backtrace { impl<'a> Backtrace { /// Returns an iterator over the backtrace frames. + #[must_use] #[unstable(feature = "backtrace_frames", issue = "79676")] pub fn frames(&'a self) -> &'a [BacktraceFrame] { if let Inner::Captured(c) = &self.inner { &c.force().frames } else { &[] } diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 01019344f4f..12246b5173d 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1707,6 +1707,7 @@ impl<'a, K, V, S> RawEntryMut<'a, K, V, S> { impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> { /// Gets a reference to the key in the entry. #[inline] + #[must_use] #[unstable(feature = "hash_raw_entry", issue = "56167")] pub fn key(&self) -> &K { self.base.key() @@ -1714,6 +1715,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> { /// Gets a mutable reference to the key in the entry. #[inline] + #[must_use] #[unstable(feature = "hash_raw_entry", issue = "56167")] pub fn key_mut(&mut self) -> &mut K { self.base.key_mut() @@ -1730,6 +1732,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> { /// Gets a reference to the value in the entry. #[inline] + #[must_use] #[unstable(feature = "hash_raw_entry", issue = "56167")] pub fn get(&self) -> &V { self.base.get() @@ -1746,6 +1749,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> { /// Gets a mutable reference to the value in the entry. #[inline] + #[must_use] #[unstable(feature = "hash_raw_entry", issue = "56167")] pub fn get_mut(&mut self) -> &mut V { self.base.get_mut() @@ -1753,6 +1757,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> { /// Gets a reference to the key and value in the entry. #[inline] + #[must_use] #[unstable(feature = "hash_raw_entry", issue = "56167")] pub fn get_key_value(&mut self) -> (&K, &V) { self.base.get_key_value() diff --git a/library/std/src/env.rs b/library/std/src/env.rs index 40b46878cd8..c6af708f6cd 100644 --- a/library/std/src/env.rs +++ b/library/std/src/env.rs @@ -113,6 +113,7 @@ pub struct VarsOs { /// ``` /// /// [`env::vars_os()`]: vars_os +#[must_use] #[stable(feature = "env", since = "1.0.0")] pub fn vars() -> Vars { Vars { inner: vars_os() } @@ -140,6 +141,7 @@ pub fn vars() -> Vars { /// println!("{:?}: {:?}", key, value); /// } /// ``` +#[must_use] #[stable(feature = "env", since = "1.0.0")] pub fn vars_os() -> VarsOs { VarsOs { inner: os_imp::env() } @@ -244,6 +246,7 @@ fn _var(key: &OsStr) -> Result { /// None => println!("{} is not defined in the environment.", key) /// } /// ``` +#[must_use] #[stable(feature = "env", since = "1.0.0")] pub fn var_os>(key: K) -> Option { _var_os(key.as_ref()) @@ -384,6 +387,7 @@ fn _remove_var(key: &OsStr) { /// documentation for more. /// /// [`env::split_paths()`]: split_paths +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "env", since = "1.0.0")] pub struct SplitPaths<'a> { inner: os_imp::SplitPaths<'a>, @@ -564,6 +568,7 @@ impl Error for JoinPathsError { reason = "This function's behavior is unexpected and probably not what you want. \ Consider using a crate from crates.io instead." )] +#[must_use] #[stable(feature = "env", since = "1.0.0")] pub fn home_dir() -> Option { os_imp::home_dir() @@ -603,6 +608,7 @@ pub fn home_dir() -> Option { /// println!("Temporary directory: {}", dir.display()); /// } /// ``` +#[must_use] #[stable(feature = "env", since = "1.0.0")] pub fn temp_dir() -> PathBuf { os_imp::temp_dir() @@ -690,6 +696,7 @@ pub fn current_exe() -> io::Result { /// should not be relied upon for security purposes. /// /// [`env::args()`]: args +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "env", since = "1.0.0")] pub struct Args { inner: ArgsOs, @@ -706,6 +713,7 @@ pub struct Args { /// should not be relied upon for security purposes. /// /// [`env::args_os()`]: args_os +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "env", since = "1.0.0")] pub struct ArgsOs { inner: sys::args::Args, diff --git a/library/std/src/ffi/c_str.rs b/library/std/src/ffi/c_str.rs index b7822b40a7c..465bbae8631 100644 --- a/library/std/src/ffi/c_str.rs +++ b/library/std/src/ffi/c_str.rs @@ -1009,6 +1009,7 @@ impl NulError { /// let nul_error = CString::new("foo bar\0").unwrap_err(); /// assert_eq!(nul_error.nul_position(), 7); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn nul_position(&self) -> usize { self.0 @@ -1107,6 +1108,7 @@ impl IntoStringError { } /// Access the underlying UTF-8 error that was the cause of this error. + #[must_use] #[stable(feature = "cstring_into", since = "1.7.0")] pub fn utf8_error(&self) -> Utf8Error { self.error @@ -1456,6 +1458,7 @@ impl CStr { /// let boxed = c_string.into_boxed_c_str(); /// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed")); /// ``` + #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "into_boxed_c_str", since = "1.20.0")] pub fn into_c_string(self: Box) -> CString { let raw = Box::into_raw(self) as *mut [u8]; diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 46c9aa5e627..d3a3e5262f9 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -239,6 +239,7 @@ impl OsString { /// assert!(os_string.capacity() >= 10); /// ``` #[stable(feature = "osstring_simple_functions", since = "1.9.0")] + #[must_use] #[inline] pub fn capacity(&self) -> usize { self.inner.capacity() @@ -707,6 +708,7 @@ impl OsStr { /// Converts a [Box]<[OsStr]> into an [`OsString`] without copying or allocating. #[stable(feature = "into_boxed_os_str", since = "1.20.0")] + #[must_use = "`self` will be dropped if the result is not used"] pub fn into_os_string(self: Box) -> OsString { let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) }; OsString { inner: Buf::from_box(boxed) } diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 9f45e89aa75..b9979280010 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -374,6 +374,7 @@ impl File { /// Ok(()) /// } /// ``` + #[must_use] #[unstable(feature = "with_options", issue = "65439")] pub fn with_options() -> OpenOptions { OpenOptions::new() @@ -983,6 +984,7 @@ impl Metadata { /// Ok(()) /// } /// ``` + #[must_use] #[stable(feature = "file_type", since = "1.1.0")] pub fn file_type(&self) -> FileType { FileType(self.0.file_type()) @@ -1100,6 +1102,7 @@ impl Metadata { /// Ok(()) /// } /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn permissions(&self) -> Permissions { Permissions(self.0.perm()) @@ -1247,6 +1250,7 @@ impl Permissions { /// Ok(()) /// } /// ``` + #[must_use = "call `set_readonly` to modify the readonly flag"] #[stable(feature = "rust1", since = "1.0.0")] pub fn readonly(&self) -> bool { self.0.readonly() @@ -1441,6 +1445,7 @@ impl DirEntry { /// ``` /// /// The exact text, of course, depends on what files you have in `.`. + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn path(&self) -> PathBuf { self.0.path() @@ -1536,6 +1541,7 @@ impl DirEntry { /// } /// } /// ``` + #[must_use] #[stable(feature = "dir_entry_ext", since = "1.1.0")] pub fn file_name(&self) -> OsString { self.0.file_name() diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 59a9cd781cb..3da28695b34 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -442,6 +442,7 @@ impl Error { /// println!("last OS error: {:?}", Error::last_os_error()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub fn last_os_error() -> Error { Error::from_raw_os_error(sys::os::errno() as i32) @@ -509,6 +510,7 @@ impl Error { /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub fn raw_os_error(&self) -> Option { match self.repr { @@ -547,6 +549,7 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] + #[must_use] #[inline] pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> { match self.repr { @@ -620,6 +623,7 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] + #[must_use] #[inline] pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> { match self.repr { @@ -688,6 +692,7 @@ impl Error { /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub fn kind(&self) -> ErrorKind { match self.repr { diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index abe29ba0f7c..f421185c2c5 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1310,6 +1310,7 @@ pub struct Initializer(bool); impl Initializer { /// Returns a new `Initializer` which will zero out buffers. #[unstable(feature = "read_initializer", issue = "42788")] + #[must_use] #[inline] pub fn zeroing() -> Initializer { Initializer(true) @@ -1324,6 +1325,7 @@ impl Initializer { /// the method accurately reflects the number of bytes that have been /// written to the head of the buffer. #[unstable(feature = "read_initializer", issue = "42788")] + #[must_use] #[inline] pub unsafe fn nop() -> Initializer { Initializer(false) @@ -1331,6 +1333,7 @@ impl Initializer { /// Indicates if a buffer should be initialized. #[unstable(feature = "read_initializer", issue = "42788")] + #[must_use] #[inline] pub fn should_initialize(&self) -> bool { self.0 diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 9389501e012..f471693774c 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -301,6 +301,7 @@ pub struct StdinLock<'a> { /// Ok(()) /// } /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn stdin() -> Stdin { static INSTANCE: SyncOnceCell>> = SyncOnceCell::new(); @@ -673,6 +674,7 @@ static STDOUT: SyncOnceCell>>> = Sy /// Ok(()) /// } /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn stdout() -> Stdout { Stdout { @@ -953,6 +955,7 @@ pub struct StderrLock<'a> { /// Ok(()) /// } /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] pub fn stderr() -> Stderr { // Note that unlike `stdout()` we don't use `at_exit` here to register a diff --git a/library/std/src/io/util.rs b/library/std/src/io/util.rs index 2f3520ae7a5..9cd7c514849 100644 --- a/library/std/src/io/util.rs +++ b/library/std/src/io/util.rs @@ -32,6 +32,7 @@ pub struct Empty; /// io::empty().read_to_string(&mut buffer).unwrap(); /// assert!(buffer.is_empty()); /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")] pub const fn empty() -> Empty { @@ -112,6 +113,7 @@ pub struct Repeat { /// io::repeat(0b101).read_exact(&mut buffer).unwrap(); /// assert_eq!(buffer, [0b101, 0b101, 0b101]); /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")] pub const fn repeat(byte: u8) -> Repeat { @@ -192,6 +194,7 @@ pub struct Sink; /// let num_bytes = io::sink().write(&buffer).unwrap(); /// assert_eq!(num_bytes, 5); /// ``` +#[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")] pub const fn sink() -> Sink { diff --git a/library/std/src/net/addr.rs b/library/std/src/net/addr.rs index a689d2a56b7..201cbf3f08d 100644 --- a/library/std/src/net/addr.rs +++ b/library/std/src/net/addr.rs @@ -149,6 +149,7 @@ impl SocketAddr { /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))); /// ``` + #[must_use] #[stable(feature = "ip_addr", since = "1.7.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn ip(&self) -> IpAddr { @@ -189,6 +190,7 @@ impl SocketAddr { /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); /// assert_eq!(socket.port(), 8080); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn port(&self) -> u16 { @@ -297,6 +299,7 @@ impl SocketAddrV4 { /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1)); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn ip(&self) -> &Ipv4Addr { @@ -331,6 +334,7 @@ impl SocketAddrV4 { /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); /// assert_eq!(socket.port(), 8080); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn port(&self) -> u16 { @@ -396,6 +400,7 @@ impl SocketAddrV6 { /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); /// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn ip(&self) -> &Ipv6Addr { @@ -428,6 +433,7 @@ impl SocketAddrV6 { /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); /// assert_eq!(socket.port(), 8080); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn port(&self) -> u16 { @@ -470,6 +476,7 @@ impl SocketAddrV6 { /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0); /// assert_eq!(socket.flowinfo(), 10); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn flowinfo(&self) -> u32 { @@ -509,6 +516,7 @@ impl SocketAddrV6 { /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78); /// assert_eq!(socket.scope_id(), 78); /// ``` + #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")] pub const fn scope_id(&self) -> u32 { diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index c080f783cbb..140647128a9 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -518,6 +518,7 @@ impl Ipv4Addr { /// ``` #[rustc_const_stable(feature = "const_ipv4", since = "1.50.0")] #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub const fn octets(&self) -> [u8; 4] { // This returns the order we want because s_addr is stored in big-endian. @@ -1284,6 +1285,7 @@ impl Ipv6Addr { /// ``` #[rustc_const_stable(feature = "const_ipv6", since = "1.50.0")] #[stable(feature = "rust1", since = "1.0.0")] + #[must_use] #[inline] pub const fn segments(&self) -> [u16; 8] { // All elements in `s6_addr` must be big endian. @@ -1594,6 +1596,7 @@ impl Ipv6Addr { /// ``` #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")] #[unstable(feature = "ip", issue = "27709")] + #[must_use] #[inline] pub const fn multicast_scope(&self) -> Option { if self.is_multicast() { @@ -1744,6 +1747,7 @@ impl Ipv6Addr { /// ``` #[rustc_const_stable(feature = "const_ipv6", since = "1.32.0")] #[stable(feature = "ipv6_to_octets", since = "1.12.0")] + #[must_use] #[inline] pub const fn octets(&self) -> [u8; 16] { self.inner.s6_addr diff --git a/library/std/src/net/tcp.rs b/library/std/src/net/tcp.rs index 2c6e3930059..5738862fb58 100644 --- a/library/std/src/net/tcp.rs +++ b/library/std/src/net/tcp.rs @@ -90,6 +90,7 @@ pub struct TcpListener(net_imp::TcpListener); /// See its documentation for more. /// /// [`accept`]: TcpListener::accept +#[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] #[derive(Debug)] pub struct Incoming<'a> { diff --git a/src/test/ui/rust-2018/uniform-paths/redundant.rs b/src/test/ui/rust-2018/uniform-paths/redundant.rs index 2d306eb920c..fd7fc7fbd41 100644 --- a/src/test/ui/rust-2018/uniform-paths/redundant.rs +++ b/src/test/ui/rust-2018/uniform-paths/redundant.rs @@ -13,8 +13,8 @@ mod bar { } fn main() { - io::stdout(); - self::std::io::stdout(); - foo::my_std::io::stdout(); - bar::std::io::stdout(); + let _ = io::stdout(); + let _ = self::std::io::stdout(); + let _ = foo::my_std::io::stdout(); + let _ = bar::std::io::stdout(); } diff --git a/src/test/ui/uniform-paths/basic-nested.rs b/src/test/ui/uniform-paths/basic-nested.rs index e4e8b32c70e..dcf0eb646f3 100644 --- a/src/test/ui/uniform-paths/basic-nested.rs +++ b/src/test/ui/uniform-paths/basic-nested.rs @@ -44,10 +44,10 @@ mod bar { fn main() { foo::Foo(()); - foo::std_io::stdout(); + let _ = foo::std_io::stdout(); foo::local_io(()); - io::stdout(); - bar::io::stdout(); + let _ = io::stdout(); + let _ = bar::io::stdout(); bar::std(); bar::std!(); @@ -56,6 +56,6 @@ fn main() { // scope is allowed, when both resolve to the same definition. use std::io; use io::stdout; - stdout(); + let _ = stdout(); } } diff --git a/src/test/ui/uniform-paths/basic.rs b/src/test/ui/uniform-paths/basic.rs index 4e2e2dedef6..ce611a7cacf 100644 --- a/src/test/ui/uniform-paths/basic.rs +++ b/src/test/ui/uniform-paths/basic.rs @@ -20,7 +20,7 @@ use self::std::io as local_io; fn main() { Foo(()); - std_io::stdout(); + let _ = std_io::stdout(); local_io(()); { @@ -28,6 +28,6 @@ fn main() { // scope is allowed, when both resolve to the same definition. use ::std::io as std_io; use std_io::stdout; - stdout(); + let _ = stdout(); } } diff --git a/src/test/ui/uniform-paths/macros-nested.rs b/src/test/ui/uniform-paths/macros-nested.rs index a62a28bb94d..175ccd34e98 100644 --- a/src/test/ui/uniform-paths/macros-nested.rs +++ b/src/test/ui/uniform-paths/macros-nested.rs @@ -46,8 +46,8 @@ mod bar { fn main() { foo::Foo(()); - foo::std_io::stdout(); + let _ = foo::std_io::stdout(); foo::local_io(()); - io::stdout(); - bar::io::stdout(); + let _ = io::stdout(); + let _ = bar::io::stdout(); } diff --git a/src/test/ui/uniform-paths/macros.rs b/src/test/ui/uniform-paths/macros.rs index 31b809f0cfd..bf512b30560 100644 --- a/src/test/ui/uniform-paths/macros.rs +++ b/src/test/ui/uniform-paths/macros.rs @@ -31,6 +31,6 @@ m2!(); fn main() { Foo(()); - std_io::stdout(); + let _ = std_io::stdout(); local_io(()); }