rust/tests/ui/moves/borrow-closures-instead-of-move.stderr
Esteban Küber 78d29ad8d6 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.
2024-03-17 21:32:26 +00:00

69 lines
2.3 KiB
Plaintext

error[E0382]: use of moved value: `f`
--> $DIR/borrow-closures-instead-of-move.rs:3:22
|
LL | fn takes_fn(f: impl Fn()) {
| - move occurs because `f` has type `impl Fn()`, which does not implement the `Copy` trait
LL | loop {
| ---- inside of this loop
LL | takes_fnonce(f);
| ^ value moved here, in previous iteration of loop
|
note: consider changing this parameter type in function `takes_fnonce` to borrow instead if owning the value isn't necessary
--> $DIR/borrow-closures-instead-of-move.rs:34:20
|
LL | fn takes_fnonce(_: impl FnOnce()) {}
| ------------ ^^^^^^^^^^^^^ this parameter takes ownership of the value
| |
| in this function
help: consider borrowing `f`
|
LL | takes_fnonce(&f);
| +
error[E0382]: use of moved value: `m`
--> $DIR/borrow-closures-instead-of-move.rs:14:18
|
LL | fn takes_fn_mut(m: impl FnMut()) {
| - move occurs because `m` has type `impl FnMut()`, which does not implement the `Copy` trait
LL | if maybe() {
LL | takes_fnonce(m);
| - value moved here
...
LL | takes_fnonce(m);
| ^ value used here after move
|
note: consider changing this parameter type in function `takes_fnonce` to borrow instead if owning the value isn't necessary
--> $DIR/borrow-closures-instead-of-move.rs:34:20
|
LL | fn takes_fnonce(_: impl FnOnce()) {}
| ------------ ^^^^^^^^^^^^^ this parameter takes ownership of the value
| |
| in this function
help: consider mutably borrowing `m`
|
LL | takes_fnonce(&mut m);
| ++++
error[E0382]: borrow of moved value: `closure`
--> $DIR/borrow-closures-instead-of-move.rs:25:5
|
LL | takes_fnonce(closure);
| ------- value moved here
LL |
LL | closure();
| ^^^^^^^ value borrowed here after move
|
note: closure cannot be moved more than once as it is not `Copy` due to moving the variable `x` out of its environment
--> $DIR/borrow-closures-instead-of-move.rs:21:9
|
LL | x += 1;
| ^
help: consider mutably borrowing `closure`
|
LL | takes_fnonce(&mut closure);
| ++++
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0382`.