diff options
| author | varkor <github@varkor.com> | 2018-10-18 00:05:19 +0100 |
|---|---|---|
| committer | varkor <github@varkor.com> | 2018-10-18 00:05:19 +0100 |
| commit | 26346467c52e34c730f3e924be7d635d69b4f737 (patch) | |
| tree | d51e0914f1498ce93f6d387214c0654db5964b60 | |
| parent | 0982be77107f9af614adb609ee6eb1c5730d8eab (diff) | |
| download | rust-26346467c52e34c730f3e924be7d635d69b4f737.tar.gz rust-26346467c52e34c730f3e924be7d635d69b4f737.zip | |
Warning about unreachable arms after matching on a diverging type
| -rw-r--r-- | src/librustc_typeck/check/_match.rs | 6 | ||||
| -rw-r--r-- | src/test/ui/unreachable/unwarned-match-on-never.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/unreachable/unwarned-match-on-never.stderr | 14 |
3 files changed, 24 insertions, 6 deletions
diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index b4c969447de..eaaf9f77430 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -614,6 +614,12 @@ https://doc.rust-lang.org/reference/types.html#trait-objects"); return tcx.types.never; } + if self.diverges.get().always() { + for arm in arms { + self.warn_if_unreachable(arm.body.id, arm.body.span, "arm"); + } + } + // Otherwise, we have to union together the types that the // arms produce and so forth. let discrim_diverges = self.diverges.get(); diff --git a/src/test/ui/unreachable/unwarned-match-on-never.rs b/src/test/ui/unreachable/unwarned-match-on-never.rs index 0c160615c8b..71f8fe3a783 100644 --- a/src/test/ui/unreachable/unwarned-match-on-never.rs +++ b/src/test/ui/unreachable/unwarned-match-on-never.rs @@ -7,12 +7,18 @@ fn foo(x: !) -> bool { // Explicit matches on the never type are unwarned. match x {} // But matches in unreachable code are warned. - match x {} //~ ERROR: unreachable expression + match x {} //~ ERROR unreachable expression +} + +fn bar() { + match (return) { + () => () //~ ERROR unreachable arm + } } fn main() { return; - match () { //~ ERROR: unreachable expression + match () { //~ ERROR unreachable expression () => (), } } diff --git a/src/test/ui/unreachable/unwarned-match-on-never.stderr b/src/test/ui/unreachable/unwarned-match-on-never.stderr index 969c24a07e8..8807e5f04e5 100644 --- a/src/test/ui/unreachable/unwarned-match-on-never.stderr +++ b/src/test/ui/unreachable/unwarned-match-on-never.stderr @@ -1,7 +1,7 @@ error: unreachable expression --> $DIR/unwarned-match-on-never.rs:10:5 | -LL | match x {} //~ ERROR: unreachable expression +LL | match x {} //~ ERROR unreachable expression | ^^^^^^^^^^ | note: lint level defined here @@ -10,13 +10,19 @@ note: lint level defined here LL | #![deny(unreachable_code)] | ^^^^^^^^^^^^^^^^ +error: unreachable arm + --> $DIR/unwarned-match-on-never.rs:15:15 + | +LL | () => () //~ ERROR unreachable arm + | ^^ + error: unreachable expression - --> $DIR/unwarned-match-on-never.rs:15:5 + --> $DIR/unwarned-match-on-never.rs:21:5 | -LL | / match () { //~ ERROR: unreachable expression +LL | / match () { //~ ERROR unreachable expression LL | | () => (), LL | | } | |_____^ -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors |
