Skip to content

Consider adding #[must_use] to std::process::ExitStatus #73127

Open
@ijackson

Description

@ijackson

Hi. Normally, Rust makes it difficult to accidentally write code which ignores errors. This is one of its great strengths. However, the std::process API requires the user to explicitly check the process exit status.

(This issue is about the return value from wait(). I am filing another one about output(). See also #70186 which is about the return value from spawn().)

Here's a demo of the problem.

use std::process::*;

fn main() {
    let command = ["ls","--no-such-option"];
    Command::new(command[0])
                     .args(&command[1..])
                     .spawn().expect("Failed spawn")
                     .wait().expect("Failed wait");
    println!("I ran {:?}", &command);
    
    println!("All went well!")
}

(Playground)

It produces this output:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.98s
     Running `target/debug/playground`
ls: unrecognized option '--no-such-option'
Try 'ls --help' for more information.

Standard Output

I ran ["ls", "--no-such-option"]
All went well!

This is not optimal. I think making ExitStatus be #[must_use] would be the best way to address this. The code above would then generate a compiler error. The programmer would be prompted to make an actual decision about the exit status.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-processArea: `std::process` and `std::env`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions