3 use syntax::ast::LitKind;
4 use utils::{is_direct_expn_of, match_def_path, opt_def_id, paths, resolve_node, 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<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
36 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
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;
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();
52 span_lint(cx, PANIC_PARAMS, params[0].span,
53 "you probably are missing some parameter in your format string");