3 use rustc::{declare_lint, lint_array};
4 use if_chain::if_chain;
5 use syntax::ast::LitKind;
7 use syntax::ext::quote::rt::Span;
8 use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, opt_def_id, paths, resolve_node, span_lint};
10 /// **What it does:** Checks for missing parameters in `panic!`.
12 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
13 /// two forms of `panic!`: if there are no parameters given, the first argument
14 /// is not a format string and used literally. So while `format!("{}")` will
15 /// fail to compile, `panic!("{}")` will not.
17 /// **Known problems:** None.
21 /// panic!("This `panic!` is probably missing a parameter there: {}");
23 declare_clippy_lint! {
26 "missing parameters in `panic!` calls"
29 /// **What it does:** Checks for usage of `unimplemented!`.
31 /// **Why is this bad?** This macro should not be present in production code
33 /// **Known problems:** None.
39 declare_clippy_lint! {
42 "`unimplemented!` should not be present in production code"
45 #[allow(missing_copy_implementations)]
48 impl LintPass for Pass {
49 fn get_lints(&self) -> LintArray {
50 lint_array!(PANIC_PARAMS, UNIMPLEMENTED)
54 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
55 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
57 if let ExprKind::Block(ref block, _) = expr.node;
58 if let Some(ref ex) = block.expr;
59 if let ExprKind::Call(ref fun, ref params) = ex.node;
60 if let ExprKind::Path(ref qpath) = fun.node;
61 if let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id));
62 if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
65 if is_expn_of(expr.span, "unimplemented").is_some() {
66 let span = get_outer_span(expr);
67 span_lint(cx, UNIMPLEMENTED, span,
68 "`unimplemented` should not be present in production code");
70 match_panic(params, expr, cx);
77 fn get_outer_span(expr: &Expr) -> Span {
79 if let Some(first) = expr.span.ctxt().outer().expn_info();
80 if let Some(second) = first.call_site.ctxt().outer().expn_info();
89 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext) {
91 if let ExprKind::Lit(ref lit) = params[0].node;
92 if is_direct_expn_of(expr.span, "panic").is_some();
93 if let LitKind::Str(ref string, _) = lit.node;
94 let string = string.as_str().replace("{{", "").replace("}}", "");
95 if let Some(par) = string.find('{');
96 if string[par..].contains('}');
97 if params[0].span.source_callee().is_none();
98 if params[0].span.lo() != params[0].span.hi();
100 span_lint(cx, PANIC_PARAMS, params[0].span,
101 "you probably are missing some parameter in your format string");