Rollup merge of #124595 - estebank:issue-104232, r=davidtwco
Suggest cloning `Arc` moved into closure
```
error[E0382]: borrow of moved value: `x`
--> $DIR/moves-based-on-type-capture-clause-bad.rs:9:20
|
LL | let x = "Hello world!".to_string();
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
LL | thread::spawn(move || {
| ------- value moved into closure here
LL | println!("{}", x);
| - variable moved due to use in closure
LL | });
LL | println!("{}", x);
| ^ value borrowed here after move
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value before moving it into the closure
|
LL ~ let value = x.clone();
LL ~ thread::spawn(move || {
LL ~ println!("{}", value);
|
```
Fix rust-lang/rust#104232.
This commit is contained in:
commit
aa8ba54caf
13 changed files with 85 additions and 33 deletions
|
|
@ -12,7 +12,7 @@ LL | let _h = to_fn_once(move || -> isize { *bar });
|
|||
| | move occurs because `bar` has type `Box<isize>`, which does not implement the `Copy` trait
|
||||
| `bar` is moved here
|
||||
|
|
||||
help: clone the value before moving it into the closure
|
||||
help: consider cloning the value before moving it into the closure
|
||||
|
|
||||
LL ~ let value = bar.clone();
|
||||
LL ~ let _h = to_fn_once(move || -> isize { value });
|
||||
|
|
|
|||
|
|
@ -12,6 +12,12 @@ LL | call_f(move|| { *t + 1 });
|
|||
| ^^^^^^ -- use occurs due to use in closure
|
||||
| |
|
||||
| value used here after move
|
||||
|
|
||||
help: consider cloning the value before moving it into the closure
|
||||
|
|
||||
LL ~ let value = t.clone();
|
||||
LL ~ call_f(move|| { value + 1 });
|
||||
|
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
|
|
|||
11
tests/ui/moves/moves-based-on-type-capture-clause-bad.fixed
Normal file
11
tests/ui/moves/moves-based-on-type-capture-clause-bad.fixed
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
//@ run-rustfix
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
let x = "Hello world!".to_string();
|
||||
let value = x.clone();
|
||||
thread::spawn(move || {
|
||||
println!("{}", value);
|
||||
});
|
||||
println!("{}", x); //~ ERROR borrow of moved value
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
//@ run-rustfix
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error[E0382]: borrow of moved value: `x`
|
||||
--> $DIR/moves-based-on-type-capture-clause-bad.rs:8:20
|
||||
--> $DIR/moves-based-on-type-capture-clause-bad.rs:9:20
|
||||
|
|
||||
LL | let x = "Hello world!".to_string();
|
||||
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
|
||||
|
|
@ -12,6 +12,12 @@ LL | println!("{}", x);
|
|||
| ^ value borrowed here after move
|
||||
|
|
||||
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
help: consider cloning the value before moving it into the closure
|
||||
|
|
||||
LL ~ let value = x.clone();
|
||||
LL ~ thread::spawn(move || {
|
||||
LL ~ println!("{}", value);
|
||||
|
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,12 @@ LL | assert_eq!((*arc_v)[2], 3);
|
|||
| ^^^^^ value borrowed here after move
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `Vec<i32>`
|
||||
help: consider cloning the value before moving it into the closure
|
||||
|
|
||||
LL ~ let value = arc_v.clone();
|
||||
LL ~ thread::spawn(move|| {
|
||||
LL ~ assert_eq!((*value)[3], 4);
|
||||
|
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
17
tests/ui/moves/no-reuse-move-arc.fixed
Normal file
17
tests/ui/moves/no-reuse-move-arc.fixed
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
//@ run-rustfix
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
let arc_v = Arc::new(v);
|
||||
|
||||
let value = arc_v.clone();
|
||||
thread::spawn(move|| {
|
||||
assert_eq!((*value)[3], 4);
|
||||
});
|
||||
|
||||
assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v`
|
||||
|
||||
println!("{:?}", *arc_v);
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
//@ run-rustfix
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
error[E0382]: borrow of moved value: `arc_v`
|
||||
--> $DIR/no-reuse-move-arc.rs:12:18
|
||||
--> $DIR/no-reuse-move-arc.rs:13:18
|
||||
|
|
||||
LL | let arc_v = Arc::new(v);
|
||||
| ----- move occurs because `arc_v` has type `Arc<Vec<i32>>`, which does not implement the `Copy` trait
|
||||
|
|
@ -13,6 +13,12 @@ LL | assert_eq!((*arc_v)[2], 3);
|
|||
| ^^^^^ value borrowed here after move
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `Vec<i32>`
|
||||
help: consider cloning the value before moving it into the closure
|
||||
|
|
||||
LL ~ let value = arc_v.clone();
|
||||
LL ~ thread::spawn(move|| {
|
||||
LL ~ assert_eq!((*value)[3], 4);
|
||||
|
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ LL | let x = var;
|
|||
| variable moved due to use in closure
|
||||
| move occurs because `var` has type `NotCopyableButCloneable`, which does not implement the `Copy` trait
|
||||
|
|
||||
help: clone the value before moving it into the closure
|
||||
help: consider cloning the value before moving it into the closure
|
||||
|
|
||||
LL ~ {
|
||||
LL + let value = var.clone();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue