1
1
use clippy_config:: Conf ;
2
2
use clippy_utils:: diagnostics:: span_lint;
3
- use clippy_utils:: is_in_test;
4
3
use clippy_utils:: macros:: { is_panic, root_macro_call_first_node} ;
5
- use rustc_hir:: Expr ;
4
+ use clippy_utils:: { is_in_test, match_def_path, paths} ;
5
+ use rustc_hir:: def:: { DefKind , Res } ;
6
+ use rustc_hir:: { Expr , ExprKind , QPath } ;
6
7
use rustc_lint:: { LateContext , LateLintPass } ;
7
8
use rustc_session:: impl_lint_pass;
8
9
@@ -95,10 +96,49 @@ impl_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC])
95
96
96
97
impl < ' tcx > LateLintPass < ' tcx > for PanicUnimplemented {
97
98
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
98
- let Some ( macro_call) = root_macro_call_first_node ( cx, expr) else {
99
- return ;
100
- } ;
101
- if is_panic ( cx, macro_call. def_id ) {
99
+ if let Some ( macro_call) = root_macro_call_first_node ( cx, expr) {
100
+ if is_panic ( cx, macro_call. def_id ) {
101
+ if cx. tcx . hir ( ) . is_inside_const_context ( expr. hir_id )
102
+ || self . allow_panic_in_tests && is_in_test ( cx. tcx , expr. hir_id )
103
+ {
104
+ return ;
105
+ }
106
+
107
+ span_lint (
108
+ cx,
109
+ PANIC ,
110
+ macro_call. span ,
111
+ "`panic` should not be present in production code" ,
112
+ ) ;
113
+ return ;
114
+ }
115
+ match cx. tcx . item_name ( macro_call. def_id ) . as_str ( ) {
116
+ "todo" => {
117
+ span_lint (
118
+ cx,
119
+ TODO ,
120
+ macro_call. span ,
121
+ "`todo` should not be present in production code" ,
122
+ ) ;
123
+ } ,
124
+ "unimplemented" => {
125
+ span_lint (
126
+ cx,
127
+ UNIMPLEMENTED ,
128
+ macro_call. span ,
129
+ "`unimplemented` should not be present in production code" ,
130
+ ) ;
131
+ } ,
132
+ "unreachable" => {
133
+ span_lint ( cx, UNREACHABLE , macro_call. span , "usage of the `unreachable!` macro" ) ;
134
+ } ,
135
+ _ => { } ,
136
+ }
137
+ } else if let ExprKind :: Call ( func, [ _] ) = expr. kind
138
+ && let ExprKind :: Path ( QPath :: Resolved ( None , expr_path) ) = func. kind
139
+ && let Res :: Def ( DefKind :: Fn , def_id) = expr_path. res
140
+ && match_def_path ( cx, def_id, & paths:: PANIC_ANY )
141
+ {
102
142
if cx. tcx . hir ( ) . is_inside_const_context ( expr. hir_id )
103
143
|| self . allow_panic_in_tests && is_in_test ( cx. tcx , expr. hir_id )
104
144
{
@@ -108,32 +148,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
108
148
span_lint (
109
149
cx,
110
150
PANIC ,
111
- macro_call . span ,
112
- "`panic ` should not be present in production code" ,
151
+ expr . span ,
152
+ "`panic_any ` should not be present in production code" ,
113
153
) ;
114
154
return ;
115
155
}
116
- match cx. tcx . item_name ( macro_call. def_id ) . as_str ( ) {
117
- "todo" => {
118
- span_lint (
119
- cx,
120
- TODO ,
121
- macro_call. span ,
122
- "`todo` should not be present in production code" ,
123
- ) ;
124
- } ,
125
- "unimplemented" => {
126
- span_lint (
127
- cx,
128
- UNIMPLEMENTED ,
129
- macro_call. span ,
130
- "`unimplemented` should not be present in production code" ,
131
- ) ;
132
- } ,
133
- "unreachable" => {
134
- span_lint ( cx, UNREACHABLE , macro_call. span , "usage of the `unreachable!` macro" ) ;
135
- } ,
136
- _ => { } ,
137
- }
138
156
}
139
157
}
0 commit comments