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<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
36 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx 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(ref qpath) = fun.node,
43 match_def_path(cx.tcx, resolve_node(cx, qpath, fun.hir_id).def_id(), &paths::BEGIN_PANIC),
44 let ExprLit(ref lit) = params[0].node,
45 is_direct_expn_of(expr.span, "panic").is_some(),
46 let LitKind::Str(ref string, _) = lit.node,
47 let Some(par) = string.as_str().find('{'),
48 string.as_str()[par..].contains('}'),
49 params[0].span.source_callee().is_none()
51 span_lint(cx, PANIC_PARAMS, params[0].span,
52 "you probably are missing some parameter in your format string");