From 5a7caa3174f8174db817228d8c2a02aa4913095c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 13 Mar 2024 15:37:27 +0000 Subject: [PATCH] Fix accuracy of `T: Clone` check in suggestion --- .../src/diagnostics/conflict_errors.rs | 1 + .../associated-types-outlives.stderr | 6 ------ tests/ui/binop/binop-move-semantics.stderr | 6 ------ tests/ui/borrowck/borrowck-issue-48962.stderr | 5 +++++ .../borrowck/borrowck-move-subcomponent.stderr | 6 ------ ...borrowck-overloaded-index-move-index.stderr | 12 ++++++++++++ tests/ui/borrowck/clone-on-ref.fixed | 4 ++-- tests/ui/borrowck/clone-on-ref.stderr | 10 ---------- tests/ui/error-codes/E0504.stderr | 6 ------ tests/ui/error-codes/E0505.stderr | 6 ------ tests/ui/nll/closure-access-spans.stderr | 10 ++++++++++ ...report-when-borrow-and-drop-conflict.stderr | 4 ++++ .../ui/nll/polonius/polonius-smoke-test.stderr | 12 ++++++++++++ .../borrowck-move-ref-pattern.stderr | 6 ------ .../ui/try-block/try-block-bad-lifetime.stderr | 5 +++++ tests/ui/unop-move-semantics.stderr | 6 ------ tests/ui/variance/variance-issue-20533.stderr | 18 ------------------ 17 files changed, 51 insertions(+), 72 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index f978a8df66b..c385fb583bb 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -988,6 +988,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { } pub(crate) fn suggest_cloning(&self, err: &mut Diag<'_>, ty: Ty<'tcx>, expr: &hir::Expr<'_>) { + let ty = ty.peel_refs(); if let Some(clone_trait_def) = self.infcx.tcx.lang_items().clone_trait() && self .infcx diff --git a/tests/ui/associated-types/associated-types-outlives.stderr b/tests/ui/associated-types/associated-types-outlives.stderr index 4a496b5fa87..deeedd22266 100644 --- a/tests/ui/associated-types/associated-types-outlives.stderr +++ b/tests/ui/associated-types/associated-types-outlives.stderr @@ -10,12 +10,6 @@ LL | drop(x); | ^ move out of `x` occurs here LL | return f(y); | - borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - 's: loop { y = denormalise(&x); break } -LL + 's: loop { y = denormalise(x.clone()); break } - | error: aborting due to 1 previous error diff --git a/tests/ui/binop/binop-move-semantics.stderr b/tests/ui/binop/binop-move-semantics.stderr index 219bfeaae06..1dd8c9a87d4 100644 --- a/tests/ui/binop/binop-move-semantics.stderr +++ b/tests/ui/binop/binop-move-semantics.stderr @@ -51,12 +51,6 @@ LL | x ... LL | use_mut(n); use_imm(m); | - borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let m = &x; -LL + let m = x.clone(); - | error[E0505]: cannot move out of `y` because it is borrowed --> $DIR/binop-move-semantics.rs:23:5 diff --git a/tests/ui/borrowck/borrowck-issue-48962.stderr b/tests/ui/borrowck/borrowck-issue-48962.stderr index ee174f6736e..6e821a4c6b0 100644 --- a/tests/ui/borrowck/borrowck-issue-48962.stderr +++ b/tests/ui/borrowck/borrowck-issue-48962.stderr @@ -17,6 +17,11 @@ LL | {src}; | --- value moved here LL | src.0 = 66; | ^^^^^^^^^^ value used here after move + | +help: consider cloning the value if the performance cost is acceptable + | +LL | {src.clone()}; + | ++++++++ error: aborting due to 2 previous errors diff --git a/tests/ui/borrowck/borrowck-move-subcomponent.stderr b/tests/ui/borrowck/borrowck-move-subcomponent.stderr index 1e4d94920f4..8408d99156a 100644 --- a/tests/ui/borrowck/borrowck-move-subcomponent.stderr +++ b/tests/ui/borrowck/borrowck-move-subcomponent.stderr @@ -9,12 +9,6 @@ LL | let S { x: ax } = a; | ^^ move out of `a.x` occurs here LL | f(pb); | -- borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let pb = &a; -LL + let pb = a.clone(); - | error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr b/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr index 7f8cc74a715..3386d2074a0 100644 --- a/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr +++ b/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr @@ -11,6 +11,12 @@ LL | println!("{}", f[s]); ... LL | use_mut(rs); | -- borrow later used here + | +help: consider cloning the value if the performance cost is acceptable + | +LL - let rs = &mut s; +LL + let rs = s.clone(); + | error[E0505]: cannot move out of `s` because it is borrowed --> $DIR/borrowck-overloaded-index-move-index.rs:53:7 @@ -25,6 +31,12 @@ LL | f[s] = 10; ... LL | use_mut(rs); | -- borrow later used here + | +help: consider cloning the value if the performance cost is acceptable + | +LL - let rs = &mut s; +LL + let rs = s.clone(); + | error[E0382]: use of moved value: `s` --> $DIR/borrowck-overloaded-index-move-index.rs:53:7 diff --git a/tests/ui/borrowck/clone-on-ref.fixed b/tests/ui/borrowck/clone-on-ref.fixed index 539ecd0b32c..b6927ba590e 100644 --- a/tests/ui/borrowck/clone-on-ref.fixed +++ b/tests/ui/borrowck/clone-on-ref.fixed @@ -9,7 +9,7 @@ fn foo(list: &mut Vec) { drop(cloned_items); } fn bar(x: T) { - let a = x.clone(); + let a = &x; let b = a.clone(); drop(x); //~^ ERROR cannot move out of `x` because it is borrowed @@ -19,7 +19,7 @@ fn bar(x: T) { #[derive(Clone)] struct A; fn qux(x: A) { - let a = x.clone(); + let a = &x; let b = a.clone(); drop(x); //~^ ERROR cannot move out of `x` because it is borrowed diff --git a/tests/ui/borrowck/clone-on-ref.stderr b/tests/ui/borrowck/clone-on-ref.stderr index 5ee56edd7b5..ee4fcadf55a 100644 --- a/tests/ui/borrowck/clone-on-ref.stderr +++ b/tests/ui/borrowck/clone-on-ref.stderr @@ -36,11 +36,6 @@ help: consider further restricting this bound | LL | fn bar(x: T) { | +++++++ -help: consider cloning the value if the performance cost is acceptable - | -LL - let a = &x; -LL + let a = x.clone(); - | error[E0505]: cannot move out of `x` because it is borrowed --> $DIR/clone-on-ref.rs:23:10 @@ -62,11 +57,6 @@ help: consider annotating `A` with `#[derive(Clone)]` LL + #[derive(Clone)] LL | struct A; | -help: consider cloning the value if the performance cost is acceptable - | -LL - let a = &x; -LL + let a = x.clone(); - | error: aborting due to 3 previous errors diff --git a/tests/ui/error-codes/E0504.stderr b/tests/ui/error-codes/E0504.stderr index 47f1218d2fa..c8a48961cb3 100644 --- a/tests/ui/error-codes/E0504.stderr +++ b/tests/ui/error-codes/E0504.stderr @@ -13,12 +13,6 @@ LL | println!("child function: {}", fancy_num.num); ... LL | println!("main function: {}", fancy_ref.num); | ------------- borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let fancy_ref = &fancy_num; -LL + let fancy_ref = fancy_num.clone(); - | error: aborting due to 1 previous error diff --git a/tests/ui/error-codes/E0505.stderr b/tests/ui/error-codes/E0505.stderr index 7e99d5252bd..250680d2c1c 100644 --- a/tests/ui/error-codes/E0505.stderr +++ b/tests/ui/error-codes/E0505.stderr @@ -10,12 +10,6 @@ LL | eat(x); | ^ move out of `x` occurs here LL | _ref_to_val.use_ref(); | ----------- borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let _ref_to_val: &Value = &x; -LL + let _ref_to_val: &Value = x.clone(); - | error: aborting due to 1 previous error diff --git a/tests/ui/nll/closure-access-spans.stderr b/tests/ui/nll/closure-access-spans.stderr index 7dc8acf8499..f789e5e9f95 100644 --- a/tests/ui/nll/closure-access-spans.stderr +++ b/tests/ui/nll/closure-access-spans.stderr @@ -109,6 +109,11 @@ LL | || *x = String::new(); | ^^ -- borrow occurs due to use in closure | | | value borrowed here after move + | +help: consider cloning the value if the performance cost is acceptable + | +LL | let r = x.clone(); + | ++++++++ error[E0382]: use of moved value: `x` --> $DIR/closure-access-spans.rs:50:5 @@ -121,6 +126,11 @@ LL | || x; | ^^ - use occurs due to use in closure | | | value used here after move + | +help: consider cloning the value if the performance cost is acceptable + | +LL | let r = x.clone(); + | ++++++++ error: aborting due to 9 previous errors diff --git a/tests/ui/nll/issue-52059-report-when-borrow-and-drop-conflict.stderr b/tests/ui/nll/issue-52059-report-when-borrow-and-drop-conflict.stderr index 7f9cbc3c30a..f4e7869bf00 100644 --- a/tests/ui/nll/issue-52059-report-when-borrow-and-drop-conflict.stderr +++ b/tests/ui/nll/issue-52059-report-when-borrow-and-drop-conflict.stderr @@ -41,6 +41,10 @@ help: consider borrowing here | LL | let p = &s.url; p | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let p = s.url.clone(); p + | ++++++++ error: aborting due to 4 previous errors diff --git a/tests/ui/nll/polonius/polonius-smoke-test.stderr b/tests/ui/nll/polonius/polonius-smoke-test.stderr index 534813b2d9f..77722beab98 100644 --- a/tests/ui/nll/polonius/polonius-smoke-test.stderr +++ b/tests/ui/nll/polonius/polonius-smoke-test.stderr @@ -27,6 +27,12 @@ LL | let z = x; | ^ move out of `x` occurs here LL | y | - returning this value requires that `*x` is borrowed for `'1` + | +help: consider cloning the value if the performance cost is acceptable + | +LL - let y = &mut *x; +LL + let y = x.clone(); + | error[E0505]: cannot move out of `s` because it is borrowed --> $DIR/polonius-smoke-test.rs:42:5 @@ -40,6 +46,12 @@ LL | s; | ^ move out of `s` occurs here LL | tmp; | --- borrow later used here + | +help: consider cloning the value if the performance cost is acceptable + | +LL - let r = &mut *s; +LL + let r = s.clone(); + | error: aborting due to 4 previous errors diff --git a/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr b/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr index 124731d01f3..a033cc0655e 100644 --- a/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr +++ b/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr @@ -10,12 +10,6 @@ LL | let [ref _x0_hold, _x1, ref xs_hold @ ..] = arr; LL | _x1 = U; LL | drop(hold_all); | -------- borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let hold_all = &arr; -LL + let hold_all = arr.clone(); - | error[E0384]: cannot assign twice to immutable variable `_x1` --> $DIR/borrowck-move-ref-pattern.rs:9:5 diff --git a/tests/ui/try-block/try-block-bad-lifetime.stderr b/tests/ui/try-block/try-block-bad-lifetime.stderr index 28941cb0a9e..6f693295357 100644 --- a/tests/ui/try-block/try-block-bad-lifetime.stderr +++ b/tests/ui/try-block/try-block-bad-lifetime.stderr @@ -34,6 +34,11 @@ LL | Err(k) ?; ... LL | ::std::mem::drop(k); | ^ value used here after move + | +help: consider cloning the value if the performance cost is acceptable + | +LL | Err(k.clone()) ?; + | ++++++++ error[E0506]: cannot assign to `i` because it is borrowed --> $DIR/try-block-bad-lifetime.rs:32:9 diff --git a/tests/ui/unop-move-semantics.stderr b/tests/ui/unop-move-semantics.stderr index 3c887f25ae8..187dd66b2fe 100644 --- a/tests/ui/unop-move-semantics.stderr +++ b/tests/ui/unop-move-semantics.stderr @@ -33,12 +33,6 @@ LL | !x; ... LL | use_mut(n); use_imm(m); | - borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let m = &x; -LL + let m = x.clone(); - | error[E0505]: cannot move out of `y` because it is borrowed --> $DIR/unop-move-semantics.rs:17:6 diff --git a/tests/ui/variance/variance-issue-20533.stderr b/tests/ui/variance/variance-issue-20533.stderr index bee9b802135..258f67db5ce 100644 --- a/tests/ui/variance/variance-issue-20533.stderr +++ b/tests/ui/variance/variance-issue-20533.stderr @@ -9,12 +9,6 @@ LL | drop(a); | ^ move out of `a` occurs here LL | drop(x); | - borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let x = foo(&a); -LL + let x = foo(a.clone()); - | error[E0505]: cannot move out of `a` because it is borrowed --> $DIR/variance-issue-20533.rs:34:14 @@ -27,12 +21,6 @@ LL | drop(a); | ^ move out of `a` occurs here LL | drop(x); | - borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let x = bar(&a); -LL + let x = bar(a.clone()); - | error[E0505]: cannot move out of `a` because it is borrowed --> $DIR/variance-issue-20533.rs:40:14 @@ -45,12 +33,6 @@ LL | drop(a); | ^ move out of `a` occurs here LL | drop(x); | - borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let x = baz(&a); -LL + let x = baz(a.clone()); - | error: aborting due to 3 previous errors