Skip to content

Commit 27a787f

Browse files
committed
Silence redundant clone suggestion
1 parent 92533ab commit 27a787f

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
@@ -1017,6 +1017,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10171017
span: Span,
10181018
) {
10191019
let tcx = self.infcx.tcx;
1020+
if let Some(_) = self.clone_on_reference(expr) {
1021+
// Avoid redundant clone suggestion already suggested in `explain_captures`.
1022+
// See `tests/ui/moves/needs-clone-through-deref.rs`
1023+
return;
1024+
}
10201025
// Try to find predicates on *generic params* that would allow copying `ty`
10211026
let suggestion =
10221027
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)