Skip to content

Commit f5d7443

Browse files
committed
Suggest semicolon removal and boxing when appropriate
1 parent c548511 commit f5d7443

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -688,12 +688,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
688688
let msg = "`match` arms have incompatible types";
689689
err.span_label(outer_error_span, msg);
690690
if let Some((sp, boxed)) = semi_span {
691-
if matches!(boxed, StatementAsExpression::NeedsBoxing) {
692-
err.span_suggestion_verbose(
691+
if let (StatementAsExpression::NeedsBoxing, [.., prior_arm]) =
692+
(boxed, &prior_arms[..])
693+
{
694+
err.multipart_suggestion(
695+
"consider removing this semicolon and boxing the expressions",
696+
vec![
697+
(prior_arm.shrink_to_lo(), "Box::new(".to_string()),
698+
(prior_arm.shrink_to_hi(), ")".to_string()),
699+
(arm_span.shrink_to_lo(), "Box::new(".to_string()),
700+
(arm_span.shrink_to_hi(), ")".to_string()),
701+
(sp, String::new()),
702+
],
703+
Applicability::HasPlaceholders,
704+
);
705+
} else if matches!(boxed, StatementAsExpression::NeedsBoxing) {
706+
err.span_suggestion_short(
693707
sp,
694-
"consider removing this semicolon and boxing the expression",
708+
"consider removing this semicolon and boxing the expressions",
695709
String::new(),
696-
Applicability::HasPlaceholders,
710+
Applicability::MachineApplicable,
697711
);
698712
} else {
699713
err.span_suggestion_short(
@@ -727,11 +741,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
727741
}
728742
if let Some((sp, boxed)) = semicolon {
729743
if matches!(boxed, StatementAsExpression::NeedsBoxing) {
730-
err.span_suggestion_verbose(
731-
sp,
744+
err.multipart_suggestion(
732745
"consider removing this semicolon and boxing the expression",
733-
String::new(),
734-
Applicability::HasPlaceholders,
746+
vec![
747+
(then.shrink_to_lo(), "Box::new(".to_string()),
748+
(then.shrink_to_hi(), ")".to_string()),
749+
(else_sp.shrink_to_lo(), "Box::new(".to_string()),
750+
(else_sp.shrink_to_hi(), ")".to_string()),
751+
(sp, String::new()),
752+
],
753+
Applicability::MachineApplicable,
735754
);
736755
} else {
737756
err.span_suggestion_short(

src/test/ui/suggestions/match-prev-arm-needing-semi.stderr

+15-8
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ help: consider `await`ing on the `Future`
2424
|
2525
LL | false => async_dummy().await,
2626
| ^^^^^^
27-
help: consider removing this semicolon and boxing the expression
27+
help: consider removing this semicolon and boxing the expressions
28+
|
29+
LL | Box::new(async_dummy())
30+
LL |
31+
LL | }
32+
LL | false => Box::new(async_dummy()),
2833
|
29-
LL | async_dummy()
30-
| --
3134

3235
error[E0308]: `match` arms have incompatible types
3336
--> $DIR/match-prev-arm-needing-semi.rs:39:18
@@ -55,10 +58,13 @@ help: consider `await`ing on the `Future`
5558
|
5659
LL | false => async_dummy2().await,
5760
| ^^^^^^
58-
help: consider removing this semicolon and boxing the expression
61+
help: consider removing this semicolon and boxing the expressions
62+
|
63+
LL | Box::new(async_dummy())
64+
LL |
65+
LL | }
66+
LL | false => Box::new(async_dummy2()),
5967
|
60-
LL | async_dummy()
61-
| --
6268

6369
error[E0308]: `match` arms have incompatible types
6470
--> $DIR/match-prev-arm-needing-semi.rs:50:18
@@ -70,10 +76,10 @@ LL | let _ = match true {
7076
| _____________-
7177
LL | | true => async_dummy(),
7278
| | ------------- this is found to be of type `impl Future`
79+
LL | |
7380
LL | | false => async_dummy2(),
7481
| | ^^^^^^^^^^^^^^ expected opaque type, found a different opaque type
75-
LL | |
76-
LL | |
82+
... |
7783
LL | |
7884
LL | | };
7985
| |_____- `match` arms have incompatible types
@@ -84,6 +90,7 @@ LL | | };
8490
help: consider `await`ing on both `Future`s
8591
|
8692
LL | true => async_dummy().await,
93+
LL |
8794
LL | false => async_dummy2().await,
8895
|
8996

0 commit comments

Comments
 (0)