Skip to content

Commit c1533d5

Browse files
committed
Silence redundant clone suggestion
1 parent 91bbbf1 commit c1533d5

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
@@ -787,6 +787,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
787787
span: Span,
788788
) {
789789
let tcx = self.infcx.tcx;
790+
if let Some(_) = self.clone_on_reference(expr) {
791+
// Avoid redundant clone suggestion already suggested in `explain_captures`.
792+
// See `tests/ui/moves/needs-clone-through-deref.rs`
793+
return;
794+
}
790795
// Try to find predicates on *generic params* that would allow copying `ty`
791796
let suggestion =
792797
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)