@@ -31,8 +31,10 @@ pub(super) fn check<'tcx>(
31
31
}
32
32
}
33
33
34
+ let cast_str = snippet_opt ( cx, cast_expr. span ) . unwrap_or_default ( ) ;
35
+
34
36
if let Some ( lit) = get_numeric_literal ( cast_expr) {
35
- let literal_str = snippet_opt ( cx , cast_expr . span ) . unwrap_or_default ( ) ;
37
+ let literal_str = & cast_str ;
36
38
37
39
if_chain ! {
38
40
if let LitKind :: Int ( n, _) = lit. node;
@@ -50,39 +52,44 @@ pub(super) fn check<'tcx>(
50
52
51
53
match lit. node {
52
54
LitKind :: Int ( _, LitIntType :: Unsuffixed ) if cast_to. is_integral ( ) => {
53
- lint_unnecessary_cast ( cx, expr, & literal_str, cast_from, cast_to) ;
55
+ lint_unnecessary_cast ( cx, expr, literal_str, cast_from, cast_to) ;
56
+ return false ;
54
57
} ,
55
58
LitKind :: Float ( _, LitFloatType :: Unsuffixed ) if cast_to. is_floating_point ( ) => {
56
- lint_unnecessary_cast ( cx, expr, & literal_str, cast_from, cast_to) ;
59
+ lint_unnecessary_cast ( cx, expr, literal_str, cast_from, cast_to) ;
60
+ return false ;
61
+ } ,
62
+ LitKind :: Int ( _, LitIntType :: Unsuffixed ) | LitKind :: Float ( _, LitFloatType :: Unsuffixed ) => {
63
+ return false ;
57
64
} ,
58
- LitKind :: Int ( _, LitIntType :: Unsuffixed ) | LitKind :: Float ( _, LitFloatType :: Unsuffixed ) => { } ,
59
65
LitKind :: Int ( _, LitIntType :: Signed ( _) | LitIntType :: Unsigned ( _) )
60
66
| LitKind :: Float ( _, LitFloatType :: Suffixed ( _) )
61
67
if cast_from. kind ( ) == cast_to. kind ( ) =>
62
68
{
63
69
if let Some ( src) = snippet_opt ( cx, cast_expr. span ) {
64
70
if let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node ) {
65
71
lint_unnecessary_cast ( cx, expr, num_lit. integer , cast_from, cast_to) ;
72
+ return true ;
66
73
}
67
74
}
68
75
} ,
69
- _ => {
70
- if cast_from. kind ( ) == cast_to. kind ( ) && !in_external_macro ( cx. sess ( ) , expr. span ) {
71
- span_lint_and_sugg (
72
- cx,
73
- UNNECESSARY_CAST ,
74
- expr. span ,
75
- & format ! ( "casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)" ) ,
76
- "try" ,
77
- literal_str,
78
- Applicability :: MachineApplicable ,
79
- ) ;
80
- return true ;
81
- }
82
- } ,
76
+ _ => { } ,
83
77
}
84
78
}
85
79
80
+ if cast_from. kind ( ) == cast_to. kind ( ) && !in_external_macro ( cx. sess ( ) , expr. span ) {
81
+ span_lint_and_sugg (
82
+ cx,
83
+ UNNECESSARY_CAST ,
84
+ expr. span ,
85
+ & format ! ( "casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)" ) ,
86
+ "try" ,
87
+ cast_str,
88
+ Applicability :: MachineApplicable ,
89
+ ) ;
90
+ return true ;
91
+ }
92
+
86
93
false
87
94
}
88
95
0 commit comments