Improve function checking

This commit is contained in:
Heinz N. Gies 2019-10-21 22:55:01 +02:00
parent 9471669e46
commit ffcf4bec0f
2 changed files with 12 additions and 3 deletions

View File

@ -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};
@ -40,7 +40,8 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
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
@ -49,7 +50,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
// 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,

View File

@ -1,5 +1,12 @@
#[warn(clippy::exit)]
fn not_main() {
if true {
std::process::exit(4);
}
}
fn also_not_main() {
std::process::exit(3);
}
@ -7,6 +14,7 @@ fn main() {
if true {
std::process::exit(2);
};
also_not_main();
not_main();
std::process::exit(1);
}