@@ -19,6 +19,7 @@ use rustc_middle::ty::{
1919 TypeVisitable ,
2020} ;
2121use rustc_session:: errors:: ExprParenthesesNeeded ;
22+ use rustc_span:: source_map:: Spanned ;
2223use rustc_span:: symbol:: { sym, Ident } ;
2324use rustc_span:: { Span , Symbol } ;
2425use rustc_trait_selection:: infer:: InferCtxtExt ;
@@ -1259,6 +1260,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12591260 ) ;
12601261 true
12611262 }
1263+ ExprKind :: Lit ( Spanned {
1264+ node : rustc_ast:: LitKind :: Int ( lit, rustc_ast:: LitIntType :: Unsuffixed ) ,
1265+ span,
1266+ } ) => {
1267+ let Ok ( snippet) = self . tcx . sess . source_map ( ) . span_to_snippet ( span) else { return false ; } ;
1268+ if !( snippet. starts_with ( "0x" ) || snippet. starts_with ( "0X" ) ) {
1269+ return false ;
1270+ }
1271+ if snippet. len ( ) <= 5 || !snippet. is_char_boundary ( snippet. len ( ) - 3 ) {
1272+ return false ;
1273+ }
1274+ let ( _, suffix) = snippet. split_at ( snippet. len ( ) - 3 ) ;
1275+ let value = match suffix {
1276+ "f32" => ( lit - 0xf32 ) / ( 16 * 16 * 16 ) ,
1277+ "f64" => ( lit - 0xf64 ) / ( 16 * 16 * 16 ) ,
1278+ _ => return false ,
1279+ } ;
1280+ err. span_suggestions (
1281+ expr. span ,
1282+ "rewrite this as a decimal floating point literal, or use `as` to turn a hex literal into a float" ,
1283+ [ format ! ( "0x{value:X} as {suffix}" ) , format ! ( "{value}_{suffix}" ) ] ,
1284+ Applicability :: MaybeIncorrect ,
1285+ ) ;
1286+ true
1287+ }
12621288 _ => false ,
12631289 }
12641290 }
0 commit comments