@@ -415,26 +415,56 @@ 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 { "" } ) ;
422
- let suffix_suggestion = format ! (
423
- "{}{}{}{}" ,
418
+ let into_suggestion = format ! (
419
+ "{}{}{}.into()" ,
424
420
if needs_paren { "(" } else { "" } ,
425
- src. trim_right_matches( & checked_ty. to_string( ) ) ,
426
- expected_ty,
421
+ src,
427
422
if needs_paren { ")" } else { "" } ,
428
423
) ;
429
-
430
- let is_suffixed = |expr : & hir:: Expr | {
424
+ let literal_is_ty_suffixed = |expr : & hir:: Expr | {
431
425
if let hir:: ExprKind :: Lit ( lit) = & expr. node {
432
426
lit. node . is_suffixed ( )
433
427
} else {
434
428
false
435
429
}
436
430
} ;
437
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
+ } ;
467
+
438
468
match ( & expected_ty. sty , & checked_ty. sty ) {
439
469
( & ty:: Int ( ref exp) , & ty:: Int ( ref found) ) => {
440
470
match ( found. bit_width ( ) , exp. bit_width ( ) ) {
@@ -459,25 +489,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
459
489
}
460
490
}
461
491
_ => {
462
- if is_suffixed ( expr) {
463
- err. span_suggestion_with_applicability (
464
- expr. span ,
465
- & format ! (
466
- "change the type of the numeric literal from `{}` to `{}`" ,
467
- checked_ty,
468
- expected_ty,
469
- ) ,
470
- suffix_suggestion,
471
- Applicability :: MaybeIncorrect ,
472
- ) ;
473
- } else {
474
- err. span_suggestion_with_applicability (
475
- expr. span ,
476
- & format ! ( "{}, which {}" , msg, will_sign_extend) ,
477
- into_suggestion,
478
- Applicability :: MachineApplicable
479
- ) ;
480
- }
492
+ suggest_to_change_suffix_or_into (
493
+ err,
494
+ Some ( will_sign_extend) ,
495
+ ) ;
481
496
}
482
497
}
483
498
true
@@ -505,25 +520,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
505
520
}
506
521
}
507
522
_ => {
508
- if is_suffixed ( expr) {
509
- err. span_suggestion_with_applicability (
510
- expr. span ,
511
- & format ! (
512
- "change the type of the numeric literal from `{}` to `{}`" ,
513
- checked_ty,
514
- expected_ty,
515
- ) ,
516
- suffix_suggestion,
517
- Applicability :: MaybeIncorrect ,
518
- ) ;
519
- } else {
520
- err. span_suggestion_with_applicability (
521
- expr. span ,
522
- & format ! ( "{}, which {}" , msg, will_zero_extend) ,
523
- into_suggestion,
524
- Applicability :: MachineApplicable
525
- ) ;
526
- }
523
+ suggest_to_change_suffix_or_into (
524
+ err,
525
+ Some ( will_zero_extend) ,
526
+ ) ;
527
527
}
528
528
}
529
529
true
@@ -624,25 +624,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
624
624
}
625
625
( & ty:: Float ( ref exp) , & ty:: Float ( ref found) ) => {
626
626
if found. bit_width ( ) < exp. bit_width ( ) {
627
- if is_suffixed ( expr) {
628
- err. span_suggestion_with_applicability (
629
- expr. span ,
630
- & format ! (
631
- "change the type of the numeric literal from `{}` to `{}`" ,
632
- checked_ty,
633
- expected_ty,
634
- ) ,
635
- suffix_suggestion,
636
- Applicability :: MaybeIncorrect ,
637
- ) ;
638
- } else {
639
- err. span_suggestion_with_applicability (
640
- expr. span ,
641
- & format ! ( "{} in a lossless way" , msg) ,
642
- into_suggestion,
643
- Applicability :: MachineApplicable
644
- ) ;
645
- }
627
+ suggest_to_change_suffix_or_into (
628
+ err,
629
+ None ,
630
+ ) ;
646
631
} else if can_cast {
647
632
err. span_suggestion_with_applicability (
648
633
expr. span ,
0 commit comments