Skip to content

Commit 4d3cec3

Browse files
committed
Suggest ; after bare match expression E0308
Fix #72634.
1 parent e5d46a5 commit 4d3cec3

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
748748
arm_ty,
749749
arm_span,
750750
);
751+
if let Some(hir::Node::Expr(m)) = self.tcx.hir().find_parent(scrut_hir_id)
752+
&& let Some(hir::Node::Stmt(stmt)) = self.tcx.hir().find_parent(m.hir_id)
753+
&& let hir::StmtKind::Expr(_) = stmt.kind
754+
{
755+
err.span_suggestion_verbose(
756+
stmt.span.shrink_to_hi(),
757+
"consider using a semicolon here, but this will discard any values \
758+
in the match arms",
759+
";",
760+
Applicability::MaybeIncorrect,
761+
);
762+
}
751763
if let Some(ret_sp) = opt_suggest_box_span {
752764
// Get return type span and point to it.
753765
self.suggest_boxing_for_return_impl_trait(

src/test/ui/suggestions/issue-81839.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ error[E0308]: `match` arms have incompatible types
44
LL | / match num {
55
LL | | 1 => {
66
LL | | cx.answer_str("hi");
7-
| | --------------------
8-
| | | |
9-
| | | help: consider removing this semicolon
10-
| | this is found to be of type `()`
7+
| | -------------------- this is found to be of type `()`
118
LL | | }
129
LL | | _ => cx.answer_str("hi"),
1310
| | ^^^^^^^^^^^^^^^^^^^ expected `()`, found opaque type
@@ -21,6 +18,15 @@ LL | pub async fn answer_str(&self, _s: &str) -> Test {
2118
| ^^^^ checked the `Output` of this `async fn`, found opaque type
2219
= note: expected unit type `()`
2320
found opaque type `impl Future<Output = Test>`
21+
help: consider removing this semicolon
22+
|
23+
LL - cx.answer_str("hi");
24+
LL + cx.answer_str("hi")
25+
|
26+
help: consider using a semicolon here, but this will discard any values in the match arms
27+
|
28+
LL | };
29+
| +
2430

2531
error: aborting due to previous error
2632

src/test/ui/wf/wf-unsafe-trait-obj-match.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ LL | | }
1111
|
1212
= note: expected reference `&S`
1313
found reference `&R`
14+
help: consider using a semicolon here, but this will discard any values in the match arms
15+
|
16+
LL | };
17+
| +
1418

1519
error[E0038]: the trait `Trait` cannot be made into an object
1620
--> $DIR/wf-unsafe-trait-obj-match.rs:26:21

0 commit comments

Comments
 (0)