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:
Matthias Krüger 2025-06-26 15:47:16 +02:00 committed by GitHub
commit aa8ba54caf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 85 additions and 33 deletions

View file

@ -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 });

View file

@ -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

View 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
}

View file

@ -1,3 +1,4 @@
//@ run-rustfix
use std::thread;
fn main() {

View file

@ -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

View file

@ -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

View 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);
}

View file

@ -1,3 +1,4 @@
//@ run-rustfix
use std::sync::Arc;
use std::thread;

View file

@ -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

View file

@ -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();