Skip to content

Commit 68b554e

Browse files
committed
Auto merge of #90437 - matthiaskrgr:rollup-vd8uqm6, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #89068 (Restructure std::rt (part 2)) - #89786 (Add #[must_use] to len and is_empty) - #90430 (Add #[must_use] to remaining std functions (A-N)) - #90431 (Add #[must_use] to remaining std functions (O-Z)) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 58899c4 + 455a79a commit 68b554e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+198
-72
lines changed

library/alloc/src/collections/binary_heap.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ impl<T> BinaryHeap<T> {
10521052
///
10531053
/// assert_eq!(heap.len(), 2);
10541054
/// ```
1055+
#[must_use]
10551056
#[stable(feature = "rust1", since = "1.0.0")]
10561057
pub fn len(&self) -> usize {
10571058
self.data.len()
@@ -1075,6 +1076,7 @@ impl<T> BinaryHeap<T> {
10751076
///
10761077
/// assert!(!heap.is_empty());
10771078
/// ```
1079+
#[must_use]
10781080
#[stable(feature = "rust1", since = "1.0.0")]
10791081
pub fn is_empty(&self) -> bool {
10801082
self.len() == 0

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

+2
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,7 @@ impl<K, V> BTreeMap<K, V> {
22122212
/// a.insert(1, "a");
22132213
/// assert_eq!(a.len(), 1);
22142214
/// ```
2215+
#[must_use]
22152216
#[stable(feature = "rust1", since = "1.0.0")]
22162217
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
22172218
pub const fn len(&self) -> usize {
@@ -2232,6 +2233,7 @@ impl<K, V> BTreeMap<K, V> {
22322233
/// a.insert(1, "a");
22332234
/// assert!(!a.is_empty());
22342235
/// ```
2236+
#[must_use]
22352237
#[stable(feature = "rust1", since = "1.0.0")]
22362238
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
22372239
pub const fn is_empty(&self) -> bool {

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

+2
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ impl<T> BTreeSet<T> {
10461046
/// v.insert(1);
10471047
/// assert_eq!(v.len(), 1);
10481048
/// ```
1049+
#[must_use]
10491050
#[stable(feature = "rust1", since = "1.0.0")]
10501051
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10511052
pub const fn len(&self) -> usize {
@@ -1064,6 +1065,7 @@ impl<T> BTreeSet<T> {
10641065
/// v.insert(1);
10651066
/// assert!(!v.is_empty());
10661067
/// ```
1068+
#[must_use]
10671069
#[stable(feature = "rust1", since = "1.0.0")]
10681070
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
10691071
pub const fn is_empty(&self) -> bool {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,8 @@ 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();
616616
let _ = set.iter();
617617
let _ = set.into_iter();

library/alloc/src/collections/linked_list.rs

+2
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ impl<T> LinkedList<T> {
583583
/// assert!(!dl.is_empty());
584584
/// ```
585585
#[inline]
586+
#[must_use]
586587
#[stable(feature = "rust1", since = "1.0.0")]
587588
pub fn is_empty(&self) -> bool {
588589
self.head.is_none()
@@ -609,6 +610,7 @@ impl<T> LinkedList<T> {
609610
/// assert_eq!(dl.len(), 3);
610611
/// ```
611612
#[inline]
613+
#[must_use]
612614
#[stable(feature = "rust1", since = "1.0.0")]
613615
pub fn len(&self) -> usize {
614616
self.len

library/alloc/src/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,7 @@ impl String {
15471547
/// assert_eq!(fancy_f.chars().count(), 3);
15481548
/// ```
15491549
#[inline]
1550+
#[must_use]
15501551
#[stable(feature = "rust1", since = "1.0.0")]
15511552
pub fn len(&self) -> usize {
15521553
self.vec.len()
@@ -1566,6 +1567,7 @@ impl String {
15661567
/// assert!(!v.is_empty());
15671568
/// ```
15681569
#[inline]
1570+
#[must_use]
15691571
#[stable(feature = "rust1", since = "1.0.0")]
15701572
pub fn is_empty(&self) -> bool {
15711573
self.len() == 0

library/core/src/ptr/non_null.rs

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ impl<T> NonNull<[T]> {
449449
/// ```
450450
#[unstable(feature = "slice_ptr_len", issue = "71146")]
451451
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
452+
#[must_use]
452453
#[inline]
453454
pub const fn len(self) -> usize {
454455
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/backtrace.rs

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ use crate::vec::Vec;
110110
/// previous point in time. In some instances the `Backtrace` type may
111111
/// internally be empty due to configuration. For more information see
112112
/// `Backtrace::capture`.
113+
#[must_use]
113114
pub struct Backtrace {
114115
inner: Inner,
115116
}
@@ -355,6 +356,7 @@ impl Backtrace {
355356
/// Returns the status of this backtrace, indicating whether this backtrace
356357
/// request was unsupported, disabled, or a stack trace was actually
357358
/// captured.
359+
#[must_use]
358360
pub fn status(&self) -> BacktraceStatus {
359361
match self.inner {
360362
Inner::Unsupported => BacktraceStatus::Unsupported,
@@ -366,6 +368,7 @@ impl Backtrace {
366368

367369
impl<'a> Backtrace {
368370
/// Returns an iterator over the backtrace frames.
371+
#[must_use]
369372
#[unstable(feature = "backtrace_frames", issue = "79676")]
370373
pub fn frames(&'a self) -> &'a [BacktraceFrame] {
371374
if let Inner::Captured(c) = &self.inner { &c.force().frames } else { &[] }

library/std/src/collections/hash/map.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1707,13 +1707,15 @@ impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
17071707
impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
17081708
/// Gets a reference to the key in the entry.
17091709
#[inline]
1710+
#[must_use]
17101711
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17111712
pub fn key(&self) -> &K {
17121713
self.base.key()
17131714
}
17141715

17151716
/// Gets a mutable reference to the key in the entry.
17161717
#[inline]
1718+
#[must_use]
17171719
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17181720
pub fn key_mut(&mut self) -> &mut K {
17191721
self.base.key_mut()
@@ -1730,6 +1732,7 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
17301732

17311733
/// Gets a reference to the value in the entry.
17321734
#[inline]
1735+
#[must_use]
17331736
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17341737
pub fn get(&self) -> &V {
17351738
self.base.get()
@@ -1746,13 +1749,15 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
17461749

17471750
/// Gets a mutable reference to the value in the entry.
17481751
#[inline]
1752+
#[must_use]
17491753
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17501754
pub fn get_mut(&mut self) -> &mut V {
17511755
self.base.get_mut()
17521756
}
17531757

17541758
/// Gets a reference to the key and value in the entry.
17551759
#[inline]
1760+
#[must_use]
17561761
#[unstable(feature = "hash_raw_entry", issue = "56167")]
17571762
pub fn get_key_value(&mut self) -> (&K, &V) {
17581763
self.base.get_key_value()

library/std/src/env.rs

+8
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub struct VarsOs {
113113
/// ```
114114
///
115115
/// [`env::vars_os()`]: vars_os
116+
#[must_use]
116117
#[stable(feature = "env", since = "1.0.0")]
117118
pub fn vars() -> Vars {
118119
Vars { inner: vars_os() }
@@ -140,6 +141,7 @@ pub fn vars() -> Vars {
140141
/// println!("{:?}: {:?}", key, value);
141142
/// }
142143
/// ```
144+
#[must_use]
143145
#[stable(feature = "env", since = "1.0.0")]
144146
pub fn vars_os() -> VarsOs {
145147
VarsOs { inner: os_imp::env() }
@@ -244,6 +246,7 @@ fn _var(key: &OsStr) -> Result<String, VarError> {
244246
/// None => println!("{} is not defined in the environment.", key)
245247
/// }
246248
/// ```
249+
#[must_use]
247250
#[stable(feature = "env", since = "1.0.0")]
248251
pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
249252
_var_os(key.as_ref())
@@ -384,6 +387,7 @@ fn _remove_var(key: &OsStr) {
384387
/// documentation for more.
385388
///
386389
/// [`env::split_paths()`]: split_paths
390+
#[must_use = "iterators are lazy and do nothing unless consumed"]
387391
#[stable(feature = "env", since = "1.0.0")]
388392
pub struct SplitPaths<'a> {
389393
inner: os_imp::SplitPaths<'a>,
@@ -564,6 +568,7 @@ impl Error for JoinPathsError {
564568
reason = "This function's behavior is unexpected and probably not what you want. \
565569
Consider using a crate from crates.io instead."
566570
)]
571+
#[must_use]
567572
#[stable(feature = "env", since = "1.0.0")]
568573
pub fn home_dir() -> Option<PathBuf> {
569574
os_imp::home_dir()
@@ -603,6 +608,7 @@ pub fn home_dir() -> Option<PathBuf> {
603608
/// println!("Temporary directory: {}", dir.display());
604609
/// }
605610
/// ```
611+
#[must_use]
606612
#[stable(feature = "env", since = "1.0.0")]
607613
pub fn temp_dir() -> PathBuf {
608614
os_imp::temp_dir()
@@ -690,6 +696,7 @@ pub fn current_exe() -> io::Result<PathBuf> {
690696
/// should not be relied upon for security purposes.
691697
///
692698
/// [`env::args()`]: args
699+
#[must_use = "iterators are lazy and do nothing unless consumed"]
693700
#[stable(feature = "env", since = "1.0.0")]
694701
pub struct Args {
695702
inner: ArgsOs,
@@ -706,6 +713,7 @@ pub struct Args {
706713
/// should not be relied upon for security purposes.
707714
///
708715
/// [`env::args_os()`]: args_os
716+
#[must_use = "iterators are lazy and do nothing unless consumed"]
709717
#[stable(feature = "env", since = "1.0.0")]
710718
pub struct ArgsOs {
711719
inner: sys::args::Args,

library/std/src/ffi/c_str.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ impl NulError {
10091009
/// let nul_error = CString::new("foo bar\0").unwrap_err();
10101010
/// assert_eq!(nul_error.nul_position(), 7);
10111011
/// ```
1012+
#[must_use]
10121013
#[stable(feature = "rust1", since = "1.0.0")]
10131014
pub fn nul_position(&self) -> usize {
10141015
self.0
@@ -1107,6 +1108,7 @@ impl IntoStringError {
11071108
}
11081109

11091110
/// Access the underlying UTF-8 error that was the cause of this error.
1111+
#[must_use]
11101112
#[stable(feature = "cstring_into", since = "1.7.0")]
11111113
pub fn utf8_error(&self) -> Utf8Error {
11121114
self.error
@@ -1456,6 +1458,7 @@ impl CStr {
14561458
/// let boxed = c_string.into_boxed_c_str();
14571459
/// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
14581460
/// ```
1461+
#[must_use = "`self` will be dropped if the result is not used"]
14591462
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
14601463
pub fn into_c_string(self: Box<CStr>) -> CString {
14611464
let raw = Box::into_raw(self) as *mut [u8];

library/std/src/ffi/os_str.rs

+4
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ impl OsString {
239239
/// assert!(os_string.capacity() >= 10);
240240
/// ```
241241
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
242+
#[must_use]
242243
#[inline]
243244
pub fn capacity(&self) -> usize {
244245
self.inner.capacity()
@@ -669,6 +670,7 @@ impl OsStr {
669670
/// assert!(!os_str.is_empty());
670671
/// ```
671672
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
673+
#[must_use]
672674
#[inline]
673675
pub fn is_empty(&self) -> bool {
674676
self.inner.inner.is_empty()
@@ -700,13 +702,15 @@ impl OsStr {
700702
/// assert_eq!(os_str.len(), 3);
701703
/// ```
702704
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
705+
#[must_use]
703706
#[inline]
704707
pub fn len(&self) -> usize {
705708
self.inner.inner.len()
706709
}
707710

708711
/// Converts a <code>[Box]<[OsStr]></code> into an [`OsString`] without copying or allocating.
709712
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
713+
#[must_use = "`self` will be dropped if the result is not used"]
710714
pub fn into_os_string(self: Box<OsStr>) -> OsString {
711715
let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
712716
OsString { inner: Buf::from_box(boxed) }

library/std/src/fs.rs

+7
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ impl File {
374374
/// Ok(())
375375
/// }
376376
/// ```
377+
#[must_use]
377378
#[unstable(feature = "with_options", issue = "65439")]
378379
pub fn with_options() -> OpenOptions {
379380
OpenOptions::new()
@@ -983,6 +984,7 @@ impl Metadata {
983984
/// Ok(())
984985
/// }
985986
/// ```
987+
#[must_use]
986988
#[stable(feature = "file_type", since = "1.1.0")]
987989
pub fn file_type(&self) -> FileType {
988990
FileType(self.0.file_type())
@@ -1080,6 +1082,7 @@ impl Metadata {
10801082
/// Ok(())
10811083
/// }
10821084
/// ```
1085+
#[must_use]
10831086
#[stable(feature = "rust1", since = "1.0.0")]
10841087
pub fn len(&self) -> u64 {
10851088
self.0.size()
@@ -1099,6 +1102,7 @@ impl Metadata {
10991102
/// Ok(())
11001103
/// }
11011104
/// ```
1105+
#[must_use]
11021106
#[stable(feature = "rust1", since = "1.0.0")]
11031107
pub fn permissions(&self) -> Permissions {
11041108
Permissions(self.0.perm())
@@ -1246,6 +1250,7 @@ impl Permissions {
12461250
/// Ok(())
12471251
/// }
12481252
/// ```
1253+
#[must_use = "call `set_readonly` to modify the readonly flag"]
12491254
#[stable(feature = "rust1", since = "1.0.0")]
12501255
pub fn readonly(&self) -> bool {
12511256
self.0.readonly()
@@ -1440,6 +1445,7 @@ impl DirEntry {
14401445
/// ```
14411446
///
14421447
/// The exact text, of course, depends on what files you have in `.`.
1448+
#[must_use]
14431449
#[stable(feature = "rust1", since = "1.0.0")]
14441450
pub fn path(&self) -> PathBuf {
14451451
self.0.path()
@@ -1535,6 +1541,7 @@ impl DirEntry {
15351541
/// }
15361542
/// }
15371543
/// ```
1544+
#[must_use]
15381545
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
15391546
pub fn file_name(&self) -> OsString {
15401547
self.0.file_name()

library/std/src/io/error.rs

+5
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ impl Error {
442442
/// println!("last OS error: {:?}", Error::last_os_error());
443443
/// ```
444444
#[stable(feature = "rust1", since = "1.0.0")]
445+
#[must_use]
445446
#[inline]
446447
pub fn last_os_error() -> Error {
447448
Error::from_raw_os_error(sys::os::errno() as i32)
@@ -509,6 +510,7 @@ impl Error {
509510
/// }
510511
/// ```
511512
#[stable(feature = "rust1", since = "1.0.0")]
513+
#[must_use]
512514
#[inline]
513515
pub fn raw_os_error(&self) -> Option<i32> {
514516
match self.repr {
@@ -547,6 +549,7 @@ impl Error {
547549
/// }
548550
/// ```
549551
#[stable(feature = "io_error_inner", since = "1.3.0")]
552+
#[must_use]
550553
#[inline]
551554
pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
552555
match self.repr {
@@ -620,6 +623,7 @@ impl Error {
620623
/// }
621624
/// ```
622625
#[stable(feature = "io_error_inner", since = "1.3.0")]
626+
#[must_use]
623627
#[inline]
624628
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
625629
match self.repr {
@@ -688,6 +692,7 @@ impl Error {
688692
/// }
689693
/// ```
690694
#[stable(feature = "rust1", since = "1.0.0")]
695+
#[must_use]
691696
#[inline]
692697
pub fn kind(&self) -> ErrorKind {
693698
match self.repr {

0 commit comments

Comments
 (0)