Skip to content

Commit 5365fb2

Browse files
committed
Fix accuracy of T: Clone check in suggestion
1 parent 4e6269d commit 5365fb2

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
@@ -748,6 +748,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
748748
}
749749

750750
pub(crate) fn suggest_cloning(&self, err: &mut Diag<'_>, ty: Ty<'tcx>, expr: &hir::Expr<'_>) {
751+
let ty = ty.peel_refs();
751752
if let Some(clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
752753
&& self
753754
.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

0 commit comments

Comments
 (0)