Skip to content

Commit 2634646

Browse files
committed
Warning about unreachable arms after matching on a diverging type
1 parent 0982be7 commit 2634646

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/librustc_typeck/check/_match.rs

+6
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,12 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
614614
return tcx.types.never;
615615
}
616616

617+
if self.diverges.get().always() {
618+
for arm in arms {
619+
self.warn_if_unreachable(arm.body.id, arm.body.span, "arm");
620+
}
621+
}
622+
617623
// Otherwise, we have to union together the types that the
618624
// arms produce and so forth.
619625
let discrim_diverges = self.diverges.get();

src/test/ui/unreachable/unwarned-match-on-never.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ fn foo(x: !) -> bool {
77
// Explicit matches on the never type are unwarned.
88
match x {}
99
// But matches in unreachable code are warned.
10-
match x {} //~ ERROR: unreachable expression
10+
match x {} //~ ERROR unreachable expression
11+
}
12+
13+
fn bar() {
14+
match (return) {
15+
() => () //~ ERROR unreachable arm
16+
}
1117
}
1218

1319
fn main() {
1420
return;
15-
match () { //~ ERROR: unreachable expression
21+
match () { //~ ERROR unreachable expression
1622
() => (),
1723
}
1824
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: unreachable expression
22
--> $DIR/unwarned-match-on-never.rs:10:5
33
|
4-
LL | match x {} //~ ERROR: unreachable expression
4+
LL | match x {} //~ ERROR unreachable expression
55
| ^^^^^^^^^^
66
|
77
note: lint level defined here
@@ -10,13 +10,19 @@ note: lint level defined here
1010
LL | #![deny(unreachable_code)]
1111
| ^^^^^^^^^^^^^^^^
1212

13+
error: unreachable arm
14+
--> $DIR/unwarned-match-on-never.rs:15:15
15+
|
16+
LL | () => () //~ ERROR unreachable arm
17+
| ^^
18+
1319
error: unreachable expression
14-
--> $DIR/unwarned-match-on-never.rs:15:5
20+
--> $DIR/unwarned-match-on-never.rs:21:5
1521
|
16-
LL | / match () { //~ ERROR: unreachable expression
22+
LL | / match () { //~ ERROR unreachable expression
1723
LL | | () => (),
1824
LL | | }
1925
| |_____^
2026

21-
error: aborting due to 2 previous errors
27+
error: aborting due to 3 previous errors
2228

0 commit comments

Comments
 (0)