1 use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, paths, span_lint};
2 use if_chain::if_chain;
3 use rustc::declare_lint_pass;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_session::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 `panic!`.
32 /// **Why is this bad?** `panic!` will stop the execution of the executable
34 /// **Known problems:** None.
38 /// panic!("even with a good reason");
42 "usage of the `panic!` macro"
45 declare_clippy_lint! {
46 /// **What it does:** Checks for usage of `unimplemented!`.
48 /// **Why is this bad?** This macro should not be present in production code
50 /// **Known problems:** None.
58 "`unimplemented!` should not be present in production code"
61 declare_clippy_lint! {
62 /// **What it does:** Checks for usage of `todo!`.
64 /// **Why is this bad?** This macro should not be present in production code
66 /// **Known problems:** None.
74 "`todo!` should not be present in production code"
77 declare_clippy_lint! {
78 /// **What it does:** Checks for usage of `unreachable!`.
80 /// **Why is this bad?** This macro can cause code to panic
82 /// **Known problems:** None.
90 "`unreachable!` should not be present in production code"
93 declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
95 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
96 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
98 if let ExprKind::Block(ref block, _) = expr.kind;
99 if let Some(ref ex) = block.expr;
100 if let Some(params) = match_function_call(cx, ex, &paths::BEGIN_PANIC);
101 if params.len() == 2;
103 if is_expn_of(expr.span, "unimplemented").is_some() {
104 let span = get_outer_span(expr);
105 span_lint(cx, UNIMPLEMENTED, span,
106 "`unimplemented` should not be present in production code");
107 } else if is_expn_of(expr.span, "todo").is_some() {
108 let span = get_outer_span(expr);
109 span_lint(cx, TODO, span,
110 "`todo` should not be present in production code");
111 } else if is_expn_of(expr.span, "unreachable").is_some() {
112 let span = get_outer_span(expr);
113 span_lint(cx, UNREACHABLE, span,
114 "`unreachable` should not be present in production code");
115 } else if is_expn_of(expr.span, "panic").is_some() {
116 let span = get_outer_span(expr);
117 span_lint(cx, PANIC, span,
118 "`panic` should not be present in production code");
119 match_panic(params, expr, cx);
126 fn get_outer_span(expr: &Expr) -> Span {
128 if expr.span.from_expansion();
129 let first = expr.span.ctxt().outer_expn_data();
130 if first.call_site.from_expansion();
131 let second = first.call_site.ctxt().outer_expn_data();
140 fn match_panic(params: &[Expr], expr: &Expr, cx: &LateContext<'_, '_>) {
142 if let ExprKind::Lit(ref lit) = params[0].kind;
143 if is_direct_expn_of(expr.span, "panic").is_some();
144 if let LitKind::Str(ref string, _) = lit.node;
145 let string = string.as_str().replace("{{", "").replace("}}", "");
146 if let Some(par) = string.find('{');
147 if string[par..].contains('}');
148 if params[0].span.source_callee().is_none();
149 if params[0].span.lo() != params[0].span.hi();
151 span_lint(cx, PANIC_PARAMS, params[0].span,
152 "you probably are missing some parameter in your format string");