Skip to content

Commit c9340a5

Browse files
committed
Exclude main from exit lint
1 parent 994de9c commit c9340a5

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

clippy_lints/src/exit.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{match_def_path, paths, qpath_res, span_lint};
22
use if_chain::if_chain;
3-
use rustc::hir::{Expr, ExprKind};
3+
use rustc::hir::{Expr, ExprKind, Item, ItemKind, Node};
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
55
use rustc::{declare_lint_pass, declare_tool_lint};
66

@@ -11,8 +11,7 @@ declare_clippy_lint! {
1111
/// **Why is this bad?** Ideally a program is terminated by finishing
1212
/// the main function.
1313
///
14-
/// **Known problems:** This can be valid code in main() to return
15-
/// errors
14+
/// **Known problems:** None.
1615
///
1716
/// **Example:**
1817
/// ```ignore
@@ -33,7 +32,29 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
3332
if let Some(def_id) = qpath_res(cx, path, path_expr.hir_id).opt_def_id();
3433
if match_def_path(cx, def_id, &paths::EXIT);
3534
then {
36-
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
35+
let mut parent = cx.tcx.hir().get_parent_item(e.hir_id);
36+
// We have to traverse the parents upwards until we find a function
37+
// otherwise a exit in a let or if in main would still trigger this
38+
loop{
39+
match cx.tcx.hir().find(parent) {
40+
Some(Node::Item(Item{ident, kind: ItemKind::Fn(..), ..})) => {
41+
// If we found a function we check it's name if it is
42+
// `main` we emit a lint.
43+
if ident.name.as_str() != "main" {
44+
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
45+
}
46+
// We found any kind of function and can end our loop
47+
break;
48+
}
49+
// If we found anything but a funciton we continue with the
50+
// loop and go one parent up
51+
Some(_) => {
52+
cx.tcx.hir().get_parent_item(parent);
53+
},
54+
// If we found nothing we break.
55+
None => break,
56+
}
57+
}
3758
}
3859

3960
}

tests/ui/exit.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
#[warn(clippy::exit)]
2+
fn not_main() {
3+
std::process::exit(3);
4+
}
5+
26
fn main() {
7+
if true {
8+
std::process::exit(2);
9+
};
10+
not_main();
311
std::process::exit(1);
412
}

tests/ui/exit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: usage of `process::exit`
22
--> $DIR/exit.rs:3:5
33
|
4-
LL | std::process::exit(1);
4+
LL | std::process::exit(3);
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::exit` implied by `-D warnings`

0 commit comments

Comments
 (0)