From 904652b2d05d967deadf201fc35e8343a822c7a6 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 1 May 2024 20:46:06 +0000 Subject: 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); | ``` --- tests/ui/borrowck/borrowck-move-by-capture.stderr | 2 +- .../borrowck-move-moved-value-into-closure.stderr | 6 ++++++ .../moves-based-on-type-capture-clause-bad.fixed | 11 ++++++++++ .../moves-based-on-type-capture-clause-bad.rs | 1 + .../moves-based-on-type-capture-clause-bad.stderr | 8 ++++++- tests/ui/moves/no-capture-arc.rs | 15 +++++++++++++ tests/ui/moves/no-capture-arc.stderr | 25 ++++++++++++++++++++++ tests/ui/moves/no-reuse-move-arc.fixed | 17 +++++++++++++++ tests/ui/moves/no-reuse-move-arc.rs | 16 ++++++++++++++ tests/ui/moves/no-reuse-move-arc.stderr | 25 ++++++++++++++++++++++ tests/ui/no-capture-arc.rs | 15 ------------- tests/ui/no-capture-arc.stderr | 19 ---------------- tests/ui/no-reuse-move-arc.rs | 15 ------------- tests/ui/no-reuse-move-arc.stderr | 19 ---------------- tests/ui/suggestions/option-content-move3.stderr | 2 +- 15 files changed, 125 insertions(+), 71 deletions(-) create mode 100644 tests/ui/moves/moves-based-on-type-capture-clause-bad.fixed create mode 100644 tests/ui/moves/no-capture-arc.rs create mode 100644 tests/ui/moves/no-capture-arc.stderr create mode 100644 tests/ui/moves/no-reuse-move-arc.fixed create mode 100644 tests/ui/moves/no-reuse-move-arc.rs create mode 100644 tests/ui/moves/no-reuse-move-arc.stderr delete mode 100644 tests/ui/no-capture-arc.rs delete mode 100644 tests/ui/no-capture-arc.stderr delete mode 100644 tests/ui/no-reuse-move-arc.rs delete mode 100644 tests/ui/no-reuse-move-arc.stderr (limited to 'tests') diff --git a/tests/ui/borrowck/borrowck-move-by-capture.stderr b/tests/ui/borrowck/borrowck-move-by-capture.stderr index 9915acfe065..58d5e90e990 100644 --- a/tests/ui/borrowck/borrowck-move-by-capture.stderr +++ b/tests/ui/borrowck/borrowck-move-by-capture.stderr @@ -12,7 +12,7 @@ LL | let _h = to_fn_once(move || -> isize { *bar }); | | move occurs because `bar` has type `Box`, 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 }); diff --git a/tests/ui/borrowck/borrowck-move-moved-value-into-closure.stderr b/tests/ui/borrowck/borrowck-move-moved-value-into-closure.stderr index 6a77d86f250..5ddc6a6d82d 100644 --- a/tests/ui/borrowck/borrowck-move-moved-value-into-closure.stderr +++ b/tests/ui/borrowck/borrowck-move-moved-value-into-closure.stderr @@ -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 diff --git a/tests/ui/moves/moves-based-on-type-capture-clause-bad.fixed b/tests/ui/moves/moves-based-on-type-capture-clause-bad.fixed new file mode 100644 index 00000000000..04a183ca96b --- /dev/null +++ b/tests/ui/moves/moves-based-on-type-capture-clause-bad.fixed @@ -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 +} diff --git a/tests/ui/moves/moves-based-on-type-capture-clause-bad.rs b/tests/ui/moves/moves-based-on-type-capture-clause-bad.rs index 9d7277c1c24..c9a7f2c8ed8 100644 --- a/tests/ui/moves/moves-based-on-type-capture-clause-bad.rs +++ b/tests/ui/moves/moves-based-on-type-capture-clause-bad.rs @@ -1,3 +1,4 @@ +//@ run-rustfix use std::thread; fn main() { diff --git a/tests/ui/moves/moves-based-on-type-capture-clause-bad.stderr b/tests/ui/moves/moves-based-on-type-capture-clause-bad.stderr index c2b9aeab237..17049fe6731 100644 --- a/tests/ui/moves/moves-based-on-type-capture-clause-bad.stderr +++ b/tests/ui/moves/moves-based-on-type-capture-clause-bad.stderr @@ -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 diff --git a/tests/ui/moves/no-capture-arc.rs b/tests/ui/moves/no-capture-arc.rs new file mode 100644 index 00000000000..9c957a4e01b --- /dev/null +++ b/tests/ui/moves/no-capture-arc.rs @@ -0,0 +1,15 @@ +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); + + thread::spawn(move|| { + assert_eq!((*arc_v)[3], 4); + }); + + assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v` + + println!("{:?}", *arc_v); +} diff --git a/tests/ui/moves/no-capture-arc.stderr b/tests/ui/moves/no-capture-arc.stderr new file mode 100644 index 00000000000..6d4a867fa88 --- /dev/null +++ b/tests/ui/moves/no-capture-arc.stderr @@ -0,0 +1,25 @@ +error[E0382]: borrow of moved value: `arc_v` + --> $DIR/no-capture-arc.rs:12:18 + | +LL | let arc_v = Arc::new(v); + | ----- move occurs because `arc_v` has type `Arc>`, which does not implement the `Copy` trait +LL | +LL | thread::spawn(move|| { + | ------ value moved into closure here +LL | assert_eq!((*arc_v)[3], 4); + | ----- variable moved due to use in closure +... +LL | assert_eq!((*arc_v)[2], 3); + | ^^^^^ value borrowed here after move + | + = note: borrow occurs due to deref coercion to `Vec` +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 + +For more information about this error, try `rustc --explain E0382`. diff --git a/tests/ui/moves/no-reuse-move-arc.fixed b/tests/ui/moves/no-reuse-move-arc.fixed new file mode 100644 index 00000000000..a5dac8cc14b --- /dev/null +++ b/tests/ui/moves/no-reuse-move-arc.fixed @@ -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); +} diff --git a/tests/ui/moves/no-reuse-move-arc.rs b/tests/ui/moves/no-reuse-move-arc.rs new file mode 100644 index 00000000000..0d67aa56489 --- /dev/null +++ b/tests/ui/moves/no-reuse-move-arc.rs @@ -0,0 +1,16 @@ +//@ 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); + + thread::spawn(move|| { + assert_eq!((*arc_v)[3], 4); + }); + + assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v` + + println!("{:?}", *arc_v); +} diff --git a/tests/ui/moves/no-reuse-move-arc.stderr b/tests/ui/moves/no-reuse-move-arc.stderr new file mode 100644 index 00000000000..aff979af905 --- /dev/null +++ b/tests/ui/moves/no-reuse-move-arc.stderr @@ -0,0 +1,25 @@ +error[E0382]: borrow of moved value: `arc_v` + --> $DIR/no-reuse-move-arc.rs:13:18 + | +LL | let arc_v = Arc::new(v); + | ----- move occurs because `arc_v` has type `Arc>`, which does not implement the `Copy` trait +LL | +LL | thread::spawn(move|| { + | ------ value moved into closure here +LL | assert_eq!((*arc_v)[3], 4); + | ----- variable moved due to use in closure +... +LL | assert_eq!((*arc_v)[2], 3); + | ^^^^^ value borrowed here after move + | + = note: borrow occurs due to deref coercion to `Vec` +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 + +For more information about this error, try `rustc --explain E0382`. diff --git a/tests/ui/no-capture-arc.rs b/tests/ui/no-capture-arc.rs deleted file mode 100644 index 9c957a4e01b..00000000000 --- a/tests/ui/no-capture-arc.rs +++ /dev/null @@ -1,15 +0,0 @@ -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); - - thread::spawn(move|| { - assert_eq!((*arc_v)[3], 4); - }); - - assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v` - - println!("{:?}", *arc_v); -} diff --git a/tests/ui/no-capture-arc.stderr b/tests/ui/no-capture-arc.stderr deleted file mode 100644 index 9c1f5c65066..00000000000 --- a/tests/ui/no-capture-arc.stderr +++ /dev/null @@ -1,19 +0,0 @@ -error[E0382]: borrow of moved value: `arc_v` - --> $DIR/no-capture-arc.rs:12:18 - | -LL | let arc_v = Arc::new(v); - | ----- move occurs because `arc_v` has type `Arc>`, which does not implement the `Copy` trait -LL | -LL | thread::spawn(move|| { - | ------ value moved into closure here -LL | assert_eq!((*arc_v)[3], 4); - | ----- variable moved due to use in closure -... -LL | assert_eq!((*arc_v)[2], 3); - | ^^^^^ value borrowed here after move - | - = note: borrow occurs due to deref coercion to `Vec` - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0382`. diff --git a/tests/ui/no-reuse-move-arc.rs b/tests/ui/no-reuse-move-arc.rs deleted file mode 100644 index 9c957a4e01b..00000000000 --- a/tests/ui/no-reuse-move-arc.rs +++ /dev/null @@ -1,15 +0,0 @@ -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); - - thread::spawn(move|| { - assert_eq!((*arc_v)[3], 4); - }); - - assert_eq!((*arc_v)[2], 3); //~ ERROR borrow of moved value: `arc_v` - - println!("{:?}", *arc_v); -} diff --git a/tests/ui/no-reuse-move-arc.stderr b/tests/ui/no-reuse-move-arc.stderr deleted file mode 100644 index 61f4837dc0e..00000000000 --- a/tests/ui/no-reuse-move-arc.stderr +++ /dev/null @@ -1,19 +0,0 @@ -error[E0382]: borrow of moved value: `arc_v` - --> $DIR/no-reuse-move-arc.rs:12:18 - | -LL | let arc_v = Arc::new(v); - | ----- move occurs because `arc_v` has type `Arc>`, which does not implement the `Copy` trait -LL | -LL | thread::spawn(move|| { - | ------ value moved into closure here -LL | assert_eq!((*arc_v)[3], 4); - | ----- variable moved due to use in closure -... -LL | assert_eq!((*arc_v)[2], 3); - | ^^^^^ value borrowed here after move - | - = note: borrow occurs due to deref coercion to `Vec` - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0382`. diff --git a/tests/ui/suggestions/option-content-move3.stderr b/tests/ui/suggestions/option-content-move3.stderr index a20dcce1ee3..faaf8a9df9d 100644 --- a/tests/ui/suggestions/option-content-move3.stderr +++ b/tests/ui/suggestions/option-content-move3.stderr @@ -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(); -- cgit 1.4.1-3-g733a5