Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Dec 2, 2019
1 parent e6aa962 commit 00ccadf
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 9 deletions.
46 changes: 37 additions & 9 deletions src/test/ui/or-patterns/exhaustiveness-pass.rs
Expand Up @@ -6,35 +6,63 @@
// We wrap patterns in a tuple because top-level or-patterns are special-cased for now.
fn main() {
// Get the fatal error out of the way
match (0u8,) {
match (0,) {
(0 | _,) => {}
//~^ ERROR or-patterns are not fully implemented yet
}

match (0u8,) {
match (0,) {
(1 | 2,) => {}
_ => {}
}

match (0u8,) {
(1 | 1,) => {} // FIXME(or_patterns): redundancy not detected for now.
_ => {}
}
match (0u8, 0u8) {
match (0, 0) {
(1 | 2, 3 | 4) => {}
(1, 2) => {}
(2, 1) => {}
(3, 1) => {}
_ => {}
}
match (Some(0u8),) {
(None | Some(0 | 1),) => {}
(Some(2..=255),) => {}
}
match ((0u8,),) {
match ((0,),) {
((0 | 1,) | (2 | 3,),) => {},
((_,),) => {},
}
match (&[0u8][..],) {
([] | [0 | 1..=255] | [_, ..],) => {},
}

match ((0, 0),) {
((0, 0) | (0, 1),) => {}
_ => {}
}
match ((0, 0),) {
((0, 0) | (1, 0),) => {}
_ => {}
}

// FIXME(or_patterns): Redundancies not detected for now.
match (0,) {
(1 | 1,) => {}
_ => {}
}
match [0; 2] {
[0 | 0, 0 | 0] => {}
_ => {}
}
match &[][..] {
[0] => {}
[0, _] => {}
[0, _, _] => {}
[1, ..] => {}
[1 | 2, ..] => {}
_ => {}
}
match Some(0) {
Some(0) => {}
Some(0 | 1) => {}
_ => {}
}
}
58 changes: 58 additions & 0 deletions src/test/ui/pattern/usefulness/top-level-alternation.rs
@@ -0,0 +1,58 @@
#![deny(unreachable_patterns)]

fn main() {
while let 0..=2 | 1 = 0 {} //~ ERROR unreachable pattern
if let 0..=2 | 1 = 0 {} //~ WARN irrefutable if-let pattern
// this one ^ is incorrect

match 0u8 {
0
| 0 => {} //~ ERROR unreachable pattern
_ => {}
}
match Some(0u8) {
Some(0)
| Some(0) => {} //~ ERROR unreachable pattern
_ => {}
}
match (0u8, 0u8) {
(0, _) | (_, 0) => {}
(0, 0) => {} //~ ERROR unreachable pattern
(1, 1) => {}
_ => {}
}
match (0u8, 0u8) {
(0, 1) | (2, 3) => {}
(0, 3) => {}
(2, 1) => {}
_ => {}
}
match (0u8, 0u8) {
(_, 0) | (_, 1) => {}
_ => {}
}
match (0u8, 0u8) {
(0, _) | (1, _) => {}
_ => {}
}
match Some(0u8) {
None | Some(_) => {}
_ => {} //~ ERROR unreachable pattern
}
match Some(0u8) {
None | Some(_) => {}
Some(_) => {} //~ ERROR unreachable pattern
None => {} //~ ERROR unreachable pattern
}
match Some(0u8) {
Some(_) => {}
None => {}
None //~ ERROR unreachable pattern
| Some(_) => {} //~ ERROR unreachable pattern
}
match 0u8 {
1 | 2 => {},
1..=2 => {}, //~ ERROR unreachable pattern
_ => {},
}
}
76 changes: 76 additions & 0 deletions src/test/ui/pattern/usefulness/top-level-alternation.stderr
@@ -0,0 +1,76 @@
error: unreachable pattern
--> $DIR/top-level-alternation.rs:4:23
|
LL | while let 0..=2 | 1 = 0 {}
| ^
|
note: lint level defined here
--> $DIR/top-level-alternation.rs:1:9
|
LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

warning: irrefutable if-let pattern
--> $DIR/top-level-alternation.rs:5:20
|
LL | if let 0..=2 | 1 = 0 {}
| ^
|
= note: `#[warn(irrefutable_let_patterns)]` on by default

error: unreachable pattern
--> $DIR/top-level-alternation.rs:10:15
|
LL | | 0 => {}
| ^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:15:15
|
LL | | Some(0) => {}
| ^^^^^^^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:20:9
|
LL | (0, 0) => {}
| ^^^^^^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:40:9
|
LL | _ => {}
| ^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:44:9
|
LL | Some(_) => {}
| ^^^^^^^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:45:9
|
LL | None => {}
| ^^^^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:50:9
|
LL | None
| ^^^^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:51:15
|
LL | | Some(_) => {}
| ^^^^^^^

error: unreachable pattern
--> $DIR/top-level-alternation.rs:55:9
|
LL | 1..=2 => {},
| ^^^^^

error: aborting due to 10 previous errors

0 comments on commit 00ccadf

Please sign in to comment.