Skip to content

Return ExitCode from main instead of calling process::exit - #17977

Merged
blyxyas merged 1 commit into
rust-lang:mainfrom
ChrisDenton:exit-code
Nov 6, 2026
Merged

blyxyas merged 1 commit into
rust-lang:mainfrom
ChrisDenton:exit-code

Conversation

@ChrisDenton

Copy link
Copy Markdown
Member

changelog: none

This replaces uses of process::exit in main with returning ExitCode from main. process::exit is fraught, it doesn't run local drops and many not run thread local drops. It also has potential soundness issues (e.g. rust-lang/rust#126600).

To be clear, I do not think clippy is affected by any issues but it'd be good to follow best practice, especially as things may evolve in the future. And as a side-effect this does provide more descriptive names for the common success and failure exit codes.

This PR does not change the lintcheck or clippy_dev crates but I might follow up on that if this is accepted.

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 4, 2026
@rustbot

rustbot commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

Comment thread src/main.rs
Comment on lines +133 to +141
let code = exit_status.code().unwrap_or(-1);
cfg_select! {
// Unlike unix platforms, Windows uses the full 32-bits for exit codes.
windows => {
use std::os::windows::process::ExitCodeExt;
ExitCode::from_raw(code as u32)
},
_ => ExitCode::from(code as u8),
}

@ChrisDenton ChrisDenton Nov 4, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part I'm least happy with. But I am working on having the standard library provide a built-in conversion from ExitStatus to ExitCode. So hopefully it can eventually be replaced.

View changes since the review

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Community review: LGTM

View changes since this review

@CommanderStorm

Copy link
Copy Markdown
Contributor

do note that CI has some complaints that will need to be fixed ^^

@rustbot

This comment has been minimized.

@rustbot

rustbot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ChrisDenton

Copy link
Copy Markdown
Member Author

do note that CI has some complaints that will need to be fixed ^^

Thanks, fixed and rebased!

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Community review: Thank you, very nice

View changes since this review

Comment thread src/main.rs Resolved
Comment thread src/main.rs
@@ -1,54 +1,43 @@
// We need this feature as it changes `dylib` linking behavior and allows us to link to
// `rustc_driver`.
// We need this feature as it changes `dylib` linking behavior.

@blyxyas blyxyas Nov 6, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I really like not needing rustc_driver here anymore

View changes since the review

@blyxyas blyxyas left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Just a couple of changes and I'll merge

View changes since this review

Comment thread src/main.rs Outdated
Comment on lines +12 to +14
match writeln!(&mut anstream::stdout().lock(), "{}", help_message()) {
Ok(()) => ExitCode::SUCCESS,
Err(_) => ExitCode::FAILURE,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the Termination trait here?

Suggested change
match writeln!(&mut anstream::stdout().lock(), "{}", help_message()) {
Ok(()) => ExitCode::SUCCESS,
Err(_) => ExitCode::FAILURE,
writeln!(&mut anstream::stdout().lock(), "{}", help_message()).report()

Comment thread src/main.rs Outdated
Comment on lines +20 to +22
match writeln!(&mut anstream::stdout().lock(), "{version_info}") {
Ok(()) => ExitCode::SUCCESS,
Err(_) => ExitCode::FAILURE,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 6, 2026
@ChrisDenton

Copy link
Copy Markdown
Member Author

I've now made use of Termination::report instead of manually implementing it. @rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 6, 2026

@blyxyas blyxyas left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! ❤️

View changes since this review

@rustbot rustbot removed the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Nov 6, 2026
@blyxyas
blyxyas added this pull request to the merge queue Nov 6, 2026
Merged via the queue into rust-lang:main with commit 07421c8 Nov 6, 2026
13 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants