Skip to content

Commit 92533ab

Browse files
committed
More move error suggestions to clone
``` error[E0507]: cannot move out of `val`, a captured variable in an `FnMut` closure --> $DIR/issue-87456-point-to-closure.rs:10:28 | LL | let val = String::new(); | --- captured outer variable LL | LL | take_mut(|| { | -- captured by this `FnMut` closure LL | LL | let _foo: String = val; | ^^^ move occurs because `val` has type `String`, which does not implement the `Copy` trait | help: consider borrowing here | LL | let _foo: String = &val; | + help: consider cloning the value if the performance cost is acceptable | LL | let _foo: String = val.clone(); | ++++++++ ```
1 parent a0ee8e6 commit 92533ab

12 files changed

+97
-5
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
444444
None => "value".to_string(),
445445
};
446446

447+
if let Some(expr) = self.find_expr(span) {
448+
self.suggest_cloning(err, place_ty, expr, span);
449+
}
450+
447451
err.subdiagnostic(
448452
self.dcx(),
449453
crate::session_diagnostics::TypeNoCopy::Label {

tests/ui/borrowck/borrowck-issue-2657-2.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55

66
match x {
77
Some(ref y) => {
8-
let _b = y; //~ ERROR cannot move out
8+
let _b = y.clone(); //~ ERROR cannot move out
99
}
1010
_ => {}
1111
}

tests/ui/borrowck/borrowck-issue-2657-2.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ help: consider removing the dereference here
99
LL - let _b = *y;
1010
LL + let _b = y;
1111
|
12+
help: consider cloning the value if the performance cost is acceptable
13+
|
14+
LL - let _b = *y;
15+
LL + let _b = y.clone();
16+
|
1217

1318
error: aborting due to 1 previous error
1419

tests/ui/borrowck/borrowck-move-from-unsafe-ptr.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ help: consider removing the dereference here
99
LL - let y = *x;
1010
LL + let y = x;
1111
|
12+
help: consider cloning the value if the performance cost is acceptable
13+
|
14+
LL - let y = *x;
15+
LL + let y = x.clone();
16+
|
1217

1318
error: aborting due to 1 previous error
1419

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ help: consider removing the dereference here
99
LL - let _x = *Rc::new("hi".to_string());
1010
LL + let _x = Rc::new("hi".to_string());
1111
|
12+
help: consider cloning the value if the performance cost is acceptable
13+
|
14+
LL - let _x = *Rc::new("hi".to_string());
15+
LL + let _x = Rc::new("hi".to_string()).clone();
16+
|
1217

1318
error: aborting due to 1 previous error
1419

tests/ui/borrowck/borrowck-overloaded-index-move-from-vec.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ help: consider borrowing here
88
|
99
LL | let bad = &v[0];
1010
| +
11+
help: consider cloning the value if the performance cost is acceptable
12+
|
13+
LL | let bad = v[0].clone();
14+
| ++++++++
1115

1216
error: aborting due to 1 previous error
1317

tests/ui/borrowck/borrowck-vec-pattern-nesting.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn c() {
4747
//~| NOTE cannot move out of here
4848
//~| NOTE move occurs because
4949
//~| HELP consider borrowing here
50+
//~| HELP consider cloning
5051
}
5152

5253
fn d() {
@@ -66,6 +67,7 @@ fn d() {
6667
//~| NOTE cannot move out of here
6768
//~| NOTE move occurs because
6869
//~| HELP consider borrowing here
70+
//~| HELP consider cloning
6971
}
7072

7173
fn e() {
@@ -86,6 +88,7 @@ fn e() {
8688
//~| NOTE cannot move out of here
8789
//~| NOTE move occurs because
8890
//~| HELP consider borrowing here
91+
//~| HELP consider cloning
8992
}
9093

9194
fn main() {}

tests/ui/borrowck/borrowck-vec-pattern-nesting.stderr

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ help: consider borrowing here
5353
|
5454
LL | let a = &vec[0];
5555
| +
56+
help: consider cloning the value if the performance cost is acceptable
57+
|
58+
LL | let a = vec[0].clone();
59+
| ++++++++
5660

5761
error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
58-
--> $DIR/borrowck-vec-pattern-nesting.rs:55:11
62+
--> $DIR/borrowck-vec-pattern-nesting.rs:56:11
5963
|
6064
LL | match vec {
6165
| ^^^ cannot move out of here
@@ -73,7 +77,7 @@ LL + [
7377
|
7478

7579
error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
76-
--> $DIR/borrowck-vec-pattern-nesting.rs:65:13
80+
--> $DIR/borrowck-vec-pattern-nesting.rs:66:13
7781
|
7882
LL | let a = vec[0];
7983
| ^^^^^^
@@ -85,9 +89,13 @@ help: consider borrowing here
8589
|
8690
LL | let a = &vec[0];
8791
| +
92+
help: consider cloning the value if the performance cost is acceptable
93+
|
94+
LL | let a = vec[0].clone();
95+
| ++++++++
8896

8997
error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
90-
--> $DIR/borrowck-vec-pattern-nesting.rs:74:11
98+
--> $DIR/borrowck-vec-pattern-nesting.rs:76:11
9199
|
92100
LL | match vec {
93101
| ^^^ cannot move out of here
@@ -106,7 +114,7 @@ LL + [_a, _b, _c] => {}
106114
|
107115

108116
error[E0508]: cannot move out of type `[Box<isize>]`, a non-copy slice
109-
--> $DIR/borrowck-vec-pattern-nesting.rs:85:13
117+
--> $DIR/borrowck-vec-pattern-nesting.rs:87:13
110118
|
111119
LL | let a = vec[0];
112120
| ^^^^^^
@@ -118,6 +126,10 @@ help: consider borrowing here
118126
|
119127
LL | let a = &vec[0];
120128
| +
129+
help: consider cloning the value if the performance cost is acceptable
130+
|
131+
LL | let a = vec[0].clone();
132+
| ++++++++
121133

122134
error: aborting due to 8 previous errors
123135

tests/ui/borrowck/issue-87456-point-to-closure.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ help: consider borrowing here
1414
|
1515
LL | let _foo: String = &val;
1616
| +
17+
help: consider cloning the value if the performance cost is acceptable
18+
|
19+
LL | let _foo: String = val.clone();
20+
| ++++++++
1721

1822
error: aborting due to 1 previous error
1923

tests/ui/check-static-values-constraints.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ help: consider borrowing here
160160
|
161161
LL | &x
162162
| +
163+
help: consider cloning the value if the performance cost is acceptable
164+
|
165+
LL | x.clone()
166+
| ++++++++
163167

164168
error: aborting due to 17 previous errors
165169

0 commit comments

Comments
 (0)