2 use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, paths, resolve_node, span_lint};
3 use if_chain::if_chain;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
7 use syntax::ast::LitKind;
11 declare_clippy_lint! {
12 /// **What it does:** Checks for missing parameters in `panic!`.
14 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
15 /// two forms of `panic!`: if there are no parameters given, the first argument
16 /// is not a format string and used literally. So while `format!("{}")` will
17 /// fail to compile, `panic!("{}")` will not.
19 /// **Known problems:** None.
23 /// panic!("This `panic!` is probably missing a parameter there: {}");
27 "missing parameters in `panic!` calls"
30 declare_clippy_lint! {
31 /// **What it does:** Checks for usage of `unimplemented!`.
33 /// **Why is this bad?** This macro should not be present in production code
35 /// **Known problems:** None.
43 "`unimplemented!` should not be present in production code"
46 declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED]);
48 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
49 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
51 if let ExprKind::Block(ref block, _) = expr.node;
52 if let Some(ref ex) = block.expr;
53 if let ExprKind::Call(ref fun, ref params) = ex.node;
54 if let ExprKind::Path(ref qpath) = fun.node;
55 if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
56 if match_def_path(cx, fun_def_id, &*paths::BEGIN_PANIC);
59 if is_expn_of(expr.span, *sym::unimplemented).is_some() {
60 let span = get_outer_span(expr);
61 span_lint(cx, UNIMPLEMENTED, span,
62 "`unimplemented` should not be present in production code");
64 match_panic(params, expr, cx);
71 fn get_outer_span(expr: &Expr) -> Span {
73 if let Some(first) = expr.span.ctxt().outer().expn_info();
74 if let Some(second) = first.call_site.ctxt().outer().expn_info();
83 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext<'_, '_>) {
85 if let ExprKind::Lit(ref lit) = params[0].node;
86 if is_direct_expn_of(expr.span, *sym::panic).is_some();
87 if let LitKind::Str(ref string, _) = lit.node;
88 let string = string.as_str().replace("{{", "").replace("}}", "");
89 if let Some(par) = string.find('{');
90 if string[par..].contains('}');
91 if params[0].span.source_callee().is_none();
92 if params[0].span.lo() != params[0].span.hi();
94 span_lint(cx, PANIC_PARAMS, params[0].span,
95 "you probably are missing some parameter in your format string");