Skip to content

Commit 5a7caa3

Browse files
committed
Fix accuracy of T: Clone check in suggestion
1 parent 259348c commit 5a7caa3

17 files changed

+51
-72
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
988988
}
989989

990990
pub(crate) fn suggest_cloning(&self, err: &mut Diag<'_>, ty: Ty<'tcx>, expr: &hir::Expr<'_>) {
991+
let ty = ty.peel_refs();
991992
if let Some(clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
992993
&& self
993994
.infcx

tests/ui/associated-types/associated-types-outlives.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ LL | drop(x);
1010
| ^ move out of `x` occurs here
1111
LL | return f(y);
1212
| - borrow later used here
13-
|
14-
help: consider cloning the value if the performance cost is acceptable
15-
|
16-
LL - 's: loop { y = denormalise(&x); break }
17-
LL + 's: loop { y = denormalise(x.clone()); break }
18-
|
1913

2014
error: aborting due to 1 previous error
2115

tests/ui/binop/binop-move-semantics.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ LL | x
5151
...
5252
LL | use_mut(n); use_imm(m);
5353
| - borrow later used here
54-
|
55-
help: consider cloning the value if the performance cost is acceptable
56-
|
57-
LL - let m = &x;
58-
LL + let m = x.clone();
59-
|
6054

6155
error[E0505]: cannot move out of `y` because it is borrowed
6256
--> $DIR/binop-move-semantics.rs:23:5

tests/ui/borrowck/borrowck-issue-48962.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ LL | {src};
1717
| --- value moved here
1818
LL | src.0 = 66;
1919
| ^^^^^^^^^^ value used here after move
20+
|
21+
help: consider cloning the value if the performance cost is acceptable
22+
|
23+
LL | {src.clone()};
24+
| ++++++++
2025

2126
error: aborting due to 2 previous errors
2227

tests/ui/borrowck/borrowck-move-subcomponent.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ LL | let S { x: ax } = a;
99
| ^^ move out of `a.x` occurs here
1010
LL | f(pb);
1111
| -- borrow later used here
12-
|
13-
help: consider cloning the value if the performance cost is acceptable
14-
|
15-
LL - let pb = &a;
16-
LL + let pb = a.clone();
17-
|
1812

1913
error: aborting due to 1 previous error
2014

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ LL | println!("{}", f[s]);
1111
...
1212
LL | use_mut(rs);
1313
| -- borrow later used here
14+
|
15+
help: consider cloning the value if the performance cost is acceptable
16+
|
17+
LL - let rs = &mut s;
18+
LL + let rs = s.clone();
19+
|
1420

1521
error[E0505]: cannot move out of `s` because it is borrowed
1622
--> $DIR/borrowck-overloaded-index-move-index.rs:53:7
@@ -25,6 +31,12 @@ LL | f[s] = 10;
2531
...
2632
LL | use_mut(rs);
2733
| -- borrow later used here
34+
|
35+
help: consider cloning the value if the performance cost is acceptable
36+
|
37+
LL - let rs = &mut s;
38+
LL + let rs = s.clone();
39+
|
2840

2941
error[E0382]: use of moved value: `s`
3042
--> $DIR/borrowck-overloaded-index-move-index.rs:53:7

tests/ui/borrowck/clone-on-ref.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn foo<T: Default + Clone>(list: &mut Vec<T>) {
99
drop(cloned_items);
1010
}
1111
fn bar<T: std::fmt::Display + Clone>(x: T) {
12-
let a = x.clone();
12+
let a = &x;
1313
let b = a.clone();
1414
drop(x);
1515
//~^ ERROR cannot move out of `x` because it is borrowed
@@ -19,7 +19,7 @@ fn bar<T: std::fmt::Display + Clone>(x: T) {
1919
#[derive(Clone)]
2020
struct A;
2121
fn qux(x: A) {
22-
let a = x.clone();
22+
let a = &x;
2323
let b = a.clone();
2424
drop(x);
2525
//~^ ERROR cannot move out of `x` because it is borrowed

tests/ui/borrowck/clone-on-ref.stderr

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ help: consider further restricting this bound
3636
|
3737
LL | fn bar<T: std::fmt::Display + Clone>(x: T) {
3838
| +++++++
39-
help: consider cloning the value if the performance cost is acceptable
40-
|
41-
LL - let a = &x;
42-
LL + let a = x.clone();
43-
|
4439

4540
error[E0505]: cannot move out of `x` because it is borrowed
4641
--> $DIR/clone-on-ref.rs:23:10
@@ -62,11 +57,6 @@ help: consider annotating `A` with `#[derive(Clone)]`
6257
LL + #[derive(Clone)]
6358
LL | struct A;
6459
|
65-
help: consider cloning the value if the performance cost is acceptable
66-
|
67-
LL - let a = &x;
68-
LL + let a = x.clone();
69-
|
7060

7161
error: aborting due to 3 previous errors
7262

tests/ui/error-codes/E0504.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ LL | println!("child function: {}", fancy_num.num);
1313
...
1414
LL | println!("main function: {}", fancy_ref.num);
1515
| ------------- borrow later used here
16-
|
17-
help: consider cloning the value if the performance cost is acceptable
18-
|
19-
LL - let fancy_ref = &fancy_num;
20-
LL + let fancy_ref = fancy_num.clone();
21-
|
2216

2317
error: aborting due to 1 previous error
2418

tests/ui/error-codes/E0505.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ LL | eat(x);
1010
| ^ move out of `x` occurs here
1111
LL | _ref_to_val.use_ref();
1212
| ----------- borrow later used here
13-
|
14-
help: consider cloning the value if the performance cost is acceptable
15-
|
16-
LL - let _ref_to_val: &Value = &x;
17-
LL + let _ref_to_val: &Value = x.clone();
18-
|
1913

2014
error: aborting due to 1 previous error
2115

tests/ui/nll/closure-access-spans.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ LL | || *x = String::new();
109109
| ^^ -- borrow occurs due to use in closure
110110
| |
111111
| value borrowed here after move
112+
|
113+
help: consider cloning the value if the performance cost is acceptable
114+
|
115+
LL | let r = x.clone();
116+
| ++++++++
112117

113118
error[E0382]: use of moved value: `x`
114119
--> $DIR/closure-access-spans.rs:50:5
@@ -121,6 +126,11 @@ LL | || x;
121126
| ^^ - use occurs due to use in closure
122127
| |
123128
| value used here after move
129+
|
130+
help: consider cloning the value if the performance cost is acceptable
131+
|
132+
LL | let r = x.clone();
133+
| ++++++++
124134

125135
error: aborting due to 9 previous errors
126136

tests/ui/nll/issue-52059-report-when-borrow-and-drop-conflict.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ help: consider borrowing here
4141
|
4242
LL | let p = &s.url; p
4343
| +
44+
help: consider cloning the value if the performance cost is acceptable
45+
|
46+
LL | let p = s.url.clone(); p
47+
| ++++++++
4448

4549
error: aborting due to 4 previous errors
4650

tests/ui/nll/polonius/polonius-smoke-test.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ LL | let z = x;
2727
| ^ move out of `x` occurs here
2828
LL | y
2929
| - returning this value requires that `*x` is borrowed for `'1`
30+
|
31+
help: consider cloning the value if the performance cost is acceptable
32+
|
33+
LL - let y = &mut *x;
34+
LL + let y = x.clone();
35+
|
3036

3137
error[E0505]: cannot move out of `s` because it is borrowed
3238
--> $DIR/polonius-smoke-test.rs:42:5
@@ -40,6 +46,12 @@ LL | s;
4046
| ^ move out of `s` occurs here
4147
LL | tmp;
4248
| --- borrow later used here
49+
|
50+
help: consider cloning the value if the performance cost is acceptable
51+
|
52+
LL - let r = &mut *s;
53+
LL + let r = s.clone();
54+
|
4355

4456
error: aborting due to 4 previous errors
4557

tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ LL | let [ref _x0_hold, _x1, ref xs_hold @ ..] = arr;
1010
LL | _x1 = U;
1111
LL | drop(hold_all);
1212
| -------- borrow later used here
13-
|
14-
help: consider cloning the value if the performance cost is acceptable
15-
|
16-
LL - let hold_all = &arr;
17-
LL + let hold_all = arr.clone();
18-
|
1913

2014
error[E0384]: cannot assign twice to immutable variable `_x1`
2115
--> $DIR/borrowck-move-ref-pattern.rs:9:5

tests/ui/try-block/try-block-bad-lifetime.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ LL | Err(k) ?;
3434
...
3535
LL | ::std::mem::drop(k);
3636
| ^ value used here after move
37+
|
38+
help: consider cloning the value if the performance cost is acceptable
39+
|
40+
LL | Err(k.clone()) ?;
41+
| ++++++++
3742

3843
error[E0506]: cannot assign to `i` because it is borrowed
3944
--> $DIR/try-block-bad-lifetime.rs:32:9

tests/ui/unop-move-semantics.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ LL | !x;
3333
...
3434
LL | use_mut(n); use_imm(m);
3535
| - borrow later used here
36-
|
37-
help: consider cloning the value if the performance cost is acceptable
38-
|
39-
LL - let m = &x;
40-
LL + let m = x.clone();
41-
|
4236

4337
error[E0505]: cannot move out of `y` because it is borrowed
4438
--> $DIR/unop-move-semantics.rs:17:6

tests/ui/variance/variance-issue-20533.stderr

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ LL | drop(a);
99
| ^ move out of `a` occurs here
1010
LL | drop(x);
1111
| - borrow later used here
12-
|
13-
help: consider cloning the value if the performance cost is acceptable
14-
|
15-
LL - let x = foo(&a);
16-
LL + let x = foo(a.clone());
17-
|
1812

1913
error[E0505]: cannot move out of `a` because it is borrowed
2014
--> $DIR/variance-issue-20533.rs:34:14
@@ -27,12 +21,6 @@ LL | drop(a);
2721
| ^ move out of `a` occurs here
2822
LL | drop(x);
2923
| - borrow later used here
30-
|
31-
help: consider cloning the value if the performance cost is acceptable
32-
|
33-
LL - let x = bar(&a);
34-
LL + let x = bar(a.clone());
35-
|
3624

3725
error[E0505]: cannot move out of `a` because it is borrowed
3826
--> $DIR/variance-issue-20533.rs:40:14
@@ -45,12 +33,6 @@ LL | drop(a);
4533
| ^ move out of `a` occurs here
4634
LL | drop(x);
4735
| - borrow later used here
48-
|
49-
help: consider cloning the value if the performance cost is acceptable
50-
|
51-
LL - let x = baz(&a);
52-
LL + let x = baz(a.clone());
53-
|
5436

5537
error: aborting due to 3 previous errors
5638

0 commit comments

Comments
 (0)