summary refs log tree commit diff
path: root/tests/ui/moves/nested-loop-moved-value-wrong-continue.rs
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-02-26 22:54:34 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-03-17 21:32:26 +0000
commit78d29ad8d6483ab01b8a380ff3a9598ae23cfa05 (patch)
tree3e163b3fe23a93c2ffb3cba00117abecaf923541 /tests/ui/moves/nested-loop-moved-value-wrong-continue.rs
parent14473adf425623268252a4d10b5dd0d4f458daad (diff)
downloadrust-78d29ad8d6483ab01b8a380ff3a9598ae23cfa05.tar.gz
rust-78d29ad8d6483ab01b8a380ff3a9598ae23cfa05.zip
Point at `continue` and `break` that might be in the wrong place
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.
Diffstat (limited to 'tests/ui/moves/nested-loop-moved-value-wrong-continue.rs')
-rw-r--r--tests/ui/moves/nested-loop-moved-value-wrong-continue.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/ui/moves/nested-loop-moved-value-wrong-continue.rs b/tests/ui/moves/nested-loop-moved-value-wrong-continue.rs
index 618d820a2ab..0235b291df5 100644
--- a/tests/ui/moves/nested-loop-moved-value-wrong-continue.rs
+++ b/tests/ui/moves/nested-loop-moved-value-wrong-continue.rs
@@ -1,3 +1,27 @@
+fn foo() {
+    let foos = vec![String::new()];
+    let bars = vec![""];
+    let mut baz = vec![];
+    let mut qux = vec![];
+    for foo in foos { for bar in &bars { if foo == *bar {
+    //~^ NOTE this reinitialization might get skipped
+    //~| NOTE move occurs because `foo` has type `String`
+    //~| NOTE inside of this loop
+    //~| HELP consider moving the expression out of the loop
+    //~| NOTE in this expansion of desugaring of `for` loop
+        baz.push(foo);
+        //~^ NOTE value moved here
+        //~| HELP consider cloning the value
+        continue;
+        //~^ NOTE verify that your loop breaking logic is correct
+        //~| NOTE this `continue` advances the loop at $DIR/nested-loop-moved-value-wrong-continue.rs:6:23
+    } }
+    qux.push(foo);
+    //~^ ERROR use of moved value
+    //~| NOTE value used here
+    }
+}
+
 fn main() {
     let foos = vec![String::new()];
     let bars = vec![""];
@@ -15,6 +39,8 @@ fn main() {
                 //~^ NOTE value moved here
                 //~| HELP consider cloning the value
                 continue;
+                //~^ NOTE verify that your loop breaking logic is correct
+                //~| NOTE this `continue` advances the loop at line 33
             }
         }
         qux.push(foo);