-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathpanic_unimplemented.rs
More file actions
156 lines (146 loc) · 4.69 KB
/
panic_unimplemented.rs
File metadata and controls
156 lines (146 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
use clippy_utils::{is_in_test, is_inside_always_const_context, sym};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
pub struct PanicUnimplemented {
allow_panic_in_tests: bool,
}
impl PanicUnimplemented {
pub fn new(conf: &'static Conf) -> Self {
Self {
allow_panic_in_tests: conf.allow_panic_in_tests,
}
}
}
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `panic!`.
///
/// ### Why restrict this?
/// This macro, or panics in general, may be unwanted in production code.
///
/// ### Example
/// ```no_run
/// panic!("even with a good reason");
/// ```
#[clippy::version = "1.40.0"]
pub PANIC,
restriction,
"usage of the `panic!` macro"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `todo!`.
///
/// ### Why restrict this?
/// The `todo!` macro indicates the presence of unfinished code,
/// so it should not be present in production code.
///
/// ### Example
/// ```no_run
/// todo!();
/// ```
/// Finish the implementation, or consider marking it as explicitly unimplemented.
/// ```no_run
/// unimplemented!();
/// ```
#[clippy::version = "1.40.0"]
pub TODO,
restriction,
"`todo!` should not be present in production code"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `unimplemented!`.
///
/// ### Why restrict this?
/// This macro, or panics in general, may be unwanted in production code.
///
/// ### Example
/// ```no_run
/// unimplemented!();
/// ```
#[clippy::version = "pre 1.29.0"]
pub UNIMPLEMENTED,
restriction,
"`unimplemented!` should not be present in production code"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `unreachable!`.
///
/// ### Why restrict this?
/// This macro, or panics in general, may be unwanted in production code.
///
/// ### Example
/// ```no_run
/// unreachable!();
/// ```
#[clippy::version = "1.40.0"]
pub UNREACHABLE,
restriction,
"usage of the `unreachable!` macro"
}
impl_lint_pass!(PanicUnimplemented => [PANIC, TODO, UNIMPLEMENTED, UNREACHABLE]);
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
if is_panic(cx, macro_call.def_id) {
if is_inside_always_const_context(cx.tcx, expr.hir_id)
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
{
return;
}
span_lint(
cx,
PANIC,
macro_call.span,
"`panic` should not be present in production code",
);
return;
}
match cx.tcx.get_diagnostic_name(macro_call.def_id) {
Some(sym::todo_macro) => {
span_lint(
cx,
TODO,
macro_call.span,
"`todo` should not be present in production code",
);
},
Some(sym::unimplemented_macro) => {
span_lint(
cx,
UNIMPLEMENTED,
macro_call.span,
"`unimplemented` should not be present in production code",
);
},
Some(sym::unreachable_macro) => {
span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
},
_ => {},
}
} else if let ExprKind::Call(func, [_]) = expr.kind
&& let ExprKind::Path(QPath::Resolved(None, expr_path)) = func.kind
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
&& cx.tcx.is_diagnostic_item(sym::panic_any, def_id)
{
if is_inside_always_const_context(cx.tcx, expr.hir_id)
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
{
return;
}
span_lint(
cx,
PANIC,
expr.span,
"`panic_any` should not be present in production code",
);
}
}
}
