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