Skip to content

Commit c6e3543

Browse files
committed
Add some tests
1 parent f81d6f0 commit c6e3543

10 files changed

+238
-202
lines changed

tests/ui/or-patterns/exhaustiveness-pass.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ fn main() {
3535
((0, 0) | (1, 0),) => {}
3636
_ => {}
3737
}
38+
match ((0, 0),) {
39+
// Note how the second one would be redundant without the guard.
40+
((x, y) | (y, x),) if x == 0 => {}
41+
_ => {}
42+
}
43+
match 0 {
44+
// We don't warn the second one as redundant in general because of cases like the one above.
45+
// We could technically do it if there are no bindings.
46+
0 | 0 if 0 == 0 => {}
47+
_ => {}
48+
}
3849

3950
// This one caused ICE https://github.com/rust-lang/rust/issues/117378
4051
match (0u8, 0) {

tests/ui/or-patterns/exhaustiveness-unreachable-pattern.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![deny(unreachable_patterns)]
22

33
// We wrap patterns in a tuple because top-level or-patterns were special-cased.
4+
#[rustfmt::skip]
45
fn main() {
56
match (0u8,) {
67
(1 | 2,) => {}
@@ -73,6 +74,11 @@ fn main() {
7374
| 0] => {} //~ ERROR unreachable
7475
_ => {}
7576
}
77+
match (true, 0) {
78+
(true, 0 | 0) => {} //~ ERROR unreachable
79+
(_, 0 | 0) => {} //~ ERROR unreachable
80+
_ => {}
81+
}
7682
match &[][..] {
7783
[0] => {}
7884
[0, _] => {}
@@ -149,4 +155,8 @@ fn main() {
149155
| true, //~ ERROR unreachable
150156
false | true) => {}
151157
}
158+
match (true, true) {
159+
(x, y)
160+
| (y, x) => {} //~ ERROR unreachable
161+
}
152162
}
Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unreachable pattern
2-
--> $DIR/exhaustiveness-unreachable-pattern.rs:7:9
2+
--> $DIR/exhaustiveness-unreachable-pattern.rs:8:9
33
|
44
LL | (1,) => {}
55
| ^^^^
@@ -11,128 +11,140 @@ LL | #![deny(unreachable_patterns)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

1313
error: unreachable pattern
14-
--> $DIR/exhaustiveness-unreachable-pattern.rs:12:9
14+
--> $DIR/exhaustiveness-unreachable-pattern.rs:13:9
1515
|
1616
LL | (2,) => {}
1717
| ^^^^
1818

1919
error: unreachable pattern
20-
--> $DIR/exhaustiveness-unreachable-pattern.rs:18:9
20+
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
2121
|
2222
LL | (1 | 2,) => {}
2323
| ^^^^^^^^
2424

2525
error: unreachable pattern
26-
--> $DIR/exhaustiveness-unreachable-pattern.rs:23:9
26+
--> $DIR/exhaustiveness-unreachable-pattern.rs:24:9
2727
|
2828
LL | (1, 3) => {}
2929
| ^^^^^^
3030

3131
error: unreachable pattern
32-
--> $DIR/exhaustiveness-unreachable-pattern.rs:24:9
32+
--> $DIR/exhaustiveness-unreachable-pattern.rs:25:9
3333
|
3434
LL | (1, 4) => {}
3535
| ^^^^^^
3636

3737
error: unreachable pattern
38-
--> $DIR/exhaustiveness-unreachable-pattern.rs:25:9
38+
--> $DIR/exhaustiveness-unreachable-pattern.rs:26:9
3939
|
4040
LL | (2, 4) => {}
4141
| ^^^^^^
4242

4343
error: unreachable pattern
44-
--> $DIR/exhaustiveness-unreachable-pattern.rs:26:9
44+
--> $DIR/exhaustiveness-unreachable-pattern.rs:27:9
4545
|
4646
LL | (2 | 1, 4) => {}
4747
| ^^^^^^^^^^
4848

4949
error: unreachable pattern
50-
--> $DIR/exhaustiveness-unreachable-pattern.rs:28:9
50+
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
5151
|
5252
LL | (1, 4 | 5) => {}
5353
| ^^^^^^^^^^
5454

5555
error: unreachable pattern
56-
--> $DIR/exhaustiveness-unreachable-pattern.rs:36:9
56+
--> $DIR/exhaustiveness-unreachable-pattern.rs:37:9
5757
|
5858
LL | (Some(1),) => {}
5959
| ^^^^^^^^^^
6060

6161
error: unreachable pattern
62-
--> $DIR/exhaustiveness-unreachable-pattern.rs:37:9
62+
--> $DIR/exhaustiveness-unreachable-pattern.rs:38:9
6363
|
6464
LL | (None,) => {}
6565
| ^^^^^^^
6666

6767
error: unreachable pattern
68-
--> $DIR/exhaustiveness-unreachable-pattern.rs:42:9
68+
--> $DIR/exhaustiveness-unreachable-pattern.rs:43:9
6969
|
7070
LL | ((1..=4,),) => {}
7171
| ^^^^^^^^^^^
7272

7373
error: unreachable pattern
74-
--> $DIR/exhaustiveness-unreachable-pattern.rs:47:14
74+
--> $DIR/exhaustiveness-unreachable-pattern.rs:48:14
7575
|
7676
LL | (1 | 1,) => {}
7777
| ^
7878

7979
error: unreachable pattern
80-
--> $DIR/exhaustiveness-unreachable-pattern.rs:51:19
80+
--> $DIR/exhaustiveness-unreachable-pattern.rs:52:19
8181
|
8282
LL | (0 | 1) | 1 => {}
8383
| ^
8484

8585
error: unreachable pattern
86-
--> $DIR/exhaustiveness-unreachable-pattern.rs:57:14
86+
--> $DIR/exhaustiveness-unreachable-pattern.rs:58:14
8787
|
8888
LL | 0 | (0 | 0) => {}
8989
| ^
9090

9191
error: unreachable pattern
92-
--> $DIR/exhaustiveness-unreachable-pattern.rs:57:18
92+
--> $DIR/exhaustiveness-unreachable-pattern.rs:58:18
9393
|
9494
LL | 0 | (0 | 0) => {}
9595
| ^
9696

9797
error: unreachable pattern
98-
--> $DIR/exhaustiveness-unreachable-pattern.rs:65:13
98+
--> $DIR/exhaustiveness-unreachable-pattern.rs:66:13
9999
|
100100
LL | / Some(
101101
LL | | 0 | 0) => {}
102102
| |______________________^
103103

104104
error: unreachable pattern
105-
--> $DIR/exhaustiveness-unreachable-pattern.rs:71:15
105+
--> $DIR/exhaustiveness-unreachable-pattern.rs:72:15
106106
|
107107
LL | | 0
108108
| ^
109109

110110
error: unreachable pattern
111-
--> $DIR/exhaustiveness-unreachable-pattern.rs:73:15
111+
--> $DIR/exhaustiveness-unreachable-pattern.rs:74:15
112112
|
113113
LL | | 0] => {}
114114
| ^
115115

116116
error: unreachable pattern
117-
--> $DIR/exhaustiveness-unreachable-pattern.rs:81:10
117+
--> $DIR/exhaustiveness-unreachable-pattern.rs:78:20
118+
|
119+
LL | (true, 0 | 0) => {}
120+
| ^
121+
122+
error: unreachable pattern
123+
--> $DIR/exhaustiveness-unreachable-pattern.rs:79:17
124+
|
125+
LL | (_, 0 | 0) => {}
126+
| ^
127+
128+
error: unreachable pattern
129+
--> $DIR/exhaustiveness-unreachable-pattern.rs:87:10
118130
|
119131
LL | [1
120132
| ^
121133

122134
error: unreachable pattern
123-
--> $DIR/exhaustiveness-unreachable-pattern.rs:93:10
135+
--> $DIR/exhaustiveness-unreachable-pattern.rs:99:10
124136
|
125137
LL | [true
126138
| ^^^^
127139

128140
error: unreachable pattern
129-
--> $DIR/exhaustiveness-unreachable-pattern.rs:100:36
141+
--> $DIR/exhaustiveness-unreachable-pattern.rs:106:36
130142
|
131143
LL | (true | false, None | Some(true
132144
| ^^^^
133145

134146
error: unreachable pattern
135-
--> $DIR/exhaustiveness-unreachable-pattern.rs:105:14
147+
--> $DIR/exhaustiveness-unreachable-pattern.rs:111:14
136148
|
137149
LL | (true
138150
| ^^^^
@@ -143,28 +155,34 @@ LL | (true | false, None | Some(t_or_f!())) => {}
143155
= note: this error originates in the macro `t_or_f` (in Nightly builds, run with -Z macro-backtrace for more info)
144156

145157
error: unreachable pattern
146-
--> $DIR/exhaustiveness-unreachable-pattern.rs:116:14
158+
--> $DIR/exhaustiveness-unreachable-pattern.rs:122:14
147159
|
148160
LL | Some(0
149161
| ^
150162

151163
error: unreachable pattern
152-
--> $DIR/exhaustiveness-unreachable-pattern.rs:135:19
164+
--> $DIR/exhaustiveness-unreachable-pattern.rs:141:19
153165
|
154166
LL | | false) => {}
155167
| ^^^^^
156168

157169
error: unreachable pattern
158-
--> $DIR/exhaustiveness-unreachable-pattern.rs:143:15
170+
--> $DIR/exhaustiveness-unreachable-pattern.rs:149:15
159171
|
160172
LL | | true) => {}
161173
| ^^^^
162174

163175
error: unreachable pattern
164-
--> $DIR/exhaustiveness-unreachable-pattern.rs:149:15
176+
--> $DIR/exhaustiveness-unreachable-pattern.rs:155:15
165177
|
166178
LL | | true,
167179
| ^^^^
168180

169-
error: aborting due to 26 previous errors
181+
error: unreachable pattern
182+
--> $DIR/exhaustiveness-unreachable-pattern.rs:160:15
183+
|
184+
LL | | (y, x) => {}
185+
| ^^^^^^
186+
187+
error: aborting due to 29 previous errors
170188

tests/ui/pattern/usefulness/integer-ranges/pointer-sized-int.allow.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0004]: non-exhaustive patterns: type `usize` is non-empty
2-
--> $DIR/pointer-sized-int.rs:54:11
2+
--> $DIR/pointer-sized-int.rs:59:11
33
|
44
LL | match 7usize {}
55
| ^^^^^^

tests/ui/pattern/usefulness/integer-ranges/pointer-sized-int.deny.stderr

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | match 0usize {
99
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
1010
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1111
|
12-
LL ~ 0 ..= usize::MAX => {},
12+
LL ~ 0..=usize::MAX => {},
1313
LL + usize::MAX.. => todo!()
1414
|
1515

@@ -24,12 +24,12 @@ LL | match 0isize {
2424
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
2525
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
2626
|
27-
LL ~ isize::MIN ..= isize::MAX => {},
27+
LL ~ isize::MIN..=isize::MAX => {},
2828
LL + ..isize::MIN | isize::MAX.. => todo!()
2929
|
3030

3131
error[E0004]: non-exhaustive patterns: `usize::MAX..` not covered
32-
--> $DIR/pointer-sized-int.rs:25:8
32+
--> $DIR/pointer-sized-int.rs:24:8
3333
|
3434
LL | m!(0usize, 0..=usize::MAX);
3535
| ^^^^^^ pattern `usize::MAX..` not covered
@@ -43,7 +43,7 @@ LL | match $s { $($t)+ => {}, usize::MAX.. => todo!() }
4343
| +++++++++++++++++++++++++
4444

4545
error[E0004]: non-exhaustive patterns: `usize::MAX..` not covered
46-
--> $DIR/pointer-sized-int.rs:27:8
46+
--> $DIR/pointer-sized-int.rs:26:8
4747
|
4848
LL | m!(0usize, 0..5 | 5..=usize::MAX);
4949
| ^^^^^^ pattern `usize::MAX..` not covered
@@ -57,7 +57,7 @@ LL | match $s { $($t)+ => {}, usize::MAX.. => todo!() }
5757
| +++++++++++++++++++++++++
5858

5959
error[E0004]: non-exhaustive patterns: `usize::MAX..` not covered
60-
--> $DIR/pointer-sized-int.rs:29:8
60+
--> $DIR/pointer-sized-int.rs:28:8
6161
|
6262
LL | m!(0usize, 0..usize::MAX | usize::MAX);
6363
| ^^^^^^ pattern `usize::MAX..` not covered
@@ -71,7 +71,7 @@ LL | match $s { $($t)+ => {}, usize::MAX.. => todo!() }
7171
| +++++++++++++++++++++++++
7272

7373
error[E0004]: non-exhaustive patterns: `(usize::MAX.., _)` not covered
74-
--> $DIR/pointer-sized-int.rs:31:8
74+
--> $DIR/pointer-sized-int.rs:30:8
7575
|
7676
LL | m!((0usize, true), (0..5, true) | (5..=usize::MAX, true) | (0..=usize::MAX, false));
7777
| ^^^^^^^^^^^^^^ pattern `(usize::MAX.., _)` not covered
@@ -85,7 +85,7 @@ LL | match $s { $($t)+ => {}, (usize::MAX.., _) => todo!() }
8585
| ++++++++++++++++++++++++++++++
8686

8787
error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
88-
--> $DIR/pointer-sized-int.rs:36:8
88+
--> $DIR/pointer-sized-int.rs:39:8
8989
|
9090
LL | m!(0isize, isize::MIN..=isize::MAX);
9191
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
@@ -99,7 +99,7 @@ LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
9999
| ++++++++++++++++++++++++++++++++++++++++
100100

101101
error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
102-
--> $DIR/pointer-sized-int.rs:38:8
102+
--> $DIR/pointer-sized-int.rs:41:8
103103
|
104104
LL | m!(0isize, isize::MIN..5 | 5..=isize::MAX);
105105
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
@@ -113,9 +113,9 @@ LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
113113
| ++++++++++++++++++++++++++++++++++++++++
114114

115115
error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
116-
--> $DIR/pointer-sized-int.rs:40:8
116+
--> $DIR/pointer-sized-int.rs:43:8
117117
|
118-
LL | m!(0isize, isize::MIN..isize::MAX | isize::MAX);
118+
LL | m!(0isize, isize::MIN..=-1 | 0 | 1..=isize::MAX);
119119
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
120120
|
121121
= note: the matched value is of type `isize`
@@ -126,37 +126,36 @@ help: ensure that all possible cases are being handled by adding a match arm wit
126126
LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
127127
| ++++++++++++++++++++++++++++++++++++++++
128128

129-
error[E0004]: non-exhaustive patterns: `(..isize::MIN, _)` and `(isize::MAX.., _)` not covered
130-
--> $DIR/pointer-sized-int.rs:42:8
129+
error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
130+
--> $DIR/pointer-sized-int.rs:45:8
131131
|
132-
LL | m!((0isize, true), (isize::MIN..5, true)
133-
| ^^^^^^^^^^^^^^ patterns `(..isize::MIN, _)` and `(isize::MAX.., _)` not covered
132+
LL | m!(0isize, isize::MIN..isize::MAX | isize::MAX);
133+
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
134134
|
135-
= note: the matched value is of type `(isize, bool)`
135+
= note: the matched value is of type `isize`
136136
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
137137
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
138138
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
139139
|
140-
LL | match $s { $($t)+ => {}, (..isize::MIN, _) | (isize::MAX.., _) => todo!() }
141-
| ++++++++++++++++++++++++++++++++++++++++++++++++++
140+
LL | match $s { $($t)+ => {}, ..isize::MIN | isize::MAX.. => todo!() }
141+
| ++++++++++++++++++++++++++++++++++++++++
142142

143-
error[E0004]: non-exhaustive patterns: `..isize::MIN` and `isize::MAX..` not covered
144-
--> $DIR/pointer-sized-int.rs:47:11
143+
error[E0004]: non-exhaustive patterns: `(..isize::MIN, _)` and `(isize::MAX.., _)` not covered
144+
--> $DIR/pointer-sized-int.rs:48:9
145145
|
146-
LL | match 0isize {
147-
| ^^^^^^ patterns `..isize::MIN` and `isize::MAX..` not covered
146+
LL | (0isize, true),
147+
| ^^^^^^^^^^^^^^ patterns `(..isize::MIN, _)` and `(isize::MAX.., _)` not covered
148148
|
149-
= note: the matched value is of type `isize`
149+
= note: the matched value is of type `(isize, bool)`
150150
= note: `isize` does not have fixed minimum and maximum values, so half-open ranges are necessary to match exhaustively
151151
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
152152
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
153153
|
154-
LL ~ 1 ..= isize::MAX => {},
155-
LL + ..isize::MIN | isize::MAX.. => todo!()
156-
|
154+
LL | match $s { $($t)+ => {}, (..isize::MIN, _) | (isize::MAX.., _) => todo!() }
155+
| ++++++++++++++++++++++++++++++++++++++++++++++++++
157156

158157
error[E0004]: non-exhaustive patterns: type `usize` is non-empty
159-
--> $DIR/pointer-sized-int.rs:54:11
158+
--> $DIR/pointer-sized-int.rs:59:11
160159
|
161160
LL | match 7usize {}
162161
| ^^^^^^

0 commit comments

Comments
 (0)