11use clippy_config:: Conf ;
22use clippy_utils:: diagnostics:: span_lint;
3- use clippy_utils:: is_in_test;
43use 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 } ;
67use rustc_lint:: { LateContext , LateLintPass } ;
78use rustc_session:: impl_lint_pass;
89
@@ -95,10 +96,49 @@ impl_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC])
9596
9697impl < ' tcx > LateLintPass < ' tcx > for PanicUnimplemented {
9798 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+ {
102142 if cx. tcx . hir ( ) . is_inside_const_context ( expr. hir_id )
103143 || self . allow_panic_in_tests && is_in_test ( cx. tcx , expr. hir_id )
104144 {
@@ -108,32 +148,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
108148 span_lint (
109149 cx,
110150 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" ,
113153 ) ;
114154 return ;
115155 }
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- }
138156 }
139157}
0 commit comments