]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/panic.rs
Create lint for unimplemented!()
[rust.git] / clippy_lints / src / panic.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use syntax::ast::LitKind;
4 use syntax::ptr::P;
5 use utils::{is_direct_expn_of, is_expn_of, match_def_path, opt_def_id, paths, resolve_node, span_lint};
6
7 /// **What it does:** Checks for missing parameters in `panic!`.
8 ///
9 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
10 /// two forms of `panic!`: if there are no parameters given, the first argument
11 /// is not a format string and used literally. So while `format!("{}")` will
12 /// fail to compile, `panic!("{}")` will not.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// panic!("This `panic!` is probably missing a parameter there: {}");
19 /// ```
20 declare_clippy_lint! {
21     pub PANIC_PARAMS,
22     style,
23     "missing parameters in `panic!` calls"
24 }
25
26 /// **What it does:** Checks for usage of `unimplemented!`.
27 ///
28 /// **Why is this bad?** This macro should not be present in production code
29 ///
30 /// **Known problems:** None.
31 ///
32 /// **Example:**
33 /// ```rust
34 /// unimplemented!();
35 /// ```
36 declare_clippy_lint! {
37     pub UNIMPLEMENTED,
38     style,
39     "`unimplemented!` should not be present in production code"
40 }
41
42 #[allow(missing_copy_implementations)]
43 pub struct Pass;
44
45 impl LintPass for Pass {
46     fn get_lints(&self) -> LintArray {
47         lint_array!(PANIC_PARAMS, UNIMPLEMENTED)
48     }
49 }
50
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
52     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
53         if_chain! {
54             if let ExprBlock(ref block, _) = expr.node;
55             if let Some(ref ex) = block.expr;
56             if let ExprCall(ref fun, ref params) = ex.node;
57             if let ExprPath(ref qpath) = fun.node;
58             if let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id));
59             if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
60             if params.len() == 2;
61             then {
62                 if is_expn_of(expr.span, "unimplemented").is_some() {
63                     span_lint(cx, UNIMPLEMENTED, expr.span,
64                               "`unimplemented` should not be present in production code");
65                 } else {
66                     match_panic(params, expr, cx);
67                 }
68             }
69         }
70     }
71 }
72
73 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext) {
74     if_chain! {
75         if let ExprLit(ref lit) = params[0].node;
76         if is_direct_expn_of(expr.span, "panic").is_some();
77         if let LitKind::Str(ref string, _) = lit.node;
78         let string = string.as_str().replace("{{", "").replace("}}", "");
79         if let Some(par) = string.find('{');
80         if string[par..].contains('}');
81         if params[0].span.source_callee().is_none();
82         if params[0].span.lo() != params[0].span.hi();
83         then {
84             span_lint(cx, PANIC_PARAMS, params[0].span,
85                       "you probably are missing some parameter in your format string");
86         }
87     }
88 }