]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/exit.rs
Add lint for exit
[rust.git] / clippy_lints / src / exit.rs
1 use crate::utils::{match_def_path, paths, qpath_res, span_lint};
2 use if_chain::if_chain;
3 use rustc::hir::{Expr, ExprKind};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
6
7 declare_clippy_lint! {
8     /// **What it does:** `exit()`  terminates the program and doesn't provide a
9     /// stack trace.
10     ///
11     /// **Why is this bad?** Ideally a program is terminated by finishing
12     /// the main function.
13     ///
14     /// **Known problems:** This can be valid code in main() to return
15     /// errors
16     ///
17     /// **Example:**
18     /// ```ignore
19     /// std::process::exit(0)
20     /// ```
21     pub EXIT,
22     restriction,
23     "`std::process::exit` is called, terminating the program"
24 }
25
26 declare_lint_pass!(Exit => [EXIT]);
27
28 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
29     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
30         if_chain! {
31             if let ExprKind::Call(ref path_expr, ref _args) = e.kind;
32             if let ExprKind::Path(ref path) = path_expr.kind;
33             if let Some(def_id) = qpath_res(cx, path, path_expr.hir_id).opt_def_id();
34             if match_def_path(cx, def_id, &paths::EXIT);
35             then {
36                 span_lint(cx, EXIT, e.span, "usage of `process::exit`");
37             }
38
39         }
40     }
41 }