3 use syntax::ast::LitKind;
4 use utils::{is_direct_expn_of, match_def_path, resolve_node, paths, span_lint};
6 /// **What it does:** Checks for missing parameters in `panic!`.
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.
13 /// **Known problems:** Should you want to use curly brackets in `panic!`
14 /// without any parameter, this lint will warn.
18 /// panic!("This `panic!` is probably missing a parameter there: {}");
23 "missing parameters in `panic!` calls"
26 #[allow(missing_copy_implementations)]
29 impl LintPass for Pass {
30 fn get_lints(&self) -> LintArray {
31 lint_array!(PANIC_PARAMS)
35 impl LateLintPass for Pass {
36 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
38 let ExprBlock(ref block) = expr.node,
39 let Some(ref ex) = block.expr,
40 let ExprCall(ref fun, ref params) = ex.node,
42 let ExprPath(None, _) = fun.node,
43 let Some(fun) = resolve_node(cx, fun.id),
44 match_def_path(cx, fun.def_id(), &paths::BEGIN_PANIC),
45 let ExprLit(ref lit) = params[0].node,
46 is_direct_expn_of(cx, params[0].span, "panic").is_some(),
47 let LitKind::Str(ref string, _) = lit.node,
48 let Some(par) = string.find('{'),
49 string[par..].contains('}')
51 span_lint(cx, PANIC_PARAMS, params[0].span,
52 "you probably are missing some parameter in your format string");