@@ -31,8 +31,10 @@ pub(super) fn check<'tcx>(
3131 }
3232 }
3333
34+ let cast_str = snippet_opt ( cx, cast_expr. span ) . unwrap_or_default ( ) ;
35+
3436 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 ;
3638
3739 if_chain ! {
3840 if let LitKind :: Int ( n, _) = lit. node;
@@ -50,39 +52,44 @@ pub(super) fn check<'tcx>(
5052
5153 match lit. node {
5254 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 ;
5457 } ,
5558 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 ;
5764 } ,
58- LitKind :: Int ( _, LitIntType :: Unsuffixed ) | LitKind :: Float ( _, LitFloatType :: Unsuffixed ) => { } ,
5965 LitKind :: Int ( _, LitIntType :: Signed ( _) | LitIntType :: Unsigned ( _) )
6066 | LitKind :: Float ( _, LitFloatType :: Suffixed ( _) )
6167 if cast_from. kind ( ) == cast_to. kind ( ) =>
6268 {
6369 if let Some ( src) = snippet_opt ( cx, cast_expr. span ) {
6470 if let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node ) {
6571 lint_unnecessary_cast ( cx, expr, num_lit. integer , cast_from, cast_to) ;
72+ return true ;
6673 }
6774 }
6875 } ,
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+ _ => { } ,
8377 }
8478 }
8579
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+
8693 false
8794}
8895
0 commit comments