-
Notifications
You must be signed in to change notification settings - Fork 1
fix: check against 'main' function name instead of entrypoint function #1
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
fix: check against 'main' function name instead of entrypoint function #1
Conversation
update docs to be a bit more clear
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()` |
There was a problem hiding this comment.
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); | ||
/// } |
There was a problem hiding this comment.
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`" |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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!
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:
main
--test
flag to ensure this works correctly regardless of compilation context