Skip to content

Conversation

sesgoe
Copy link

@sesgoe sesgoe commented Jul 7, 2025

Hey @Jared-Prime!

Here's a quick PR to update your PR with the updates suggested by y21 in your PR.

This changes it to:

  • check that the function name is literally main
  • updates docs to be a bit more clear about how this works
  • adds tests that all run with the --test flag to ensure this works correctly regardless of compilation context

use rustc_hir::{Expr, ExprKind, Item, ItemKind, OwnerNode};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::declare_lint_pass;
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
/// Detects calls to the `exit()` function which terminates the program.
/// Detects calls to the `exit()` function that are not in the `main` function. Calls to `exit()`
Copy link
Owner

Choose a reason for hiding this comment

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

👍🏽

///
/// fn other_function() {
/// std::process::exit(0);
/// }
Copy link
Owner

Choose a reason for hiding this comment

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

👍🏽

@@ -36,7 +49,7 @@ declare_clippy_lint! {
#[clippy::version = "1.41.0"]
pub EXIT,
restriction,
"detects `std::process::exit` calls"
"detects `std::process::exit` calls outside of `main`"
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the clarifications!

// if you instead check for the parent of the `exit()` call being the entrypoint function, as this worked before,
// in compilation contexts like --all-targets (which include --tests), you get false positives
// because in a test context, main is not the entrypoint function
&& ident.name.as_str() != "main"
Copy link
Owner

Choose a reason for hiding this comment

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

OK... so we need to get the identity of the "next item up", inspect its literal name, and check that it's not named "main". Moreover, we're saying this avoids the false positive described by rust-lang#13358

I think I get it. Thanks!

@Jared-Prime Jared-Prime merged commit 52aa383 into Jared-Prime:fix_issue_13518 Jul 7, 2025
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.

2 participants