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