]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/panic_unimplemented.rs
Auto merge of #81156 - DrMeepster:read_buf, r=joshtriplett
[rust.git] / src / tools / clippy / clippy_lints / src / panic_unimplemented.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::{is_expn_of, match_panic_call};
3 use if_chain::if_chain;
4 use rustc_hir::Expr;
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::Span;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks for usage of `panic!`.
12     ///
13     /// ### Why is this bad?
14     /// `panic!` will stop the execution of the executable
15     ///
16     /// ### Example
17     /// ```no_run
18     /// panic!("even with a good reason");
19     /// ```
20     #[clippy::version = "1.40.0"]
21     pub PANIC,
22     restriction,
23     "usage of the `panic!` macro"
24 }
25
26 declare_clippy_lint! {
27     /// ### What it does
28     /// Checks for usage of `unimplemented!`.
29     ///
30     /// ### Why is this bad?
31     /// This macro should not be present in production code
32     ///
33     /// ### Example
34     /// ```no_run
35     /// unimplemented!();
36     /// ```
37     #[clippy::version = "pre 1.29.0"]
38     pub UNIMPLEMENTED,
39     restriction,
40     "`unimplemented!` should not be present in production code"
41 }
42
43 declare_clippy_lint! {
44     /// ### What it does
45     /// Checks for usage of `todo!`.
46     ///
47     /// ### Why is this bad?
48     /// This macro should not be present in production code
49     ///
50     /// ### Example
51     /// ```no_run
52     /// todo!();
53     /// ```
54     #[clippy::version = "1.40.0"]
55     pub TODO,
56     restriction,
57     "`todo!` should not be present in production code"
58 }
59
60 declare_clippy_lint! {
61     /// ### What it does
62     /// Checks for usage of `unreachable!`.
63     ///
64     /// ### Why is this bad?
65     /// This macro can cause code to panic
66     ///
67     /// ### Example
68     /// ```no_run
69     /// unreachable!();
70     /// ```
71     #[clippy::version = "1.40.0"]
72     pub UNREACHABLE,
73     restriction,
74     "usage of the `unreachable!` macro"
75 }
76
77 declare_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
78
79 impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
80     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
81         if match_panic_call(cx, expr).is_some()
82             && (is_expn_of(expr.span, "debug_assert").is_none() && is_expn_of(expr.span, "assert").is_none())
83         {
84             let span = get_outer_span(expr);
85             if is_expn_of(expr.span, "unimplemented").is_some() {
86                 span_lint(
87                     cx,
88                     UNIMPLEMENTED,
89                     span,
90                     "`unimplemented` should not be present in production code",
91                 );
92             } else if is_expn_of(expr.span, "todo").is_some() {
93                 span_lint(cx, TODO, span, "`todo` should not be present in production code");
94             } else if is_expn_of(expr.span, "unreachable").is_some() {
95                 span_lint(cx, UNREACHABLE, span, "usage of the `unreachable!` macro");
96             } else if is_expn_of(expr.span, "panic").is_some() {
97                 span_lint(cx, PANIC, span, "`panic` should not be present in production code");
98             }
99         }
100     }
101 }
102
103 fn get_outer_span(expr: &Expr<'_>) -> Span {
104     if_chain! {
105         if expr.span.from_expansion();
106         let first = expr.span.ctxt().outer_expn_data().call_site;
107         if first.from_expansion();
108         then {
109             first.ctxt().outer_expn_data().call_site
110         } else {
111             expr.span
112         }
113     }
114 }