]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/panic_unimplemented.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / clippy_lints / src / panic_unimplemented.rs
1 use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, paths, span_lint};
2 use if_chain::if_chain;
3 use rustc_hir::*;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::Span;
7 use syntax::ast::LitKind;
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for missing parameters in `panic!`.
11     ///
12     /// **Why is this bad?** Contrary to the `format!` family of macros, there are
13     /// two forms of `panic!`: if there are no parameters given, the first argument
14     /// is not a format string and used literally. So while `format!("{}")` will
15     /// fail to compile, `panic!("{}")` will not.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     /// ```no_run
21     /// panic!("This `panic!` is probably missing a parameter there: {}");
22     /// ```
23     pub PANIC_PARAMS,
24     style,
25     "missing parameters in `panic!` calls"
26 }
27
28 declare_clippy_lint! {
29     /// **What it does:** Checks for usage of `panic!`.
30     ///
31     /// **Why is this bad?** `panic!` will stop the execution of the executable
32     ///
33     /// **Known problems:** None.
34     ///
35     /// **Example:**
36     /// ```no_run
37     /// panic!("even with a good reason");
38     /// ```
39     pub PANIC,
40     restriction,
41     "usage of the `panic!` macro"
42 }
43
44 declare_clippy_lint! {
45     /// **What it does:** Checks for usage of `unimplemented!`.
46     ///
47     /// **Why is this bad?** This macro should not be present in production code
48     ///
49     /// **Known problems:** None.
50     ///
51     /// **Example:**
52     /// ```no_run
53     /// unimplemented!();
54     /// ```
55     pub UNIMPLEMENTED,
56     restriction,
57     "`unimplemented!` should not be present in production code"
58 }
59
60 declare_clippy_lint! {
61     /// **What it does:** Checks for usage of `todo!`.
62     ///
63     /// **Why is this bad?** This macro should not be present in production code
64     ///
65     /// **Known problems:** None.
66     ///
67     /// **Example:**
68     /// ```no_run
69     /// todo!();
70     /// ```
71     pub TODO,
72     restriction,
73     "`todo!` should not be present in production code"
74 }
75
76 declare_clippy_lint! {
77     /// **What it does:** Checks for usage of `unreachable!`.
78     ///
79     /// **Why is this bad?** This macro can cause code to panic
80     ///
81     /// **Known problems:** None.
82     ///
83     /// **Example:**
84     /// ```no_run
85     /// unreachable!();
86     /// ```
87     pub UNREACHABLE,
88     restriction,
89     "`unreachable!` should not be present in production code"
90 }
91
92 declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
93
94 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
95     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
96         if_chain! {
97             if let ExprKind::Block(ref block, _) = expr.kind;
98             if let Some(ref ex) = block.expr;
99             if let Some(params) = match_function_call(cx, ex, &paths::BEGIN_PANIC);
100             if params.len() == 1;
101             then {
102                 if is_expn_of(expr.span, "unimplemented").is_some() {
103                     let span = get_outer_span(expr);
104                     span_lint(cx, UNIMPLEMENTED, span,
105                               "`unimplemented` should not be present in production code");
106                 } else if is_expn_of(expr.span, "todo").is_some() {
107                     let span = get_outer_span(expr);
108                     span_lint(cx, TODO, span,
109                               "`todo` should not be present in production code");
110                 } else if is_expn_of(expr.span, "unreachable").is_some() {
111                     let span = get_outer_span(expr);
112                     span_lint(cx, UNREACHABLE, span,
113                               "`unreachable` should not be present in production code");
114                 } else if is_expn_of(expr.span, "panic").is_some() {
115                     let span = get_outer_span(expr);
116                     span_lint(cx, PANIC, span,
117                               "`panic` should not be present in production code");
118                     match_panic(params, expr, cx);
119                 }
120             }
121         }
122     }
123 }
124
125 fn get_outer_span(expr: &Expr<'_>) -> Span {
126     if_chain! {
127         if expr.span.from_expansion();
128         let first = expr.span.ctxt().outer_expn_data();
129         if first.call_site.from_expansion();
130         let second = first.call_site.ctxt().outer_expn_data();
131         then {
132             second.call_site
133         } else {
134             expr.span
135         }
136     }
137 }
138
139 fn match_panic(params: &[Expr<'_>], expr: &Expr<'_>, cx: &LateContext<'_, '_>) {
140     if_chain! {
141         if let ExprKind::Lit(ref lit) = params[0].kind;
142         if is_direct_expn_of(expr.span, "panic").is_some();
143         if let LitKind::Str(ref string, _) = lit.node;
144         let string = string.as_str().replace("{{", "").replace("}}", "");
145         if let Some(par) = string.find('{');
146         if string[par..].contains('}');
147         if params[0].span.source_callee().is_none();
148         if params[0].span.lo() != params[0].span.hi();
149         then {
150             span_lint(cx, PANIC_PARAMS, params[0].span,
151                       "you probably are missing some parameter in your format string");
152         }
153     }
154 }