@@ -415,10 +415,55 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
415415 src,
416416 if needs_paren { ")" } else { "" } ,
417417 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+ } ;
422467
423468 match ( & expected_ty. sty , & checked_ty. sty ) {
424469 ( & ty:: Int ( ref exp) , & ty:: Int ( ref found) ) => {
@@ -444,11 +489,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
444489 }
445490 }
446491 _ => {
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) ,
452495 ) ;
453496 }
454497 }
@@ -477,12 +520,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
477520 }
478521 }
479522 _ => {
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+ ) ;
486527 }
487528 }
488529 true
@@ -583,12 +624,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
583624 }
584625 ( & ty:: Float ( ref exp) , & ty:: Float ( ref found) ) => {
585626 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+ ) ;
592631 } else if can_cast {
593632 err. span_suggestion_with_applicability (
594633 expr. span ,
0 commit comments