Skip to content

Commit

Permalink
Rollup merge of #131833 - c-ryan747:patch-1, r=Noratrieb
Browse files Browse the repository at this point in the history
Add `must_use` to `CommandExt::exec`

[CommandExt::exec](https://fburl.com/0qhpo7nu) returns a `std::io::Error` in the case exec fails, but its not currently marked as `must_use` making it easy to accidentally ignore it.

This PR adds the `must_use` attributed here as i think it fits the definition in the guide of [When to add #[must_use]](https://std-dev-guide.rust-lang.org/policy/must-use.html#when-to-add-must_use)
  • Loading branch information
matthiaskrgr authored Oct 17, 2024
2 parents 405eb41 + 9b5b607 commit 372e8c1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions library/std/src/os/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub trait CommandExt: Sealed {
/// required to gracefully handle errors it is recommended to use the
/// cross-platform `spawn` instead.
#[stable(feature = "process_exec2", since = "1.9.0")]
#[must_use]
fn exec(&mut self) -> io::Error;

/// Set executable argument
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/command/command-exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ fn main() {
}

"exec-test2" => {
Command::new("/path/to/nowhere").exec();
let _ = Command::new("/path/to/nowhere").exec();
println!("passed");
}

"exec-test3" => {
Command::new(&me).arg("bad\0").exec();
let _ = Command::new(&me).arg("bad\0").exec();
println!("passed");
}

"exec-test4" => {
Command::new(&me).current_dir("/path/to/nowhere").exec();
let _ = Command::new(&me).current_dir("/path/to/nowhere").exec();
println!("passed");
}

"exec-test5" => {
env::set_var("VARIABLE", "ABC");
Command::new("definitely-not-a-real-binary").env("VARIABLE", "XYZ").exec();
let _ = Command::new("definitely-not-a-real-binary").env("VARIABLE", "XYZ").exec();
assert_eq!(env::var("VARIABLE").unwrap(), "ABC");
println!("passed");
}
Expand Down

0 comments on commit 372e8c1

Please sign in to comment.