]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/panic.rs
Merge pull request #950 from oli-obk/split3
[rust.git] / clippy_lints / src / panic.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use syntax::ast::LitKind;
4 use utils::{is_direct_expn_of, match_path, paths, span_lint};
5
6 /// **What it does:** This lint checks for missing parameters in `panic!`.
7 ///
8 /// **Known problems:** Should you want to use curly brackets in `panic!` without any parameter,
9 /// this lint will warn.
10 ///
11 /// **Example:**
12 /// ```
13 /// panic!("This `panic!` is probably missing a parameter there: {}");
14 /// ```
15 declare_lint! {
16     pub PANIC_PARAMS, Warn, "missing parameters in `panic!`"
17 }
18
19 #[allow(missing_copy_implementations)]
20 pub struct PanicPass;
21
22 impl LintPass for PanicPass {
23     fn get_lints(&self) -> LintArray {
24         lint_array!(PANIC_PARAMS)
25     }
26 }
27
28 impl LateLintPass for PanicPass {
29     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
30         if_let_chain! {[
31             let ExprBlock(ref block) = expr.node,
32             let Some(ref ex) = block.expr,
33             let ExprCall(ref fun, ref params) = ex.node,
34             params.len() == 2,
35             let ExprPath(None, ref path) = fun.node,
36             match_path(path, &paths::BEGIN_PANIC),
37             let ExprLit(ref lit) = params[0].node,
38             is_direct_expn_of(cx, params[0].span, "panic").is_some(),
39             let LitKind::Str(ref string, _) = lit.node,
40             let Some(par) = string.find('{'),
41             string[par..].contains('}')
42         ], {
43             span_lint(cx, PANIC_PARAMS, params[0].span,
44                       "you probably are missing some parameter in your format string");
45         }}
46     }
47 }