]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/panic_unimplemented.rs
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / tools / clippy / clippy_lints / src / panic_unimplemented.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::macros::{is_panic, root_macro_call_first_node};
3 use rustc_hir::Expr;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::declare_lint_pass;
6
7 declare_clippy_lint! {
8 /// ### What it does
9 /// Checks for usage of `panic!`.
10 ///
11 /// ### Why is this bad?
12 /// `panic!` will stop the execution of the executable.
13 ///
14 /// ### Example
15 /// ```no_run
16 /// panic!("even with a good reason");
17 /// ```
18 #[clippy::version = "1.40.0"]
19 pub PANIC,
20 restriction,
21 "usage of the `panic!` macro"
22 }
23
24 declare_clippy_lint! {
25 /// ### What it does
26 /// Checks for usage of `unimplemented!`.
27 ///
28 /// ### Why is this bad?
29 /// This macro should not be present in production code.
30 ///
31 /// ### Example
32 /// ```no_run
33 /// unimplemented!();
34 /// ```
35 #[clippy::version = "pre 1.29.0"]
36 pub UNIMPLEMENTED,
37 restriction,
38 "`unimplemented!` should not be present in production code"
39 }
40
41 declare_clippy_lint! {
42 /// ### What it does
43 /// Checks for usage of `todo!`.
44 ///
45 /// ### Why is this bad?
46 /// The `todo!` macro is often used for unfinished code, and it causes
47 /// code to panic. It should not be present in production code.
48 ///
49 /// ### Example
50 /// ```no_run
51 /// todo!();
52 /// ```
53 /// Finish the implementation, or consider marking it as explicitly unimplemented.
54 /// ```no_run
55 /// unimplemented!();
56 /// ```
57 #[clippy::version = "1.40.0"]
58 pub TODO,
59 restriction,
60 "`todo!` should not be present in production code"
61 }
62
63 declare_clippy_lint! {
64 /// ### What it does
65 /// Checks for usage of `unreachable!`.
66 ///
67 /// ### Why is this bad?
68 /// This macro can cause code to panic.
69 ///
70 /// ### Example
71 /// ```no_run
72 /// unreachable!();
73 /// ```
74 #[clippy::version = "1.40.0"]
75 pub UNREACHABLE,
76 restriction,
77 "usage of the `unreachable!` macro"
78 }
79
80 declare_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
81
82 impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
83 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
84 let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
85 return;
86 };
87 if is_panic(cx, macro_call.def_id) {
88 if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
89 return;
90 }
91
92 span_lint(
93 cx,
94 PANIC,
95 macro_call.span,
96 "`panic` should not be present in production code",
97 );
98 return;
99 }
100 match cx.tcx.item_name(macro_call.def_id).as_str() {
101 "todo" => {
102 span_lint(
103 cx,
104 TODO,
105 macro_call.span,
106 "`todo` should not be present in production code",
107 );
108 },
109 "unimplemented" => {
110 span_lint(
111 cx,
112 UNIMPLEMENTED,
113 macro_call.span,
114 "`unimplemented` should not be present in production code",
115 );
116 },
117 "unreachable" => {
118 span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
119 },
120 _ => {},
121 }
122 }
123 }