From ffcf4bec0f9785925b12bce49149997b1ba5c79e Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Mon, 21 Oct 2019 22:55:01 +0200 Subject: [PATCH] Improve function checking --- clippy_lints/src/exit.rs | 7 ++++--- tests/ui/exit.rs | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/exit.rs b/clippy_lints/src/exit.rs index 9952ef3ea62..23952efbc8d 100644 --- a/clippy_lints/src/exit.rs +++ b/clippy_lints/src/exit.rs @@ -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, diff --git a/tests/ui/exit.rs b/tests/ui/exit.rs index bf8ea9bebf7..0ad15faef77 100644 --- a/tests/ui/exit.rs +++ b/tests/ui/exit.rs @@ -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); }