1 use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, paths, span_lint};
2 use if_chain::if_chain;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
6 use syntax::ast::LitKind;
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: {}");
25 "missing parameters in `panic!` calls"
28 declare_clippy_lint! {
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.
41 "`unimplemented!` should not be present in production code"
44 declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED]);
46 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
47 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
49 if let ExprKind::Block(ref block, _) = expr.kind;
50 if let Some(ref ex) = block.expr;
51 if let Some(params) = match_function_call(cx, ex, &paths::BEGIN_PANIC);
54 if is_expn_of(expr.span, "unimplemented").is_some() {
55 let span = get_outer_span(expr);
56 span_lint(cx, UNIMPLEMENTED, span,
57 "`unimplemented` should not be present in production code");
59 match_panic(params, expr, cx);
66 fn get_outer_span(expr: &Expr) -> Span {
68 if expr.span.from_expansion();
69 let first = expr.span.ctxt().outer_expn_data();
70 if first.call_site.from_expansion();
71 let second = first.call_site.ctxt().outer_expn_data();
80 fn match_panic(params: &[Expr], expr: &Expr, cx: &LateContext<'_, '_>) {
82 if let ExprKind::Lit(ref lit) = params[0].kind;
83 if is_direct_expn_of(expr.span, "panic").is_some();
84 if let LitKind::Str(ref string, _) = lit.node;
85 let string = string.as_str().replace("{{", "").replace("}}", "");
86 if let Some(par) = string.find('{');
87 if string[par..].contains('}');
88 if params[0].span.source_callee().is_none();
89 if params[0].span.lo() != params[0].span.hi();
91 span_lint(cx, PANIC_PARAMS, params[0].span,
92 "you probably are missing some parameter in your format string");