Skip to content

Commit

Permalink
Suggest semicolon removal and boxing when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 23, 2020
1 parent c548511 commit f5d7443
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
35 changes: 27 additions & 8 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Expand Up @@ -688,12 +688,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let msg = "`match` arms have incompatible types";
err.span_label(outer_error_span, msg);
if let Some((sp, boxed)) = semi_span {
if matches!(boxed, StatementAsExpression::NeedsBoxing) {
err.span_suggestion_verbose(
if let (StatementAsExpression::NeedsBoxing, [.., prior_arm]) =
(boxed, &prior_arms[..])
{
err.multipart_suggestion(
"consider removing this semicolon and boxing the expressions",
vec![
(prior_arm.shrink_to_lo(), "Box::new(".to_string()),
(prior_arm.shrink_to_hi(), ")".to_string()),
(arm_span.shrink_to_lo(), "Box::new(".to_string()),
(arm_span.shrink_to_hi(), ")".to_string()),
(sp, String::new()),
],
Applicability::HasPlaceholders,
);
} else if matches!(boxed, StatementAsExpression::NeedsBoxing) {
err.span_suggestion_short(
sp,
"consider removing this semicolon and boxing the expression",
"consider removing this semicolon and boxing the expressions",
String::new(),
Applicability::HasPlaceholders,
Applicability::MachineApplicable,
);
} else {
err.span_suggestion_short(
Expand Down Expand Up @@ -727,11 +741,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
if let Some((sp, boxed)) = semicolon {
if matches!(boxed, StatementAsExpression::NeedsBoxing) {
err.span_suggestion_verbose(
sp,
err.multipart_suggestion(
"consider removing this semicolon and boxing the expression",
String::new(),
Applicability::HasPlaceholders,
vec![
(then.shrink_to_lo(), "Box::new(".to_string()),
(then.shrink_to_hi(), ")".to_string()),
(else_sp.shrink_to_lo(), "Box::new(".to_string()),
(else_sp.shrink_to_hi(), ")".to_string()),
(sp, String::new()),
],
Applicability::MachineApplicable,
);
} else {
err.span_suggestion_short(
Expand Down
23 changes: 15 additions & 8 deletions src/test/ui/suggestions/match-prev-arm-needing-semi.stderr
Expand Up @@ -24,10 +24,13 @@ help: consider `await`ing on the `Future`
|
LL | false => async_dummy().await,
| ^^^^^^
help: consider removing this semicolon and boxing the expression
help: consider removing this semicolon and boxing the expressions
|
LL | Box::new(async_dummy())
LL |
LL | }
LL | false => Box::new(async_dummy()),
|
LL | async_dummy()
| --

error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:39:18
Expand Down Expand Up @@ -55,10 +58,13 @@ help: consider `await`ing on the `Future`
|
LL | false => async_dummy2().await,
| ^^^^^^
help: consider removing this semicolon and boxing the expression
help: consider removing this semicolon and boxing the expressions
|
LL | Box::new(async_dummy())
LL |
LL | }
LL | false => Box::new(async_dummy2()),
|
LL | async_dummy()
| --

error[E0308]: `match` arms have incompatible types
--> $DIR/match-prev-arm-needing-semi.rs:50:18
Expand All @@ -70,10 +76,10 @@ LL | let _ = match true {
| _____________-
LL | | true => async_dummy(),
| | ------------- this is found to be of type `impl Future`
LL | |
LL | | false => async_dummy2(),
| | ^^^^^^^^^^^^^^ expected opaque type, found a different opaque type
LL | |
LL | |
... |
LL | |
LL | | };
| |_____- `match` arms have incompatible types
Expand All @@ -84,6 +90,7 @@ LL | | };
help: consider `await`ing on both `Future`s
|
LL | true => async_dummy().await,
LL |
LL | false => async_dummy2().await,
|

Expand Down

0 comments on commit f5d7443

Please sign in to comment.