]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/panic.rs
Fix lines that exceed max width manually
[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_def_path, opt_def_id, paths, resolve_node, span_lint};
5
6 /// **What it does:** Checks for missing parameters in `panic!`.
7 ///
8 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
9 /// two forms of `panic!`: if there are no parameters given, the first argument
10 /// is not a format string and used literally. So while `format!("{}")` will
11 /// fail to compile, `panic!("{}")` will not.
12 ///
13 /// **Known problems:** Should you want to use curly brackets in `panic!`
14 /// without any parameter, this lint will warn.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// panic!("This `panic!` is probably missing a parameter there: {}");
19 /// ```
20 declare_lint! {
21     pub PANIC_PARAMS,
22     Warn,
23     "missing parameters in `panic!` calls"
24 }
25
26 #[allow(missing_copy_implementations)]
27 pub struct Pass;
28
29 impl LintPass for Pass {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(PANIC_PARAMS)
32     }
33 }
34
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
36     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
37         if_chain! {
38             if let ExprBlock(ref block) = expr.node;
39             if let Some(ref ex) = block.expr;
40             if let ExprCall(ref fun, ref params) = ex.node;
41             if params.len() == 2;
42             if let ExprPath(ref qpath) = fun.node;
43             if let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id));
44             if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
45             if let ExprLit(ref lit) = params[0].node;
46             if is_direct_expn_of(expr.span, "panic").is_some();
47             if let LitKind::Str(ref string, _) = lit.node;
48             if let Some(par) = string.as_str().find('{');
49             if string.as_str()[par..].contains('}');
50             if params[0].span.source_callee().is_none();
51             then {
52                 span_lint(cx, PANIC_PARAMS, params[0].span,
53                           "you probably are missing some parameter in your format string");
54             }
55         }
56     }
57 }