Skip to content

Commit 455a79a

Browse files
authored
Rollup merge of #90431 - jkugelman:must-use-std-o-through-z, r=joshtriplett
Add #[must_use] to remaining std functions (O-Z) I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is half of the remaining items from the `std` crate, from O-Z. `panicking::take_hook` has a side effect: it unregisters the current panic hook, returning it. I almost ignored it, but the documentation example shows `let _ = panic::take_hook();`, so following suit I went ahead and added a `#[must_use]`. ```rust std::panicking fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>; ``` I added these functions that clippy did not flag: ```rust std::path::Path fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool; std::path::Path fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool; std::path::Path fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf; std::path::Path fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf; ``` Parent issue: #89692 r? `@joshtriplett`
2 parents 26f505c + a81d4b1 commit 455a79a

File tree

12 files changed

+57
-2
lines changed

12 files changed

+57
-2
lines changed

library/std/src/os/unix/net/ancillary.rs

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ impl SocketCred {
201201
}
202202

203203
/// Get the current PID.
204+
#[must_use]
204205
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
205206
pub fn get_pid(&self) -> libc::pid_t {
206207
self.0.pid
@@ -213,6 +214,7 @@ impl SocketCred {
213214
}
214215

215216
/// Get the current UID.
217+
#[must_use]
216218
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
217219
pub fn get_uid(&self) -> libc::uid_t {
218220
self.0.uid
@@ -225,6 +227,7 @@ impl SocketCred {
225227
}
226228

227229
/// Get the current GID.
230+
#[must_use]
228231
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
229232
pub fn get_gid(&self) -> libc::gid_t {
230233
self.0.gid
@@ -330,6 +333,7 @@ impl<'a> AncillaryData<'a> {
330333
}
331334

332335
/// This struct is used to iterate through the control messages.
336+
#[must_use = "iterators are lazy and do nothing unless consumed"]
333337
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
334338
pub struct Messages<'a> {
335339
buffer: &'a [u8],
@@ -425,6 +429,7 @@ impl<'a> SocketAncillary<'a> {
425429
}
426430

427431
/// Returns the capacity of the buffer.
432+
#[must_use]
428433
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
429434
pub fn capacity(&self) -> usize {
430435
self.buffer.len()
@@ -473,6 +478,7 @@ impl<'a> SocketAncillary<'a> {
473478
/// Ok(())
474479
/// }
475480
/// ```
481+
#[must_use]
476482
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
477483
pub fn truncated(&self) -> bool {
478484
self.truncated

library/std/src/os/unix/net/listener.rs

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ impl<'a> IntoIterator for &'a UnixListener {
365365
/// }
366366
/// ```
367367
#[derive(Debug)]
368+
#[must_use = "iterators are lazy and do nothing unless consumed"]
368369
#[stable(feature = "unix_socket", since = "1.10.0")]
369370
pub struct Incoming<'a> {
370371
listener: &'a UnixListener,

library/std/src/os/unix/process.rs

+1
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ impl From<crate::process::ChildStderr> for OwnedFd {
436436
}
437437

438438
/// Returns the OS-assigned process identifier associated with this process's parent.
439+
#[must_use]
439440
#[stable(feature = "unix_ppid", since = "1.27.0")]
440441
pub fn parent_id() -> u32 {
441442
crate::sys::os::getppid()

library/std/src/panicking.rs

+3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pub fn set_hook(hook: Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>) {
160160
///
161161
/// panic!("Normal panic");
162162
/// ```
163+
#[must_use]
163164
#[stable(feature = "panic_hooks", since = "1.10.0")]
164165
pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
165166
if thread::panicking() {
@@ -284,11 +285,13 @@ pub mod panic_count {
284285
}
285286

286287
// Disregards ALWAYS_ABORT_FLAG
288+
#[must_use]
287289
pub fn get_count() -> usize {
288290
LOCAL_PANIC_COUNT.with(|c| c.get())
289291
}
290292

291293
// Disregards ALWAYS_ABORT_FLAG
294+
#[must_use]
292295
#[inline]
293296
pub fn count_is_zero() -> bool {
294297
if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) & !ALWAYS_ABORT_FLAG == 0 {

library/std/src/path.rs

+15
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ impl<'a> PrefixComponent<'a> {
422422
/// See [`Prefix`]'s documentation for more information on the different
423423
/// kinds of prefixes.
424424
#[stable(feature = "rust1", since = "1.0.0")]
425+
#[must_use]
425426
#[inline]
426427
pub fn kind(&self) -> Prefix<'a> {
427428
self.parsed
@@ -583,6 +584,7 @@ impl AsRef<Path> for Component<'_> {
583584
///
584585
/// [`components`]: Path::components
585586
#[derive(Clone)]
587+
#[must_use = "iterators are lazy and do nothing unless consumed"]
586588
#[stable(feature = "rust1", since = "1.0.0")]
587589
pub struct Components<'a> {
588590
// The path left to parse components from
@@ -609,6 +611,7 @@ pub struct Components<'a> {
609611
///
610612
/// [`iter`]: Path::iter
611613
#[derive(Clone)]
614+
#[must_use = "iterators are lazy and do nothing unless consumed"]
612615
#[stable(feature = "rust1", since = "1.0.0")]
613616
pub struct Iter<'a> {
614617
inner: Components<'a>,
@@ -1051,6 +1054,7 @@ fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cm
10511054
///
10521055
/// [`ancestors`]: Path::ancestors
10531056
#[derive(Copy, Clone, Debug)]
1057+
#[must_use = "iterators are lazy and do nothing unless consumed"]
10541058
#[stable(feature = "path_ancestors", since = "1.28.0")]
10551059
pub struct Ancestors<'a> {
10561060
next: Option<&'a Path>,
@@ -1459,6 +1463,7 @@ impl PathBuf {
14591463
///
14601464
/// [`capacity`]: OsString::capacity
14611465
#[stable(feature = "path_buf_capacity", since = "1.44.0")]
1466+
#[must_use]
14621467
#[inline]
14631468
pub fn capacity(&self) -> usize {
14641469
self.inner.capacity()
@@ -2103,6 +2108,7 @@ impl Path {
21032108
/// assert_eq!(grand_parent.parent(), None);
21042109
/// ```
21052110
#[stable(feature = "rust1", since = "1.0.0")]
2111+
#[must_use]
21062112
pub fn parent(&self) -> Option<&Path> {
21072113
let mut comps = self.components();
21082114
let comp = comps.next_back();
@@ -2169,6 +2175,7 @@ impl Path {
21692175
/// assert_eq!(None, Path::new("/").file_name());
21702176
/// ```
21712177
#[stable(feature = "rust1", since = "1.0.0")]
2178+
#[must_use]
21722179
pub fn file_name(&self) -> Option<&OsStr> {
21732180
self.components().next_back().and_then(|p| match p {
21742181
Component::Normal(p) => Some(p),
@@ -2241,6 +2248,7 @@ impl Path {
22412248
/// assert!(!Path::new("/etc/foo.rs").starts_with("/etc/foo"));
22422249
/// ```
22432250
#[stable(feature = "rust1", since = "1.0.0")]
2251+
#[must_use]
22442252
pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
22452253
self._starts_with(base.as_ref())
22462254
}
@@ -2268,6 +2276,7 @@ impl Path {
22682276
/// assert!(!path.ends_with("conf")); // use .extension() instead
22692277
/// ```
22702278
#[stable(feature = "rust1", since = "1.0.0")]
2279+
#[must_use]
22712280
pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
22722281
self._ends_with(child.as_ref())
22732282
}
@@ -2303,6 +2312,7 @@ impl Path {
23032312
/// [`Path::file_prefix`]: Path::file_prefix
23042313
///
23052314
#[stable(feature = "rust1", since = "1.0.0")]
2315+
#[must_use]
23062316
pub fn file_stem(&self) -> Option<&OsStr> {
23072317
self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after))
23082318
}
@@ -2336,6 +2346,7 @@ impl Path {
23362346
/// [`Path::file_stem`]: Path::file_stem
23372347
///
23382348
#[unstable(feature = "path_file_prefix", issue = "86319")]
2349+
#[must_use]
23392350
pub fn file_prefix(&self) -> Option<&OsStr> {
23402351
self.file_name().map(split_file_at_dot).and_then(|(before, _after)| Some(before))
23412352
}
@@ -2360,6 +2371,7 @@ impl Path {
23602371
/// assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap());
23612372
/// ```
23622373
#[stable(feature = "rust1", since = "1.0.0")]
2374+
#[must_use]
23632375
pub fn extension(&self) -> Option<&OsStr> {
23642376
self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.and(after))
23652377
}
@@ -2403,6 +2415,7 @@ impl Path {
24032415
/// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
24042416
/// ```
24052417
#[stable(feature = "rust1", since = "1.0.0")]
2418+
#[must_use]
24062419
pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
24072420
self._with_file_name(file_name.as_ref())
24082421
}
@@ -2660,6 +2673,7 @@ impl Path {
26602673
/// This is a convenience function that coerces errors to false. If you want to
26612674
/// check errors, call [`fs::metadata`].
26622675
#[stable(feature = "path_ext", since = "1.5.0")]
2676+
#[must_use]
26632677
#[inline]
26642678
pub fn exists(&self) -> bool {
26652679
fs::metadata(self).is_ok()
@@ -2786,6 +2800,7 @@ impl Path {
27862800
/// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
27872801
/// allocating.
27882802
#[stable(feature = "into_boxed_path", since = "1.20.0")]
2803+
#[must_use = "`self` will be dropped if the result is not used"]
27892804
pub fn into_path_buf(self: Box<Path>) -> PathBuf {
27902805
let rw = Box::into_raw(self) as *mut OsStr;
27912806
let inner = unsafe { Box::from_raw(rw) };

library/std/src/process.rs

+13
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ impl Command {
948948
/// let cmd = Command::new("echo");
949949
/// assert_eq!(cmd.get_program(), "echo");
950950
/// ```
951+
#[must_use]
951952
#[stable(feature = "command_access", since = "1.57.0")]
952953
pub fn get_program(&self) -> &OsStr {
953954
self.inner.get_program()
@@ -1021,6 +1022,7 @@ impl Command {
10211022
/// cmd.current_dir("/bin");
10221023
/// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
10231024
/// ```
1025+
#[must_use]
10241026
#[stable(feature = "command_access", since = "1.57.0")]
10251027
pub fn get_current_dir(&self) -> Option<&Path> {
10261028
self.inner.get_current_dir()
@@ -1053,6 +1055,7 @@ impl AsInnerMut<imp::Command> for Command {
10531055
///
10541056
/// This struct is created by [`Command::get_args`]. See its documentation for
10551057
/// more.
1058+
#[must_use = "iterators are lazy and do nothing unless consumed"]
10561059
#[stable(feature = "command_access", since = "1.57.0")]
10571060
#[derive(Debug)]
10581061
pub struct CommandArgs<'a> {
@@ -1183,6 +1186,7 @@ impl Stdio {
11831186
/// its entire stdin before writing more than a pipe buffer's worth of output.
11841187
/// The size of a pipe buffer varies on different targets.
11851188
///
1189+
#[must_use]
11861190
#[stable(feature = "process", since = "1.0.0")]
11871191
pub fn piped() -> Stdio {
11881192
Stdio(imp::Stdio::MakePipe)
@@ -1222,6 +1226,7 @@ impl Stdio {
12221226
/// print!("You piped in the reverse of: ");
12231227
/// io::stdout().write_all(&output.stdout).unwrap();
12241228
/// ```
1229+
#[must_use]
12251230
#[stable(feature = "process", since = "1.0.0")]
12261231
pub fn inherit() -> Stdio {
12271232
Stdio(imp::Stdio::Inherit)
@@ -1261,6 +1266,7 @@ impl Stdio {
12611266
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "");
12621267
/// // Ignores any piped-in input
12631268
/// ```
1269+
#[must_use]
12641270
#[stable(feature = "process", since = "1.0.0")]
12651271
pub fn null() -> Stdio {
12661272
Stdio(imp::Stdio::Null)
@@ -1462,6 +1468,7 @@ impl ExitStatus {
14621468
/// println!("failed to create 'projects/' directory: {}", status);
14631469
/// }
14641470
/// ```
1471+
#[must_use]
14651472
#[stable(feature = "process", since = "1.0.0")]
14661473
pub fn success(&self) -> bool {
14671474
self.0.exit_ok().is_ok()
@@ -1493,6 +1500,7 @@ impl ExitStatus {
14931500
/// None => println!("Process terminated by signal")
14941501
/// }
14951502
/// ```
1503+
#[must_use]
14961504
#[stable(feature = "process", since = "1.0.0")]
14971505
pub fn code(&self) -> Option<i32> {
14981506
self.0.code()
@@ -1580,6 +1588,7 @@ impl ExitStatusError {
15801588
/// assert_eq!(bad.code(), Some(1));
15811589
/// # } // #[cfg(unix)]
15821590
/// ```
1591+
#[must_use]
15831592
pub fn code(&self) -> Option<i32> {
15841593
self.code_nonzero().map(Into::into)
15851594
}
@@ -1605,11 +1614,13 @@ impl ExitStatusError {
16051614
/// assert_eq!(bad.code_nonzero().unwrap(), NonZeroI32::try_from(1).unwrap());
16061615
/// # } // cfg!(unix)
16071616
/// ```
1617+
#[must_use]
16081618
pub fn code_nonzero(&self) -> Option<NonZeroI32> {
16091619
self.0.code()
16101620
}
16111621

16121622
/// Converts an `ExitStatusError` (back) to an `ExitStatus`.
1623+
#[must_use]
16131624
pub fn into_status(&self) -> ExitStatus {
16141625
ExitStatus(self.0.into())
16151626
}
@@ -1718,6 +1729,7 @@ impl Child {
17181729
/// println!("ls command didn't start");
17191730
/// }
17201731
/// ```
1732+
#[must_use]
17211733
#[stable(feature = "process_id", since = "1.3.0")]
17221734
pub fn id(&self) -> u32 {
17231735
self.handle.id()
@@ -1988,6 +2000,7 @@ pub fn abort() -> ! {
19882000
/// ```
19892001
///
19902002
///
2003+
#[must_use]
19912004
#[stable(feature = "getpid", since = "1.26.0")]
19922005
pub fn id() -> u32 {
19932006
crate::sys::os::getpid()

library/std/src/sync/condvar.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl WaitTimeoutResult {
6161
/// }
6262
/// }
6363
/// ```
64+
#[must_use]
6465
#[stable(feature = "wait_timeout", since = "1.5.0")]
6566
pub fn timed_out(&self) -> bool {
6667
self.0

library/std/src/sync/mpsc/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ impl<T> UnsafeFlavor<T> for Receiver<T> {
707707
/// // Let's see what that answer was
708708
/// println!("{:?}", receiver.recv().unwrap());
709709
/// ```
710+
#[must_use]
710711
#[stable(feature = "rust1", since = "1.0.0")]
711712
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
712713
let a = Arc::new(oneshot::Packet::new());
@@ -755,6 +756,7 @@ pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
755756
/// assert_eq!(receiver.recv().unwrap(), 1);
756757
/// assert_eq!(receiver.recv().unwrap(), 2);
757758
/// ```
759+
#[must_use]
758760
#[stable(feature = "rust1", since = "1.0.0")]
759761
pub fn sync_channel<T>(bound: usize) -> (SyncSender<T>, Receiver<T>) {
760762
let a = Arc::new(sync::Packet::new(bound));

library/std/src/sys_common/process.rs

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl CommandEnv {
106106
/// This struct is created by
107107
/// [`Command::get_envs`][crate::process::Command::get_envs]. See its
108108
/// documentation for more.
109+
#[must_use = "iterators are lazy and do nothing unless consumed"]
109110
#[stable(feature = "command_access", since = "1.57.0")]
110111
#[derive(Debug)]
111112
pub struct CommandEnvs<'a> {

library/std/src/thread/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ where
650650
///
651651
/// handler.join().unwrap();
652652
/// ```
653+
#[must_use]
653654
#[stable(feature = "rust1", since = "1.0.0")]
654655
pub fn current() -> Thread {
655656
thread_info::current_thread().expect(
@@ -738,6 +739,7 @@ pub fn yield_now() {
738739
///
739740
/// [Mutex]: crate::sync::Mutex
740741
#[inline]
742+
#[must_use]
741743
#[stable(feature = "rust1", since = "1.0.0")]
742744
pub fn panicking() -> bool {
743745
panicking::panicking()
@@ -1132,6 +1134,7 @@ impl Thread {
11321134
/// assert!(thread::current().id() != other_thread_id);
11331135
/// ```
11341136
#[stable(feature = "thread_id", since = "1.19.0")]
1137+
#[must_use]
11351138
pub fn id(&self) -> ThreadId {
11361139
self.inner.id
11371140
}
@@ -1174,6 +1177,7 @@ impl Thread {
11741177
///
11751178
/// [naming-threads]: ./index.html#naming-threads
11761179
#[stable(feature = "rust1", since = "1.0.0")]
1180+
#[must_use]
11771181
pub fn name(&self) -> Option<&str> {
11781182
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
11791183
}
@@ -1361,6 +1365,7 @@ impl<T> JoinHandle<T> {
13611365
/// println!("thread id: {:?}", thread.id());
13621366
/// ```
13631367
#[stable(feature = "rust1", since = "1.0.0")]
1368+
#[must_use]
13641369
pub fn thread(&self) -> &Thread {
13651370
&self.0.thread
13661371
}

0 commit comments

Comments
 (0)