Skip to content

Commit 9d6c4f0

Browse files
committed
merge into/literal suggestion for DRY
1 parent d31a2a0 commit 9d6c4f0

File tree

1 file changed

+52
-67
lines changed

1 file changed

+52
-67
lines changed

src/librustc_typeck/check/demand.rs

+52-67
Original file line numberDiff line numberDiff line change
@@ -415,26 +415,56 @@ 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 { "" });
422-
let suffix_suggestion = format!(
423-
"{}{}{}{}",
418+
let into_suggestion = format!(
419+
"{}{}{}.into()",
424420
if needs_paren { "(" } else { "" },
425-
src.trim_right_matches(&checked_ty.to_string()),
426-
expected_ty,
421+
src,
427422
if needs_paren { ")" } else { "" },
428423
);
429-
430-
let is_suffixed = |expr: &hir::Expr| {
424+
let literal_is_ty_suffixed = |expr: &hir::Expr| {
431425
if let hir::ExprKind::Lit(lit) = &expr.node {
432426
lit.node.is_suffixed()
433427
} else {
434428
false
435429
}
436430
};
437431

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+
438468
match (&expected_ty.sty, &checked_ty.sty) {
439469
(&ty::Int(ref exp), &ty::Int(ref found)) => {
440470
match (found.bit_width(), exp.bit_width()) {
@@ -459,25 +489,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
459489
}
460490
}
461491
_ => {
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+
);
481496
}
482497
}
483498
true
@@ -505,25 +520,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
505520
}
506521
}
507522
_ => {
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+
);
527527
}
528528
}
529529
true
@@ -624,25 +624,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
624624
}
625625
(&ty::Float(ref exp), &ty::Float(ref found)) => {
626626
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+
);
646631
} else if can_cast {
647632
err.span_suggestion_with_applicability(
648633
expr.span,

0 commit comments

Comments
 (0)