]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/panic_unimplemented.rs
rustup https://github.com/rust-lang/rust/pull/61995
[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::ptr::P;
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_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 declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED]);
46
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
48     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
49         if_chain! {
50             if let ExprKind::Block(ref block, _) = expr.node;
51             if let Some(ref ex) = block.expr;
52             if let ExprKind::Call(ref fun, ref params) = ex.node;
53             if let ExprKind::Path(ref qpath) = fun.node;
54             if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
55             if match_def_path(cx, fun_def_id, &paths::BEGIN_PANIC);
56             if params.len() == 2;
57             then {
58                 if is_expn_of(expr.span, "unimplemented").is_some() {
59                     let span = get_outer_span(expr);
60                     span_lint(cx, UNIMPLEMENTED, span,
61                               "`unimplemented` should not be present in production code");
62                 } else {
63                     match_panic(params, expr, cx);
64                 }
65             }
66         }
67     }
68 }
69
70 fn get_outer_span(expr: &Expr) -> Span {
71     if_chain! {
72         if let Some(first) = expr.span.ctxt().outer_expn_info();
73         if let Some(second) = first.call_site.ctxt().outer_expn_info();
74         then {
75             second.call_site
76         } else {
77             expr.span
78         }
79     }
80 }
81
82 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext<'_, '_>) {
83     if_chain! {
84         if let ExprKind::Lit(ref lit) = params[0].node;
85         if is_direct_expn_of(expr.span, "panic").is_some();
86         if let LitKind::Str(ref string, _) = lit.node;
87         let string = string.as_str().replace("{{", "").replace("}}", "");
88         if let Some(par) = string.find('{');
89         if string[par..].contains('}');
90         if params[0].span.source_callee().is_none();
91         if params[0].span.lo() != params[0].span.hi();
92         then {
93             span_lint(cx, PANIC_PARAMS, params[0].span,
94                       "you probably are missing some parameter in your format string");
95         }
96     }
97 }