3 use syntax::ast::LitKind;
5 use utils::{is_direct_expn_of, is_expn_of, match_def_path, opt_def_id, paths, resolve_node, span_lint};
7 /// **What it does:** Checks for missing parameters in `panic!`.
9 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
10 /// two forms of `panic!`: if there are no parameters given, the first argument
11 /// is not a format string and used literally. So while `format!("{}")` will
12 /// fail to compile, `panic!("{}")` will not.
14 /// **Known problems:** None.
18 /// panic!("This `panic!` is probably missing a parameter there: {}");
20 declare_clippy_lint! {
23 "missing parameters in `panic!` calls"
26 /// **What it does:** Checks for usage of `unimplemented!`.
28 /// **Why is this bad?** This macro should not be present in production code
30 /// **Known problems:** None.
36 declare_clippy_lint! {
39 "`unimplemented!` should not be present in production code"
42 #[allow(missing_copy_implementations)]
45 impl LintPass for Pass {
46 fn get_lints(&self) -> LintArray {
47 lint_array!(PANIC_PARAMS, UNIMPLEMENTED)
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
52 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
54 if let ExprBlock(ref block, _) = expr.node;
55 if let Some(ref ex) = block.expr;
56 if let ExprCall(ref fun, ref params) = ex.node;
57 if let ExprPath(ref qpath) = fun.node;
58 if let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id));
59 if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
62 if is_expn_of(expr.span, "unimplemented").is_some() {
63 span_lint(cx, UNIMPLEMENTED, expr.span,
64 "`unimplemented` should not be present in production code");
66 match_panic(params, expr, cx);
73 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext) {
75 if let ExprLit(ref lit) = params[0].node;
76 if is_direct_expn_of(expr.span, "panic").is_some();
77 if let LitKind::Str(ref string, _) = lit.node;
78 let string = string.as_str().replace("{{", "").replace("}}", "");
79 if let Some(par) = string.find('{');
80 if string[par..].contains('}');
81 if params[0].span.source_callee().is_none();
82 if params[0].span.lo() != params[0].span.hi();
84 span_lint(cx, PANIC_PARAMS, params[0].span,
85 "you probably are missing some parameter in your format string");