@@ -27,6 +27,7 @@ use rustc_target::spec::abi::Abi;
27
27
use rustc_typeck:: hir_ty_to_ty;
28
28
29
29
use crate :: consts:: { constant, Constant } ;
30
+ use crate :: literal_representation:: { NumericLiteral , Radix } ;
30
31
use crate :: utils:: paths;
31
32
use crate :: utils:: {
32
33
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 {
1210
1211
let ( cast_from, cast_to) = ( cx. tables . expr_ty ( ex) , cx. tables . expr_ty ( expr) ) ;
1211
1212
lint_fn_to_numeric_cast ( cx, expr, ex, cast_from, cast_to) ;
1212
1213
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 {
1215
1219
let from_nbits = 128 - n. leading_zeros( ) ;
1216
1220
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
+ }
1228
1235
}
1229
1236
}
1230
1237
}
0 commit comments