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