Skip to content

skip exit late lint pass on tests #15222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Jared-Prime
Copy link

@Jared-Prime Jared-Prime commented Jul 7, 2025

changelog: [exit]: When using the --test or --all-targets flag, the exit lint should not fail on the main function.

Fixes #13518

With help from @sesgoe

When using the `--test` or `--all-targets` flag, the exit lint should not fail on the main function.
@rustbot
Copy link
Collaborator

rustbot commented Jul 7, 2025

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 7, 2025
Comment on lines 46 to 49
if cx.sess().is_test_crate() {
return;
}

Copy link
Member

Choose a reason for hiding this comment

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

This suppresses the lint entirely in --test sessions and will not emit a lint anywhere in the crate, even if we aren't actually in a main fn and we should emit a warning, right?

I think that would interact confusingly with #[expect] and create a situation similar to #13358, where if you have something like:

fn foo() {
  #[expect(clippy::exit)]
  std::process::exit();
}

and run cargo clippy --all-targets, then you get an unfulfilled lint expectation, but if you remove the #[expect], you do get the warning there.

--all-targets runs clippy twice, once without --test and again with --test, so only sometimes emitting a warning dependent on the session (like is done here) is a bit confusing for users IMO

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if, instead of using !is_entrypoint_fn at the end of the condition chain, which I guess is also dependent on the session and different for the test harness, we could just change that to check if the enclosing function's name is not literally "main". That seems closer to what we actually want and would be more consistent between compiler sessions

Copy link

Choose a reason for hiding this comment

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

@y21 I had the same thought after reading your first comment, and upon consideration, this seems like a good way to go

I saw the if cx.sess().is_test_crate() { return; } pattern elsewhere, which is why it seemed reasonable to slot it in here too -- the only real thought was: in a test context, is this lint relevant?

The confusing interaction with --all-targets is worth fixing, and checking that the name is literally main makes it clear in both compilation contexts/sessions, so that feels like a good solution.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, @y21 . I added the changes and documentation updates recommended by @sesgoe then removed the blanket skip for test crates. Let me know if anything else required here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The exit lint is emitted for test target but not for bin target
5 participants