@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_with_applicability;
33use rustc_errors:: Applicability ;
44use rustc_hir:: Expr ;
55use rustc_lint:: LateContext ;
6- use rustc_middle:: ty:: { self , Ty } ;
6+ use rustc_middle:: ty:: Ty ;
77
88use super :: { FN_TO_NUMERIC_CAST_WITH_TRUNCATION , utils} ;
99
@@ -12,23 +12,20 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
1212 let Some ( to_nbits) = utils:: int_ty_to_nbits ( cx. tcx , cast_to) else {
1313 return ;
1414 } ;
15- match cast_from. kind ( ) {
16- ty:: FnDef ( ..) | ty:: FnPtr ( ..) => {
17- let mut applicability = Applicability :: MaybeIncorrect ;
18- let from_snippet = snippet_with_applicability ( cx, cast_expr. span , "x" , & mut applicability) ;
15+ if cast_from. is_fn ( ) {
16+ let mut applicability = Applicability :: MaybeIncorrect ;
17+ let from_snippet = snippet_with_applicability ( cx, cast_expr. span , "x" , & mut applicability) ;
1918
20- if to_nbits < cx. tcx . data_layout . pointer_size ( ) . bits ( ) {
21- span_lint_and_sugg (
22- cx,
23- FN_TO_NUMERIC_CAST_WITH_TRUNCATION ,
24- expr. span ,
25- format ! ( "casting function pointer `{from_snippet}` to `{cast_to}`, which truncates the value" ) ,
26- "try" ,
27- format ! ( "{from_snippet} as usize" ) ,
28- applicability,
29- ) ;
30- }
31- } ,
32- _ => { } ,
19+ if to_nbits < cx. tcx . data_layout . pointer_size ( ) . bits ( ) {
20+ span_lint_and_sugg (
21+ cx,
22+ FN_TO_NUMERIC_CAST_WITH_TRUNCATION ,
23+ expr. span ,
24+ format ! ( "casting function pointer `{from_snippet}` to `{cast_to}`, which truncates the value" ) ,
25+ "try" ,
26+ format ! ( "{from_snippet} as usize" ) ,
27+ applicability,
28+ ) ;
29+ }
3330 }
3431}
0 commit comments