mirror of
https://github.com/rust-lang/rust.git
synced 2025-11-26 21:08:12 +00:00
Sometimes move errors are because of a misplaced `continue`, but we didn't
surface that anywhere. Now when there are more than one set of nested loops
we show them out and point at the `continue` and `break` expressions within
that might need to go elsewhere.
```
error[E0382]: use of moved value: `foo`
--> $DIR/nested-loop-moved-value-wrong-continue.rs:46:18
|
LL | for foo in foos {
| ---
| |
| this reinitialization might get skipped
| move occurs because `foo` has type `String`, which does not implement the `Copy` trait
...
LL | for bar in &bars {
| ---------------- inside of this loop
...
LL | baz.push(foo);
| --- value moved here, in previous iteration of loop
...
LL | qux.push(foo);
| ^^^ value used here after move
|
note: verify that your loop breaking logic is correct
--> $DIR/nested-loop-moved-value-wrong-continue.rs:41:17
|
LL | for foo in foos {
| ---------------
...
LL | for bar in &bars {
| ----------------
...
LL | continue;
| ^^^^^^^^ this `continue` advances the loop at line 33
help: consider moving the expression out of the loop so it is only moved once
|
LL ~ let mut value = baz.push(foo);
LL ~ for bar in &bars {
LL |
...
LL | if foo == *bar {
LL ~ value;
|
help: consider cloning the value if the performance cost is acceptable
|
LL | baz.push(foo.clone());
| ++++++++
```
Fix #92531.
60 lines
1.3 KiB
Rust
60 lines
1.3 KiB
Rust
fn iter<T>(vec: Vec<T>) -> impl Iterator<Item = T> {
|
|
vec.into_iter()
|
|
}
|
|
fn foo() {
|
|
let vec = vec!["one", "two", "three"];
|
|
while let Some(item) = iter(vec).next() { //~ ERROR use of moved value
|
|
println!("{:?}", item);
|
|
}
|
|
}
|
|
fn bar() {
|
|
let vec = vec!["one", "two", "three"];
|
|
loop {
|
|
let Some(item) = iter(vec).next() else { //~ ERROR use of moved value
|
|
break;
|
|
};
|
|
println!("{:?}", item);
|
|
}
|
|
}
|
|
fn baz() {
|
|
let vec = vec!["one", "two", "three"];
|
|
loop {
|
|
let item = iter(vec).next(); //~ ERROR use of moved value
|
|
if item.is_none() {
|
|
break;
|
|
}
|
|
println!("{:?}", item);
|
|
}
|
|
}
|
|
fn qux() {
|
|
let vec = vec!["one", "two", "three"];
|
|
loop {
|
|
if let Some(item) = iter(vec).next() { //~ ERROR use of moved value
|
|
println!("{:?}", item);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
fn zap() {
|
|
loop {
|
|
let vec = vec!["one", "two", "three"];
|
|
loop {
|
|
loop {
|
|
loop {
|
|
if let Some(item) = iter(vec).next() { //~ ERROR use of moved value
|
|
println!("{:?}", item);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
fn main() {
|
|
foo();
|
|
bar();
|
|
baz();
|
|
qux();
|
|
zap();
|
|
}
|