Skip to content

Commit e129d49

Browse files
committed
Add #[must_use] to remaining std functions (A-N)
1 parent e249ce6 commit e129d49

File tree

18 files changed

+68
-14
lines changed

18 files changed

+68
-14
lines changed

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

+2
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()
@@ -707,6 +708,7 @@ impl OsStr {
707708

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

library/std/src/fs.rs

+6
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())
@@ -1100,6 +1102,7 @@ impl Metadata {
11001102
/// Ok(())
11011103
/// }
11021104
/// ```
1105+
#[must_use]
11031106
#[stable(feature = "rust1", since = "1.0.0")]
11041107
pub fn permissions(&self) -> Permissions {
11051108
Permissions(self.0.perm())
@@ -1247,6 +1250,7 @@ impl Permissions {
12471250
/// Ok(())
12481251
/// }
12491252
/// ```
1253+
#[must_use = "call `set_readonly` to modify the readonly flag"]
12501254
#[stable(feature = "rust1", since = "1.0.0")]
12511255
pub fn readonly(&self) -> bool {
12521256
self.0.readonly()
@@ -1441,6 +1445,7 @@ impl DirEntry {
14411445
/// ```
14421446
///
14431447
/// The exact text, of course, depends on what files you have in `.`.
1448+
#[must_use]
14441449
#[stable(feature = "rust1", since = "1.0.0")]
14451450
pub fn path(&self) -> PathBuf {
14461451
self.0.path()
@@ -1536,6 +1541,7 @@ impl DirEntry {
15361541
/// }
15371542
/// }
15381543
/// ```
1544+
#[must_use]
15391545
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
15401546
pub fn file_name(&self) -> OsString {
15411547
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 {

library/std/src/io/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,7 @@ pub struct Initializer(bool);
13101310
impl Initializer {
13111311
/// Returns a new `Initializer` which will zero out buffers.
13121312
#[unstable(feature = "read_initializer", issue = "42788")]
1313+
#[must_use]
13131314
#[inline]
13141315
pub fn zeroing() -> Initializer {
13151316
Initializer(true)
@@ -1324,13 +1325,15 @@ impl Initializer {
13241325
/// the method accurately reflects the number of bytes that have been
13251326
/// written to the head of the buffer.
13261327
#[unstable(feature = "read_initializer", issue = "42788")]
1328+
#[must_use]
13271329
#[inline]
13281330
pub unsafe fn nop() -> Initializer {
13291331
Initializer(false)
13301332
}
13311333

13321334
/// Indicates if a buffer should be initialized.
13331335
#[unstable(feature = "read_initializer", issue = "42788")]
1336+
#[must_use]
13341337
#[inline]
13351338
pub fn should_initialize(&self) -> bool {
13361339
self.0

library/std/src/io/stdio.rs

+3
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ pub struct StdinLock<'a> {
301301
/// Ok(())
302302
/// }
303303
/// ```
304+
#[must_use]
304305
#[stable(feature = "rust1", since = "1.0.0")]
305306
pub fn stdin() -> Stdin {
306307
static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
@@ -673,6 +674,7 @@ static STDOUT: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = Sy
673674
/// Ok(())
674675
/// }
675676
/// ```
677+
#[must_use]
676678
#[stable(feature = "rust1", since = "1.0.0")]
677679
pub fn stdout() -> Stdout {
678680
Stdout {
@@ -953,6 +955,7 @@ pub struct StderrLock<'a> {
953955
/// Ok(())
954956
/// }
955957
/// ```
958+
#[must_use]
956959
#[stable(feature = "rust1", since = "1.0.0")]
957960
pub fn stderr() -> Stderr {
958961
// Note that unlike `stdout()` we don't use `at_exit` here to register a

library/std/src/io/util.rs

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct Empty;
3232
/// io::empty().read_to_string(&mut buffer).unwrap();
3333
/// assert!(buffer.is_empty());
3434
/// ```
35+
#[must_use]
3536
#[stable(feature = "rust1", since = "1.0.0")]
3637
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
3738
pub const fn empty() -> Empty {
@@ -112,6 +113,7 @@ pub struct Repeat {
112113
/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
113114
/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
114115
/// ```
116+
#[must_use]
115117
#[stable(feature = "rust1", since = "1.0.0")]
116118
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
117119
pub const fn repeat(byte: u8) -> Repeat {
@@ -192,6 +194,7 @@ pub struct Sink;
192194
/// let num_bytes = io::sink().write(&buffer).unwrap();
193195
/// assert_eq!(num_bytes, 5);
194196
/// ```
197+
#[must_use]
195198
#[stable(feature = "rust1", since = "1.0.0")]
196199
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
197200
pub const fn sink() -> Sink {

library/std/src/net/addr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ impl SocketAddr {
149149
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
150150
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
151151
/// ```
152+
#[must_use]
152153
#[stable(feature = "ip_addr", since = "1.7.0")]
153154
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
154155
pub const fn ip(&self) -> IpAddr {
@@ -189,6 +190,7 @@ impl SocketAddr {
189190
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
190191
/// assert_eq!(socket.port(), 8080);
191192
/// ```
193+
#[must_use]
192194
#[stable(feature = "rust1", since = "1.0.0")]
193195
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
194196
pub const fn port(&self) -> u16 {
@@ -297,6 +299,7 @@ impl SocketAddrV4 {
297299
/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
298300
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
299301
/// ```
302+
#[must_use]
300303
#[stable(feature = "rust1", since = "1.0.0")]
301304
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
302305
pub const fn ip(&self) -> &Ipv4Addr {
@@ -331,6 +334,7 @@ impl SocketAddrV4 {
331334
/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
332335
/// assert_eq!(socket.port(), 8080);
333336
/// ```
337+
#[must_use]
334338
#[stable(feature = "rust1", since = "1.0.0")]
335339
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
336340
pub const fn port(&self) -> u16 {
@@ -396,6 +400,7 @@ impl SocketAddrV6 {
396400
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
397401
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
398402
/// ```
403+
#[must_use]
399404
#[stable(feature = "rust1", since = "1.0.0")]
400405
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
401406
pub const fn ip(&self) -> &Ipv6Addr {
@@ -428,6 +433,7 @@ impl SocketAddrV6 {
428433
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
429434
/// assert_eq!(socket.port(), 8080);
430435
/// ```
436+
#[must_use]
431437
#[stable(feature = "rust1", since = "1.0.0")]
432438
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
433439
pub const fn port(&self) -> u16 {
@@ -470,6 +476,7 @@ impl SocketAddrV6 {
470476
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0);
471477
/// assert_eq!(socket.flowinfo(), 10);
472478
/// ```
479+
#[must_use]
473480
#[stable(feature = "rust1", since = "1.0.0")]
474481
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
475482
pub const fn flowinfo(&self) -> u32 {
@@ -509,6 +516,7 @@ impl SocketAddrV6 {
509516
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78);
510517
/// assert_eq!(socket.scope_id(), 78);
511518
/// ```
519+
#[must_use]
512520
#[stable(feature = "rust1", since = "1.0.0")]
513521
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
514522
pub const fn scope_id(&self) -> u32 {

0 commit comments

Comments
 (0)