Skip to content

Commit

Permalink
Add if let tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Apr 3, 2024
1 parent 8021192 commit e2ebaa1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
18 changes: 16 additions & 2 deletions tests/ui/nll/match-cfg-fake-edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@ fn all_patterns_are_tested() {
// Even though `x` is never actually moved out of, we don't want borrowck results to be based on
// whether MIR lowering reveals which patterns are unreachable.
let x = String::new();
let _ = match true {
match true {
_ => {},
_ => drop(x),
};
}
// Borrowck must not know the second arm is never run.
drop(x); //~ ERROR use of moved value

let x = String::new();
if let _ = true { //~ WARN irrefutable
} else {
drop(x)
}
// Borrowck must not know the else branch is never run.
drop(x); //~ ERROR use of moved value

let x = (String::new(), String::new());
match x {
(y, _) | (_, y) => (),
}
&x.0; //~ ERROR borrow of moved value
// Borrowck must not know the second pattern never matches.
&x.1; //~ ERROR borrow of moved value

let x = (String::new(), String::new());
let ((y, _) | (_, y)) = x;
&x.0; //~ ERROR borrow of moved value
// Borrowck must not know the second pattern never matches.
&x.1; //~ ERROR borrow of moved value
}

#[rustfmt::skip]
Expand Down
72 changes: 64 additions & 8 deletions tests/ui/nll/match-cfg-fake-edges.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
warning: irrefutable `if let` pattern
--> $DIR/match-cfg-fake-edges.rs:19:8
|
LL | if let _ = true {
| ^^^^^^^^^^^^
|
= note: this pattern will always match, so the `if let` is useless
= help: consider replacing the `if let` with a `let`
= note: `#[warn(irrefutable_let_patterns)]` on by default

error[E0382]: use of moved value: `x`
--> $DIR/match-cfg-fake-edges.rs:16:10
|
Expand All @@ -15,8 +25,25 @@ help: consider cloning the value if the performance cost is acceptable
LL | _ => drop(x.clone()),
| ++++++++

error[E0382]: use of moved value: `x`
--> $DIR/match-cfg-fake-edges.rs:24:10
|
LL | let x = String::new();
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
...
LL | drop(x)
| - value moved here
...
LL | drop(x);
| ^ value used here after move
|
help: consider cloning the value if the performance cost is acceptable
|
LL | drop(x.clone())
| ++++++++

error[E0382]: borrow of moved value: `x.0`
--> $DIR/match-cfg-fake-edges.rs:22:5
--> $DIR/match-cfg-fake-edges.rs:30:5
|
LL | (y, _) | (_, y) => (),
| - value moved here
Expand All @@ -31,7 +58,7 @@ LL | (ref y, _) | (_, y) => (),
| +++

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

error[E0382]: borrow of moved value: `x.0`
--> $DIR/match-cfg-fake-edges.rs:36:5
|
LL | let ((y, _) | (_, y)) = x;
| - value moved here
LL | &x.0;
| ^^^^ value borrowed here after move
|
= note: move occurs because `x.0` has type `String`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ((ref y, _) | (_, y)) = x;
| +++

error[E0382]: borrow of moved value: `x.1`
--> $DIR/match-cfg-fake-edges.rs:38:5
|
LL | let ((y, _) | (_, y)) = x;
| - value moved here
...
LL | &x.1;
| ^^^^ value borrowed here after move
|
= note: move occurs because `x.1` has type `String`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | let ((y, _) | (_, ref y)) = x;
| +++

error[E0381]: used binding `x` is possibly-uninitialized
--> $DIR/match-cfg-fake-edges.rs:58:19
--> $DIR/match-cfg-fake-edges.rs:72:19
|
LL | let x;
| - binding declared here but left uninitialized
Expand All @@ -57,7 +113,7 @@ LL | _ => drop(x),
| if this pattern is matched, `x` is not initialized

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

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

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

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

error: aborting due to 8 previous errors
error: aborting due to 11 previous errors; 1 warning emitted

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

0 comments on commit e2ebaa1

Please sign in to comment.