Also check if let chains with multiple lets in these two tests

This commit is contained in:
est31 2024-11-16 05:01:52 +01:00
parent ec239b888f
commit 427d9152d2
4 changed files with 43 additions and 6 deletions

View File

@ -10,6 +10,8 @@ fn main() {
//~^ ERROR: mutable more than once
if let Some(ref mut y @ ref mut z) = x && true {}
//~^ ERROR: mutable more than once
if let Some(_) = Some(()) && let Some(ref mut y @ ref mut z) = x && true {}
//~^ ERROR: mutable more than once
while let Some(ref mut y @ ref mut z) = x {}
//~^ ERROR: mutable more than once
while let Some(ref mut y @ ref mut z) = x && true {}

View File

@ -31,9 +31,9 @@ LL | if let Some(ref mut y @ ref mut z) = x && true {}
| value is mutably borrowed by `y` here
error: cannot borrow value as mutable more than once at a time
--> $DIR/conflicting_bindings.rs:13:20
--> $DIR/conflicting_bindings.rs:13:43
|
LL | while let Some(ref mut y @ ref mut z) = x {}
LL | if let Some(_) = Some(()) && let Some(ref mut y @ ref mut z) = x && true {}
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
| |
| value is mutably borrowed by `y` here
@ -41,13 +41,21 @@ LL | while let Some(ref mut y @ ref mut z) = x {}
error: cannot borrow value as mutable more than once at a time
--> $DIR/conflicting_bindings.rs:15:20
|
LL | while let Some(ref mut y @ ref mut z) = x {}
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
| |
| value is mutably borrowed by `y` here
error: cannot borrow value as mutable more than once at a time
--> $DIR/conflicting_bindings.rs:17:20
|
LL | while let Some(ref mut y @ ref mut z) = x && true {}
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
| |
| value is mutably borrowed by `y` here
error: cannot borrow value as mutable more than once at a time
--> $DIR/conflicting_bindings.rs:18:9
--> $DIR/conflicting_bindings.rs:20:9
|
LL | ref mut y @ ref mut z => {}
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
@ -55,12 +63,12 @@ LL | ref mut y @ ref mut z => {}
| value is mutably borrowed by `y` here
error: cannot borrow value as mutable more than once at a time
--> $DIR/conflicting_bindings.rs:21:24
--> $DIR/conflicting_bindings.rs:23:24
|
LL | () if let Some(ref mut y @ ref mut z) = x => {}
| ^^^^^^^^^ --------- value is mutably borrowed by `z` here
| |
| value is mutably borrowed by `y` here
error: aborting due to 8 previous errors
error: aborting due to 9 previous errors

View File

@ -94,4 +94,15 @@ fn use_in_arm_ok(c: bool) {
};
}
fn use_in_same_chain(c: bool) {
let x: Box<_> = Box::new(1);
let v = (1, 2);
match v {
(1, 2) if let y = x && c && let z = x => false, //~ ERROR use of moved value: `x`
_ => true,
};
}
fn main() {}

View File

@ -60,6 +60,22 @@ help: borrow this binding in the pattern to avoid moving the value
LL | (1, 2) if let ref y = x && c => false,
| +++
error: aborting due to 4 previous errors
error[E0382]: use of moved value: `x`
--> $DIR/move-guard-if-let-chain.rs:103:41
|
LL | let x: Box<_> = Box::new(1);
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
...
LL | (1, 2) if let y = x && c && let z = x => false,
| - ^ value used here after move
| |
| value moved here
|
help: borrow this binding in the pattern to avoid moving the value
|
LL | (1, 2) if let ref y = x && c && let z = x => false,
| +++
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0382`.