Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve function checking
  • Loading branch information
Licenser committed Nov 7, 2019
commit ffcf4bec0f9785925b12bce49149997b1ba5c79e
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);
}