Skip to content

Commit e2ebaa1

Browse files
committed
Add if let tests
1 parent 8021192 commit e2ebaa1

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed

tests/ui/nll/match-cfg-fake-edges.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,34 @@ fn all_patterns_are_tested() {
88
// Even though `x` is never actually moved out of, we don't want borrowck results to be based on
99
// whether MIR lowering reveals which patterns are unreachable.
1010
let x = String::new();
11-
let _ = match true {
11+
match true {
1212
_ => {},
1313
_ => drop(x),
14-
};
14+
}
1515
// Borrowck must not know the second arm is never run.
1616
drop(x); //~ ERROR use of moved value
1717

18+
let x = String::new();
19+
if let _ = true { //~ WARN irrefutable
20+
} else {
21+
drop(x)
22+
}
23+
// Borrowck must not know the else branch is never run.
24+
drop(x); //~ ERROR use of moved value
25+
1826
let x = (String::new(), String::new());
1927
match x {
2028
(y, _) | (_, y) => (),
2129
}
2230
&x.0; //~ ERROR borrow of moved value
2331
// Borrowck must not know the second pattern never matches.
2432
&x.1; //~ ERROR borrow of moved value
33+
34+
let x = (String::new(), String::new());
35+
let ((y, _) | (_, y)) = x;
36+
&x.0; //~ ERROR borrow of moved value
37+
// Borrowck must not know the second pattern never matches.
38+
&x.1; //~ ERROR borrow of moved value
2539
}
2640

2741
#[rustfmt::skip]

tests/ui/nll/match-cfg-fake-edges.stderr

+64-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
warning: irrefutable `if let` pattern
2+
--> $DIR/match-cfg-fake-edges.rs:19:8
3+
|
4+
LL | if let _ = true {
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: this pattern will always match, so the `if let` is useless
8+
= help: consider replacing the `if let` with a `let`
9+
= note: `#[warn(irrefutable_let_patterns)]` on by default
10+
111
error[E0382]: use of moved value: `x`
212
--> $DIR/match-cfg-fake-edges.rs:16:10
313
|
@@ -15,8 +25,25 @@ help: consider cloning the value if the performance cost is acceptable
1525
LL | _ => drop(x.clone()),
1626
| ++++++++
1727

28+
error[E0382]: use of moved value: `x`
29+
--> $DIR/match-cfg-fake-edges.rs:24:10
30+
|
31+
LL | let x = String::new();
32+
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
33+
...
34+
LL | drop(x)
35+
| - value moved here
36+
...
37+
LL | drop(x);
38+
| ^ value used here after move
39+
|
40+
help: consider cloning the value if the performance cost is acceptable
41+
|
42+
LL | drop(x.clone())
43+
| ++++++++
44+
1845
error[E0382]: borrow of moved value: `x.0`
19-
--> $DIR/match-cfg-fake-edges.rs:22:5
46+
--> $DIR/match-cfg-fake-edges.rs:30:5
2047
|
2148
LL | (y, _) | (_, y) => (),
2249
| - value moved here
@@ -31,7 +58,7 @@ LL | (ref y, _) | (_, y) => (),
3158
| +++
3259

3360
error[E0382]: borrow of moved value: `x.1`
34-
--> $DIR/match-cfg-fake-edges.rs:24:5
61+
--> $DIR/match-cfg-fake-edges.rs:32:5
3562
|
3663
LL | (y, _) | (_, y) => (),
3764
| - value moved here
@@ -45,8 +72,37 @@ help: borrow this binding in the pattern to avoid moving the value
4572
LL | (y, _) | (_, ref y) => (),
4673
| +++
4774

75+
error[E0382]: borrow of moved value: `x.0`
76+
--> $DIR/match-cfg-fake-edges.rs:36:5
77+
|
78+
LL | let ((y, _) | (_, y)) = x;
79+
| - value moved here
80+
LL | &x.0;
81+
| ^^^^ value borrowed here after move
82+
|
83+
= note: move occurs because `x.0` has type `String`, which does not implement the `Copy` trait
84+
help: borrow this binding in the pattern to avoid moving the value
85+
|
86+
LL | let ((ref y, _) | (_, y)) = x;
87+
| +++
88+
89+
error[E0382]: borrow of moved value: `x.1`
90+
--> $DIR/match-cfg-fake-edges.rs:38:5
91+
|
92+
LL | let ((y, _) | (_, y)) = x;
93+
| - value moved here
94+
...
95+
LL | &x.1;
96+
| ^^^^ value borrowed here after move
97+
|
98+
= note: move occurs because `x.1` has type `String`, which does not implement the `Copy` trait
99+
help: borrow this binding in the pattern to avoid moving the value
100+
|
101+
LL | let ((y, _) | (_, ref y)) = x;
102+
| +++
103+
48104
error[E0381]: used binding `x` is possibly-uninitialized
49-
--> $DIR/match-cfg-fake-edges.rs:58:19
105+
--> $DIR/match-cfg-fake-edges.rs:72:19
50106
|
51107
LL | let x;
52108
| - binding declared here but left uninitialized
@@ -57,7 +113,7 @@ LL | _ => drop(x),
57113
| if this pattern is matched, `x` is not initialized
58114

59115
error[E0381]: used binding `x` isn't initialized
60-
--> $DIR/match-cfg-fake-edges.rs:65:16
116+
--> $DIR/match-cfg-fake-edges.rs:79:16
61117
|
62118
LL | let x;
63119
| - binding declared here but left uninitialized
@@ -74,7 +130,7 @@ LL | let x = 0;
74130
| +++
75131

76132
error[E0381]: used binding `x` isn't initialized
77-
--> $DIR/match-cfg-fake-edges.rs:72:31
133+
--> $DIR/match-cfg-fake-edges.rs:86:31
78134
|
79135
LL | let x;
80136
| - binding declared here but left uninitialized
@@ -90,7 +146,7 @@ LL | let x = 0;
90146
| +++
91147

92148
error[E0382]: use of moved value: `x`
93-
--> $DIR/match-cfg-fake-edges.rs:85:22
149+
--> $DIR/match-cfg-fake-edges.rs:99:22
94150
|
95151
LL | let x = String::new();
96152
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
@@ -107,7 +163,7 @@ LL | false if { drop(x.clone()); true } => {},
107163
| ++++++++
108164

109165
error[E0382]: use of moved value: `x`
110-
--> $DIR/match-cfg-fake-edges.rs:100:22
166+
--> $DIR/match-cfg-fake-edges.rs:114:22
111167
|
112168
LL | let x = String::new();
113169
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
@@ -122,7 +178,7 @@ help: consider cloning the value if the performance cost is acceptable
122178
LL | false if let Some(()) = { drop(x.clone()); Some(()) } => {},
123179
| ++++++++
124180

125-
error: aborting due to 8 previous errors
181+
error: aborting due to 11 previous errors; 1 warning emitted
126182

127183
Some errors have detailed explanations: E0381, E0382.
128184
For more information about an error, try `rustc --explain E0381`.

0 commit comments

Comments
 (0)