Skip to content

Commit 2c3bb42

Browse files
Only omit trailing comma if block doesn't come from macro expansion
1 parent 2b646bd commit 2c3bb42

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -829,16 +829,27 @@ fn non_exhaustive_match<'p, 'tcx>(
829829
} else {
830830
" ".to_string()
831831
};
832-
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
832+
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..))
833+
&& only.span.ctxt() == only.body.span.ctxt()
834+
{
835+
""
836+
} else {
837+
","
838+
};
833839
suggestion = Some((
834840
only.span.shrink_to_hi(),
835841
format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
836842
));
837843
}
838844
[.., prev, last] if prev.span.ctxt() == last.span.ctxt() => {
839845
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
840-
let comma =
841-
if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
846+
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
847+
&& last.span.ctxt() == last.body.span.ctxt()
848+
{
849+
""
850+
} else {
851+
","
852+
};
842853
suggestion = Some((
843854
last.span.shrink_to_hi(),
844855
format!(

src/test/ui/issue-94866.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! m {
2+
() => {
3+
{}
4+
};
5+
}
6+
7+
enum Enum { A, B }
8+
9+
fn main() {
10+
match Enum::A {
11+
//~^ ERROR non-exhaustive patterns
12+
Enum::A => m!()
13+
}
14+
}

src/test/ui/issue-94866.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0004]: non-exhaustive patterns: `B` not covered
2+
--> $DIR/issue-94866.rs:10:11
3+
|
4+
LL | match Enum::A {
5+
| ^^^^^^^ pattern `B` not covered
6+
|
7+
note: `Enum` defined here
8+
--> $DIR/issue-94866.rs:7:16
9+
|
10+
LL | enum Enum { A, B }
11+
| ---- ^ not covered
12+
= note: the matched value is of type `Enum`
13+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
14+
|
15+
LL ~ Enum::A => m!(),
16+
LL + B => todo!()
17+
|
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0004`.

src/test/ui/pattern/usefulness/tuple-struct-nonexhaustive.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | struct Foo(isize, isize);
1212
= note: the matched value is of type `Foo`
1313
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1414
|
15-
LL ~ Foo(2, b) => println!("{}", b)
15+
LL ~ Foo(2, b) => println!("{}", b),
1616
LL + Foo(_, _) => todo!()
1717
|
1818

0 commit comments

Comments
 (0)