Skip to content

Commit f343896

Browse files
committed
Improve function checking
1 parent c9340a5 commit f343896

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

clippy_lints/src/exit.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{match_def_path, paths, qpath_res, span_lint};
1+
use crate::utils::{is_entrypoint_fn, match_def_path, paths, qpath_res, span_lint};
22
use if_chain::if_chain;
33
use rustc::hir::{Expr, ExprKind, Item, ItemKind, Node};
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -40,7 +40,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
4040
Some(Node::Item(Item{ident, kind: ItemKind::Fn(..), ..})) => {
4141
// If we found a function we check it's name if it is
4242
// `main` we emit a lint.
43-
if ident.name.as_str() != "main" {
43+
let def_id = cx.tcx.hir().local_def_id(parent);
44+
if !is_entrypoint_fn(cx, def_id) {
4445
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
4546
}
4647
// We found any kind of function and can end our loop
@@ -49,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
4950
// If we found anything but a funciton we continue with the
5051
// loop and go one parent up
5152
Some(_) => {
52-
cx.tcx.hir().get_parent_item(parent);
53+
parent = cx.tcx.hir().get_parent_item(parent);
5354
},
5455
// If we found nothing we break.
5556
None => break,

tests/ui/exit.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
#[warn(clippy::exit)]
2+
23
fn not_main() {
4+
if true {
5+
std::process::exit(4);
6+
}
7+
}
8+
9+
fn also_not_main() {
310
std::process::exit(3);
411
}
512

613
fn main() {
714
if true {
815
std::process::exit(2);
916
};
17+
also_not_main();
1018
not_main();
1119
std::process::exit(1);
1220
}

0 commit comments

Comments
 (0)