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;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 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"
47 impl LintPass for Pass {
48 fn get_lints(&self) -> LintArray {
49 lint_array!(PANIC_PARAMS, UNIMPLEMENTED)
52 fn name(&self) -> &'static str {
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
58 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
60 if let ExprKind::Block(ref block, _) = expr.node;
61 if let Some(ref ex) = block.expr;
62 if let ExprKind::Call(ref fun, ref params) = ex.node;
63 if let ExprKind::Path(ref qpath) = fun.node;
64 if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
65 if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
68 if is_expn_of(expr.span, "unimplemented").is_some() {
69 let span = get_outer_span(expr);
70 span_lint(cx, UNIMPLEMENTED, span,
71 "`unimplemented` should not be present in production code");
73 match_panic(params, expr, cx);
80 fn get_outer_span(expr: &Expr) -> Span {
82 if let Some(first) = expr.span.ctxt().outer().expn_info();
83 if let Some(second) = first.call_site.ctxt().outer().expn_info();
92 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext<'_, '_>) {
94 if let ExprKind::Lit(ref lit) = params[0].node;
95 if is_direct_expn_of(expr.span, "panic").is_some();
96 if let LitKind::Str(ref string, _) = lit.node;
97 let string = string.as_str().replace("{{", "").replace("}}", "");
98 if let Some(par) = string.find('{');
99 if string[par..].contains('}');
100 if params[0].span.source_callee().is_none();
101 if params[0].span.lo() != params[0].span.hi();
103 span_lint(cx, PANIC_PARAMS, params[0].span,
104 "you probably are missing some parameter in your format string");