@@ -415,10 +415,55 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
415
415
src,
416
416
if needs_paren { ")" } else { "" } ,
417
417
expected_ty) ;
418
- let into_suggestion = format ! ( "{}{}{}.into()" ,
419
- if needs_paren { "(" } else { "" } ,
420
- src,
421
- if needs_paren { ")" } else { "" } ) ;
418
+ let into_suggestion = format ! (
419
+ "{}{}{}.into()" ,
420
+ if needs_paren { "(" } else { "" } ,
421
+ src,
422
+ if needs_paren { ")" } else { "" } ,
423
+ ) ;
424
+ let literal_is_ty_suffixed = |expr : & hir:: Expr | {
425
+ if let hir:: ExprKind :: Lit ( lit) = & expr. node {
426
+ lit. node . is_suffixed ( )
427
+ } else {
428
+ false
429
+ }
430
+ } ;
431
+
432
+ let into_sugg = into_suggestion. clone ( ) ;
433
+ let suggest_to_change_suffix_or_into = |err : & mut DiagnosticBuilder ,
434
+ note : Option < & str > | {
435
+ let suggest_msg = if literal_is_ty_suffixed ( expr) {
436
+ format ! (
437
+ "change the type of the numeric literal from `{}` to `{}`" ,
438
+ checked_ty,
439
+ expected_ty,
440
+ )
441
+ } else {
442
+ match note {
443
+ Some ( note) => format ! ( "{}, which {}" , msg, note) ,
444
+ _ => format ! ( "{} in a lossless way" , msg) ,
445
+ }
446
+ } ;
447
+
448
+ let suffix_suggestion = format ! (
449
+ "{}{}{}{}" ,
450
+ if needs_paren { "(" } else { "" } ,
451
+ src. trim_right_matches( & checked_ty. to_string( ) ) ,
452
+ expected_ty,
453
+ if needs_paren { ")" } else { "" } ,
454
+ ) ;
455
+
456
+ err. span_suggestion_with_applicability (
457
+ expr. span ,
458
+ & suggest_msg,
459
+ if literal_is_ty_suffixed ( expr) {
460
+ suffix_suggestion
461
+ } else {
462
+ into_sugg
463
+ } ,
464
+ Applicability :: MachineApplicable ,
465
+ ) ;
466
+ } ;
422
467
423
468
match ( & expected_ty. sty , & checked_ty. sty ) {
424
469
( & ty:: Int ( ref exp) , & ty:: Int ( ref found) ) => {
@@ -444,11 +489,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
444
489
}
445
490
}
446
491
_ => {
447
- err. span_suggestion_with_applicability (
448
- expr. span ,
449
- & format ! ( "{}, which {}" , msg, will_sign_extend) ,
450
- into_suggestion,
451
- Applicability :: MachineApplicable
492
+ suggest_to_change_suffix_or_into (
493
+ err,
494
+ Some ( will_sign_extend) ,
452
495
) ;
453
496
}
454
497
}
@@ -477,12 +520,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
477
520
}
478
521
}
479
522
_ => {
480
- err. span_suggestion_with_applicability (
481
- expr. span ,
482
- & format ! ( "{}, which {}" , msg, will_zero_extend) ,
483
- into_suggestion,
484
- Applicability :: MachineApplicable
485
- ) ;
523
+ suggest_to_change_suffix_or_into (
524
+ err,
525
+ Some ( will_zero_extend) ,
526
+ ) ;
486
527
}
487
528
}
488
529
true
@@ -583,12 +624,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
583
624
}
584
625
( & ty:: Float ( ref exp) , & ty:: Float ( ref found) ) => {
585
626
if found. bit_width ( ) < exp. bit_width ( ) {
586
- err. span_suggestion_with_applicability (
587
- expr. span ,
588
- & format ! ( "{} in a lossless way" , msg) ,
589
- into_suggestion,
590
- Applicability :: MachineApplicable
591
- ) ;
627
+ suggest_to_change_suffix_or_into (
628
+ err,
629
+ None ,
630
+ ) ;
592
631
} else if can_cast {
593
632
err. span_suggestion_with_applicability (
594
633
expr. span ,
0 commit comments