Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 01b810e

Browse files
committed
Silence redundant clone suggestion
1 parent 065454d commit 01b810e

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10271027
span: Span,
10281028
) {
10291029
let tcx = self.infcx.tcx;
1030+
if let Some(_) = self.clone_on_reference(expr) {
1031+
// Avoid redundant clone suggestion already suggested in `explain_captures`.
1032+
// See `tests/ui/moves/needs-clone-through-deref.rs`
1033+
return;
1034+
}
10301035
// Try to find predicates on *generic params* that would allow copying `ty`
10311036
let suggestion =
10321037
if let Some(symbol) = tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ run-rustfix
2+
#![allow(dead_code, noop_method_call)]
3+
use std::ops::Deref;
4+
struct S(Vec<usize>);
5+
impl Deref for S {
6+
type Target = Vec<usize>;
7+
fn deref(&self) -> &Self::Target {
8+
&self.0
9+
}
10+
}
11+
12+
impl S {
13+
fn foo(&self) {
14+
// `self.clone()` returns `&S`, not `Vec`
15+
for _ in <Vec<usize> as Clone>::clone(&self).into_iter() {} //~ ERROR cannot move out of dereference of `S`
16+
}
17+
}
18+
fn main() {}

tests/ui/moves/needs-clone-through-deref.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ run-rustfix
12
#![allow(dead_code, noop_method_call)]
23
use std::ops::Deref;
34
struct S(Vec<usize>);

tests/ui/moves/needs-clone-through-deref.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0507]: cannot move out of dereference of `S`
2-
--> $DIR/needs-clone-through-deref.rs:14:18
2+
--> $DIR/needs-clone-through-deref.rs:15:18
33
|
44
LL | for _ in self.clone().into_iter() {}
55
| ^^^^^^^^^^^^ ----------- value moved due to this method call
@@ -12,10 +12,6 @@ help: you can `clone` the value and consume it, but this might not be your desir
1212
|
1313
LL | for _ in <Vec<usize> as Clone>::clone(&self).into_iter() {}
1414
| ++++++++++++++++++++++++++++++ ~
15-
help: consider cloning the value if the performance cost is acceptable
16-
|
17-
LL | for _ in self.clone().clone().into_iter() {}
18-
| ++++++++
1915

2016
error: aborting due to 1 previous error
2117

0 commit comments

Comments
 (0)