3 use syntax::ast::LitKind;
5 use syntax::ext::quote::rt::Span;
6 use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, opt_def_id, paths, resolve_node, span_lint};
8 /// **What it does:** Checks for missing parameters in `panic!`.
10 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
11 /// two forms of `panic!`: if there are no parameters given, the first argument
12 /// is not a format string and used literally. So while `format!("{}")` will
13 /// fail to compile, `panic!("{}")` will not.
15 /// **Known problems:** None.
19 /// panic!("This `panic!` is probably missing a parameter there: {}");
21 declare_clippy_lint! {
24 "missing parameters in `panic!` calls"
27 /// **What it does:** Checks for usage of `unimplemented!`.
29 /// **Why is this bad?** This macro should not be present in production code
31 /// **Known problems:** None.
37 declare_clippy_lint! {
40 "`unimplemented!` should not be present in production code"
43 #[allow(missing_copy_implementations)]
46 impl LintPass for Pass {
47 fn get_lints(&self) -> LintArray {
48 lint_array!(PANIC_PARAMS, UNIMPLEMENTED)
52 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
53 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
55 if let ExprBlock(ref block, _) = expr.node;
56 if let Some(ref ex) = block.expr;
57 if let ExprCall(ref fun, ref params) = ex.node;
58 if let ExprPath(ref qpath) = fun.node;
59 if let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id));
60 if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
63 if is_expn_of(expr.span, "unimplemented").is_some() {
64 let span = get_outer_span(expr);
65 span_lint(cx, UNIMPLEMENTED, span,
66 "`unimplemented` should not be present in production code");
68 match_panic(params, expr, cx);
75 fn get_outer_span(expr: &Expr) -> Span {
77 if let Some(first) = expr.span.ctxt().outer().expn_info();
78 if let Some(second) = first.call_site.ctxt().outer().expn_info();
87 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext) {
89 if let ExprLit(ref lit) = params[0].node;
90 if is_direct_expn_of(expr.span, "panic").is_some();
91 if let LitKind::Str(ref string, _) = lit.node;
92 let string = string.as_str().replace("{{", "").replace("}}", "");
93 if let Some(par) = string.find('{');
94 if string[par..].contains('}');
95 if params[0].span.source_callee().is_none();
96 if params[0].span.lo() != params[0].span.hi();
98 span_lint(cx, PANIC_PARAMS, params[0].span,
99 "you probably are missing some parameter in your format string");