mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-30 14:01:51 +00:00
Silence redundant clone suggestion
This commit is contained in:
parent
065454dd1d
commit
01b810e052
@ -1027,6 +1027,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||
span: Span,
|
||||
) {
|
||||
let tcx = self.infcx.tcx;
|
||||
if let Some(_) = self.clone_on_reference(expr) {
|
||||
// Avoid redundant clone suggestion already suggested in `explain_captures`.
|
||||
// See `tests/ui/moves/needs-clone-through-deref.rs`
|
||||
return;
|
||||
}
|
||||
// Try to find predicates on *generic params* that would allow copying `ty`
|
||||
let suggestion =
|
||||
if let Some(symbol) = tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) {
|
||||
|
18
tests/ui/moves/needs-clone-through-deref.fixed
Normal file
18
tests/ui/moves/needs-clone-through-deref.fixed
Normal file
@ -0,0 +1,18 @@
|
||||
//@ run-rustfix
|
||||
#![allow(dead_code, noop_method_call)]
|
||||
use std::ops::Deref;
|
||||
struct S(Vec<usize>);
|
||||
impl Deref for S {
|
||||
type Target = Vec<usize>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl S {
|
||||
fn foo(&self) {
|
||||
// `self.clone()` returns `&S`, not `Vec`
|
||||
for _ in <Vec<usize> as Clone>::clone(&self).into_iter() {} //~ ERROR cannot move out of dereference of `S`
|
||||
}
|
||||
}
|
||||
fn main() {}
|
@ -1,3 +1,4 @@
|
||||
//@ run-rustfix
|
||||
#![allow(dead_code, noop_method_call)]
|
||||
use std::ops::Deref;
|
||||
struct S(Vec<usize>);
|
||||
|
@ -1,5 +1,5 @@
|
||||
error[E0507]: cannot move out of dereference of `S`
|
||||
--> $DIR/needs-clone-through-deref.rs:14:18
|
||||
--> $DIR/needs-clone-through-deref.rs:15:18
|
||||
|
|
||||
LL | for _ in self.clone().into_iter() {}
|
||||
| ^^^^^^^^^^^^ ----------- 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
|
||||
|
|
||||
LL | for _ in <Vec<usize> as Clone>::clone(&self).into_iter() {}
|
||||
| ++++++++++++++++++++++++++++++ ~
|
||||
help: consider cloning the value if the performance cost is acceptable
|
||||
|
|
||||
LL | for _ in self.clone().clone().into_iter() {}
|
||||
| ++++++++
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user