@@ -27,6 +27,7 @@ use rustc_target::spec::abi::Abi;
2727use rustc_typeck:: hir_ty_to_ty;
2828
2929use crate :: consts:: { constant, Constant } ;
30+ use crate :: literal_representation:: { NumericLiteral , Radix } ;
3031use crate :: utils:: paths;
3132use crate :: utils:: {
3233 clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, last_path_segment, match_def_path,
@@ -1210,21 +1211,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
12101211 let ( cast_from, cast_to) = ( cx. tables . expr_ty ( ex) , cx. tables . expr_ty ( expr) ) ;
12111212 lint_fn_to_numeric_cast ( cx, expr, ex, cast_from, cast_to) ;
12121213 if let ExprKind :: Lit ( ref lit) = ex. kind {
1213- if let LitKind :: Int ( n, _) = lit. node {
1214- if cast_to. is_floating_point ( ) {
1214+ if_chain ! {
1215+ if let LitKind :: Int ( n, _) = lit. node;
1216+ if let Some ( src) = snippet_opt( cx, lit. span) ;
1217+ if cast_to. is_floating_point( ) ;
1218+ then {
12151219 let from_nbits = 128 - n. leading_zeros( ) ;
12161220 let to_nbits = fp_ty_mantissa_nbits( cast_to) ;
1217- if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits {
1218- span_lint_and_sugg (
1219- cx,
1220- UNNECESSARY_CAST ,
1221- expr. span ,
1222- & format ! ( "casting integer literal to `{}` is unnecessary" , cast_to) ,
1223- "try" ,
1224- format ! ( "{}_{}" , n, cast_to) ,
1225- Applicability :: MachineApplicable ,
1226- ) ;
1227- return ;
1221+ if let Some ( num_lit) = NumericLiteral :: from_lit_kind( & src, & lit. node) {
1222+ if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits &&
1223+ num_lit. radix != Radix :: Hexadecimal {
1224+ span_lint_and_sugg(
1225+ cx,
1226+ UNNECESSARY_CAST ,
1227+ expr. span,
1228+ & format!( "casting integer literal to `{}` is unnecessary" , cast_to) ,
1229+ "try" ,
1230+ format!( "{}_{}" , n, cast_to) ,
1231+ Applicability :: MachineApplicable ,
1232+ ) ;
1233+ return ;
1234+ }
12281235 }
12291236 }
12301237 }
0 commit comments