1 use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, paths, resolve_node, span_lint};
2 use if_chain::if_chain;
3 use rustc::hir::ptr::P;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
7 use syntax::ast::LitKind;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for missing parameters in `panic!`.
13 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
14 /// two forms of `panic!`: if there are no parameters given, the first argument
15 /// is not a format string and used literally. So while `format!("{}")` will
16 /// fail to compile, `panic!("{}")` will not.
18 /// **Known problems:** None.
22 /// panic!("This `panic!` is probably missing a parameter there: {}");
26 "missing parameters in `panic!` calls"
29 declare_clippy_lint! {
30 /// **What it does:** Checks for usage of `unimplemented!`.
32 /// **Why is this bad?** This macro should not be present in production code
34 /// **Known problems:** None.
42 "`unimplemented!` should not be present in production code"
45 declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED]);
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
48 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
50 if let ExprKind::Block(ref block, _) = expr.node;
51 if let Some(ref ex) = block.expr;
52 if let ExprKind::Call(ref fun, ref params) = ex.node;
53 if let ExprKind::Path(ref qpath) = fun.node;
54 if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
55 if match_def_path(cx, fun_def_id, &paths::BEGIN_PANIC);
58 if is_expn_of(expr.span, "unimplemented").is_some() {
59 let span = get_outer_span(expr);
60 span_lint(cx, UNIMPLEMENTED, span,
61 "`unimplemented` should not be present in production code");
63 match_panic(params, expr, cx);
70 fn get_outer_span(expr: &Expr) -> Span {
72 if expr.span.from_expansion();
73 let first = expr.span.ctxt().outer_expn_data();
74 if first.call_site.from_expansion();
75 let second = first.call_site.ctxt().outer_expn_data();
84 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext<'_, '_>) {
86 if let ExprKind::Lit(ref lit) = params[0].node;
87 if is_direct_expn_of(expr.span, "panic").is_some();
88 if let LitKind::Str(ref string, _) = lit.node;
89 let string = string.as_str().replace("{{", "").replace("}}", "");
90 if let Some(par) = string.find('{');
91 if string[par..].contains('}');
92 if params[0].span.source_callee().is_none();
93 if params[0].span.lo() != params[0].span.hi();
95 span_lint(cx, PANIC_PARAMS, params[0].span,
96 "you probably are missing some parameter in your format string");