diff options
| author | csmoe <35686186+csmoe@users.noreply.github.com> | 2018-09-17 09:24:33 +0800 |
|---|---|---|
| committer | csmoe <35686186+csmoe@users.noreply.github.com> | 2018-09-17 14:26:58 +0800 |
| commit | 9d6c4f09c4b309fd45379996975f4075ba37258b (patch) | |
| tree | 4d674bc5aea714d259c9ae1bce47c5a815877eaf | |
| parent | d31a2a0c603c6a7ec2a69e2b5658bcca016dbc29 (diff) | |
| download | rust-9d6c4f09c4b309fd45379996975f4075ba37258b.tar.gz rust-9d6c4f09c4b309fd45379996975f4075ba37258b.zip | |
merge into/literal suggestion for DRY
| -rw-r--r-- | src/librustc_typeck/check/demand.rs | 119 |
1 files changed, 52 insertions, 67 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index ebed4924949..e78cd4891a5 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -415,19 +415,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { src, if needs_paren { ")" } else { "" }, expected_ty); - let into_suggestion = format!("{}{}{}.into()", - if needs_paren { "(" } else { "" }, - src, - if needs_paren { ")" } else { "" }); - let suffix_suggestion = format!( - "{}{}{}{}", + let into_suggestion = format!( + "{}{}{}.into()", if needs_paren { "(" } else { "" }, - src.trim_right_matches(&checked_ty.to_string()), - expected_ty, + src, if needs_paren { ")" } else { "" }, ); - - let is_suffixed = |expr: &hir::Expr| { + let literal_is_ty_suffixed = |expr: &hir::Expr| { if let hir::ExprKind::Lit(lit) = &expr.node { lit.node.is_suffixed() } else { @@ -435,6 +429,42 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } }; + let into_sugg = into_suggestion.clone(); + let suggest_to_change_suffix_or_into = |err: &mut DiagnosticBuilder, + note: Option<&str>| { + let suggest_msg = if literal_is_ty_suffixed(expr) { + format!( + "change the type of the numeric literal from `{}` to `{}`", + checked_ty, + expected_ty, + ) + } else { + match note { + Some(note) => format!("{}, which {}", msg, note), + _ => format!("{} in a lossless way", msg), + } + }; + + let suffix_suggestion = format!( + "{}{}{}{}", + if needs_paren { "(" } else { "" }, + src.trim_right_matches(&checked_ty.to_string()), + expected_ty, + if needs_paren { ")" } else { "" }, + ); + + err.span_suggestion_with_applicability( + expr.span, + &suggest_msg, + if literal_is_ty_suffixed(expr) { + suffix_suggestion + } else { + into_sugg + }, + Applicability::MachineApplicable, + ); + }; + match (&expected_ty.sty, &checked_ty.sty) { (&ty::Int(ref exp), &ty::Int(ref found)) => { match (found.bit_width(), exp.bit_width()) { @@ -459,25 +489,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } _ => { - if is_suffixed(expr) { - err.span_suggestion_with_applicability( - expr.span, - &format!( - "change the type of the numeric literal from `{}` to `{}`", - checked_ty, - expected_ty, - ), - suffix_suggestion, - Applicability::MaybeIncorrect, - ); - } else { - err.span_suggestion_with_applicability( - expr.span, - &format!("{}, which {}", msg, will_sign_extend), - into_suggestion, - Applicability::MachineApplicable - ); - } + suggest_to_change_suffix_or_into( + err, + Some(will_sign_extend), + ); } } true @@ -505,25 +520,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } _ => { - if is_suffixed(expr) { - err.span_suggestion_with_applicability( - expr.span, - &format!( - "change the type of the numeric literal from `{}` to `{}`", - checked_ty, - expected_ty, - ), - suffix_suggestion, - Applicability::MaybeIncorrect, - ); - } else { - err.span_suggestion_with_applicability( - expr.span, - &format!("{}, which {}", msg, will_zero_extend), - into_suggestion, - Applicability::MachineApplicable - ); - } + suggest_to_change_suffix_or_into( + err, + Some(will_zero_extend), + ); } } true @@ -624,25 +624,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } (&ty::Float(ref exp), &ty::Float(ref found)) => { if found.bit_width() < exp.bit_width() { - if is_suffixed(expr) { - err.span_suggestion_with_applicability( - expr.span, - &format!( - "change the type of the numeric literal from `{}` to `{}`", - checked_ty, - expected_ty, - ), - suffix_suggestion, - Applicability::MaybeIncorrect, - ); - } else { - err.span_suggestion_with_applicability( - expr.span, - &format!("{} in a lossless way", msg), - into_suggestion, - Applicability::MachineApplicable - ); - } + suggest_to_change_suffix_or_into( + err, + None, + ); } else if can_cast { err.span_suggestion_with_applicability( expr.span, |
