Skip to content

Commit

Permalink
Improve function checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Licenser committed Oct 21, 2019
1 parent c9340a5 commit f343896
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions clippy_lints/src/exit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{match_def_path, paths, qpath_res, span_lint};
use crate::utils::{is_entrypoint_fn, match_def_path, paths, qpath_res, span_lint};
use if_chain::if_chain;
use rustc::hir::{Expr, ExprKind, Item, ItemKind, Node};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
Expand Down Expand Up @@ -40,7 +40,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
Some(Node::Item(Item{ident, kind: ItemKind::Fn(..), ..})) => {
// If we found a function we check it's name if it is
// `main` we emit a lint.
if ident.name.as_str() != "main" {
let def_id = cx.tcx.hir().local_def_id(parent);
if !is_entrypoint_fn(cx, def_id) {
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
}
// We found any kind of function and can end our loop
Expand All @@ -49,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
// If we found anything but a funciton we continue with the
// loop and go one parent up
Some(_) => {
cx.tcx.hir().get_parent_item(parent);
parent = cx.tcx.hir().get_parent_item(parent);
},
// If we found nothing we break.
None => break,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/exit.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#[warn(clippy::exit)]

fn not_main() {
if true {
std::process::exit(4);
}
}

fn also_not_main() {
std::process::exit(3);
}

fn main() {
if true {
std::process::exit(2);
};
also_not_main();
not_main();
std::process::exit(1);
}

0 comments on commit f343896

Please sign in to comment.