Skip to content

Commit a0ee8e6

Browse files
committed
Suggest .clone() in some move errors
``` error[E0507]: cannot move out of `*x` which is behind a shared reference --> $DIR/borrowck-fn-in-const-a.rs:6:16 | LL | return *x | ^^ move occurs because `*x` has type `String`, which does not implement the `Copy` trait | help: consider cloning the value if the performance cost is acceptable | LL - return *x LL + return x.clone() | ```
1 parent 1964b93 commit a0ee8e6

35 files changed

+296
-29
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
474474
Some(desc) => format!("`{desc}`"),
475475
None => "value".to_string(),
476476
};
477+
478+
if let Some(expr) = self.find_expr(span) {
479+
self.suggest_cloning(err, place_ty, expr, span);
480+
}
481+
477482
err.subdiagnostic(
478483
self.dcx(),
479484
crate::session_diagnostics::TypeNoCopy::Label {
@@ -582,6 +587,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
582587

583588
if binds_to.len() == 1 {
584589
let place_desc = &format!("`{}`", self.local_names[*local].unwrap());
590+
591+
if let Some(expr) = self.find_expr(binding_span) {
592+
self.suggest_cloning(err, bind_to.ty, expr, binding_span);
593+
}
594+
585595
err.subdiagnostic(
586596
self.dcx(),
587597
crate::session_diagnostics::TypeNoCopy::Label {

tests/ui/borrowck/borrowck-fn-in-const-a.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0507]: cannot move out of `*x` which is behind a shared reference
33
|
44
LL | return *x
55
| ^^ move occurs because `*x` has type `String`, which does not implement the `Copy` trait
6+
|
7+
help: consider cloning the value if the performance cost is acceptable
8+
|
9+
LL - return *x
10+
LL + return x.clone()
11+
|
612

713
error: aborting due to 1 previous error
814

tests/ui/borrowck/borrowck-in-static.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | Box::new(|| x)
77
| -- ^ move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
88
| |
99
| captured by this `Fn` closure
10+
|
11+
help: consider cloning the value if the performance cost is acceptable
12+
|
13+
LL | Box::new(|| x.clone())
14+
| ++++++++
1015

1116
error: aborting due to 1 previous error
1217

tests/ui/borrowck/borrowck-move-out-of-overloaded-auto-deref.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
use std::rc::Rc;
33

44
pub fn main() {
5-
let _x = <Vec<i32> as Clone>::clone(&Rc::new(vec![1, 2])).into_iter();
5+
let _x = <Vec<i32> as Clone>::clone(&Rc::new(vec![1, 2]).clone()).into_iter();
66
//~^ ERROR [E0507]
77
}

tests/ui/borrowck/borrowck-move-out-of-overloaded-auto-deref.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ help: you can `clone` the value and consume it, but this might not be your desir
1212
|
1313
LL | let _x = <Vec<i32> as Clone>::clone(&Rc::new(vec![1, 2])).into_iter();
1414
| ++++++++++++++++++++++++++++ +
15+
help: consider cloning the value if the performance cost is acceptable
16+
|
17+
LL | let _x = Rc::new(vec![1, 2]).clone().into_iter();
18+
| ++++++++
1519

1620
error: aborting due to 1 previous error
1721

tests/ui/borrowck/borrowck-struct-update-with-dtor.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ LL | let _s2 = T{a: 2, ..s0};
1515
| |
1616
| cannot move out of here
1717
| move occurs because `s0.mv` has type `Box<isize>`, which does not implement the `Copy` trait
18+
|
19+
help: consider cloning the value if the performance cost is acceptable
20+
|
21+
LL | let _s2 = T{a: 2, ..s0}.clone();
22+
| ++++++++
1823

1924
error: aborting due to 2 previous errors
2025

tests/ui/borrowck/clone-span-on-try-operator.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ impl Foo {
77
}
88
fn main() {
99
let foo = &Foo;
10-
<Foo as Clone>::clone(&(*foo)).foo(); //~ ERROR cannot move out
10+
<Foo as Clone>::clone(&foo.clone()).foo(); //~ ERROR cannot move out
1111
}

tests/ui/borrowck/clone-span-on-try-operator.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ help: you can `clone` the value and consume it, but this might not be your desir
1515
|
1616
LL | <Foo as Clone>::clone(&(*foo)).foo();
1717
| +++++++++++++++++++++++ +
18+
help: consider cloning the value if the performance cost is acceptable
19+
|
20+
LL - (*foo).foo();
21+
LL + foo.clone().foo();
22+
|
1823

1924
error: aborting due to 1 previous error
2025

tests/ui/borrowck/issue-64453.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ error[E0507]: cannot move out of static item `settings_dir`
2222
|
2323
LL | let settings_data = from_string(settings_dir);
2424
| ^^^^^^^^^^^^ move occurs because `settings_dir` has type `String`, which does not implement the `Copy` trait
25+
|
26+
help: consider cloning the value if the performance cost is acceptable
27+
|
28+
LL | let settings_data = from_string(settings_dir.clone());
29+
| ++++++++
2530

2631
error: aborting due to 3 previous errors
2732

tests/ui/borrowck/move-error-in-promoted.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ LL | let _ = S1(C[0]).clone();
66
| |
77
| cannot move out of here
88
| move occurs because value has type `S2`, which does not implement the `Copy` trait
9+
|
10+
help: consider cloning the value if the performance cost is acceptable
11+
|
12+
LL | let _ = S1(C[0].clone()).clone();
13+
| ++++++++
914

1015
error: aborting due to 1 previous error
1116

0 commit comments

Comments
 (0)