Skip to content

Allow explicit matches on ! without warning #55119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,6 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
self.check_expr_has_type_or_error(discrim, discrim_ty);
};

// If the discriminant diverges, the match is pointless (e.g.,
// `match (return) { }`).
self.warn_if_unreachable(expr.id, expr.span, "expression");

// If there are no arms, that is a diverging match; a special case.
if arms.is_empty() {
self.diverges.set(self.diverges.get() | Diverges::Always);
Expand All @@ -620,7 +616,6 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");

// Otherwise, we have to union together the types that the
// arms produce and so forth.

let discrim_diverges = self.diverges.get();
self.diverges.set(Diverges::Maybe);

Expand Down

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions src/test/ui/reachable/expr_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
#![allow(dead_code)]
#![deny(unreachable_code)]

fn a() {
// The match is considered unreachable here, because the `return`
// diverges:
match {return} { } //~ ERROR unreachable
}

fn b() {
match () { () => return }
println!("I am dead");
Expand Down
19 changes: 6 additions & 13 deletions src/test/ui/reachable/expr_match.stderr
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
error: unreachable expression
--> $DIR/expr_match.rs:19:5
error: unreachable statement
--> $DIR/expr_match.rs:18:5
|
LL | match {return} { } //~ ERROR unreachable
| ^^^^^^^^^^^^^^^^^^
LL | println!("I am dead");
| ^^^^^^^^^^^^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/expr_match.rs:14:9
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^

error: unreachable statement
--> $DIR/expr_match.rs:24:5
|
LL | println!("I am dead");
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: unreachable statement
--> $DIR/expr_match.rs:35:5
--> $DIR/expr_match.rs:29:5
|
LL | println!("I am dead");
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

18 changes: 18 additions & 0 deletions src/test/ui/unreachable/unwarned-match-on-never.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![deny(unreachable_code)]
#![allow(dead_code)]

#![feature(never_type)]

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
}

fn main() {
return;
match () { //~ ERROR: unreachable expression
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to test

fn main() {
  match (return) {
    () => ()
  }
}

too (this should warn, I think, on the arm?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, this won't warn at all. I'll try to get the arms warning.

() => (),
}
}
22 changes: 22 additions & 0 deletions src/test/ui/unreachable/unwarned-match-on-never.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: unreachable expression
--> $DIR/unwarned-match-on-never.rs:10:5
|
LL | match x {} //~ ERROR: unreachable expression
| ^^^^^^^^^^
|
note: lint level defined here
--> $DIR/unwarned-match-on-never.rs:1:9
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^

error: unreachable expression
--> $DIR/unwarned-match-on-never.rs:15:5
|
LL | / match () { //~ ERROR: unreachable expression
LL | | () => (),
LL | | }
| |_____^

error: aborting due to 2 previous errors