1 use crate::utils::{is_direct_expn_of, is_expn_of, match_panic_call, span_lint};
2 use if_chain::if_chain;
3 use rustc_ast::ast::LitKind;
4 use rustc_hir::{Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
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 `panic!`.
31 /// **Why is this bad?** `panic!` will stop the execution of the executable
33 /// **Known problems:** None.
37 /// panic!("even with a good reason");
41 "usage of the `panic!` macro"
44 declare_clippy_lint! {
45 /// **What it does:** Checks for usage of `unimplemented!`.
47 /// **Why is this bad?** This macro should not be present in production code
49 /// **Known problems:** None.
57 "`unimplemented!` should not be present in production code"
60 declare_clippy_lint! {
61 /// **What it does:** Checks for usage of `todo!`.
63 /// **Why is this bad?** This macro should not be present in production code
65 /// **Known problems:** None.
73 "`todo!` should not be present in production code"
76 declare_clippy_lint! {
77 /// **What it does:** Checks for usage of `unreachable!`.
79 /// **Why is this bad?** This macro can cause code to panic
81 /// **Known problems:** None.
89 "`unreachable!` should not be present in production code"
92 declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
94 impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
95 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
96 if let Some(params) = match_panic_call(cx, expr) {
97 let span = get_outer_span(expr);
98 if is_expn_of(expr.span, "unimplemented").is_some() {
103 "`unimplemented` should not be present in production code",
105 } else if is_expn_of(expr.span, "todo").is_some() {
106 span_lint(cx, TODO, span, "`todo` should not be present in production code");
107 } else if is_expn_of(expr.span, "unreachable").is_some() {
112 "`unreachable` should not be present in production code",
114 } else if is_expn_of(expr.span, "panic").is_some() {
115 span_lint(cx, PANIC, span, "`panic` should not be present in production code");
116 match_panic(params, expr, cx);
122 fn get_outer_span(expr: &Expr<'_>) -> Span {
124 if expr.span.from_expansion();
125 let first = expr.span.ctxt().outer_expn_data();
126 if first.call_site.from_expansion();
127 let second = first.call_site.ctxt().outer_expn_data();
136 fn match_panic(params: &[Expr<'_>], expr: &Expr<'_>, cx: &LateContext<'_>) {
138 if let ExprKind::Lit(ref lit) = params[0].kind;
139 if is_direct_expn_of(expr.span, "panic").is_some();
140 if let LitKind::Str(ref string, _) = lit.node;
141 let string = string.as_str().replace("{{", "").replace("}}", "");
142 if let Some(par) = string.find('{');
143 if string[par..].contains('}');
144 if params[0].span.source_callee().is_none();
145 if params[0].span.lo() != params[0].span.hi();
147 span_lint(cx, PANIC_PARAMS, params[0].span,
148 "you probably are missing some parameter in your format string");