Tweak output in for loops

Do not suggest `.clone()` as we already suggest borrowing the iterated
value.
This commit is contained in:
Esteban Küber 2022-11-03 13:51:44 -07:00
parent c0e481731b
commit d687d46f68
5 changed files with 11 additions and 19 deletions

View File

@ -397,8 +397,7 @@ fn visit_pat(&mut self, p: &'hir hir::Pat<'hir>) {
&& let Some(node) = hir.find(hir.local_def_id_to_hir_id(def_id))
&& let Some(fn_sig) = node.fn_sig()
&& let Some(ident) = node.ident()
&& let Some(pos) = args.iter()
.position(|arg| arg.hir_id == expr.hir_id)
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
&& let Some(arg) = fn_sig.decl.inputs.get(pos + offset)
{
let mut span: MultiSpan = arg.span.into();
@ -425,7 +424,16 @@ fn visit_pat(&mut self, p: &'hir hir::Pat<'hir>) {
}
let place = &self.move_data.move_paths[mpi].place;
let ty = place.ty(self.body, self.infcx.tcx).ty;
self.suggest_cloning(err, ty, move_span);
if let hir::Node::Expr(parent_expr) = parent
&& let hir::ExprKind::Call(call_expr, _) = parent_expr.kind
&& let hir::ExprKind::Path(
hir::QPath::LangItem(LangItem::IntoIterIntoIter, _, _)
) = call_expr.kind
{
// Do not suggest `.clone()` in a `for` loop, we already suggest borrowing.
} else {
self.suggest_cloning(err, ty, move_span);
}
}
}
if let Some(pat) = finder.pat && !seen_spans.contains(&pat.span) {

View File

@ -14,10 +14,6 @@ note: this function takes ownership of the receiver `self`, which moves `bad_let
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^
help: consider cloning the value if the performance cost is acceptable
|
LL | for l in bad_letters.clone() {
| ++++++++
help: consider iterating over a slice of the `Vec<char>`'s content to avoid moving into the `for` loop
|
LL | for l in &bad_letters {

View File

@ -15,10 +15,6 @@ note: this function takes ownership of the receiver `self`, which moves `orig`
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^
help: consider cloning the value if the performance cost is acceptable
|
LL | for _val in orig.clone() {}
| ++++++++
help: consider iterating over a slice of the `Vec<bool>`'s content to avoid moving into the `for` loop
|
LL | for _val in &orig {}

View File

@ -127,10 +127,6 @@ LL | for _val in implicit_into_iter {}
LL | implicit_into_iter;
| ^^^^^^^^^^^^^^^^^^ value used here after move
|
help: consider cloning the value if the performance cost is acceptable
|
LL | for _val in implicit_into_iter.clone() {}
| ++++++++
help: consider iterating over a slice of the `Vec<bool>`'s content to avoid moving into the `for` loop
|
LL | for _val in &implicit_into_iter {}

View File

@ -21,10 +21,6 @@ note: this function takes ownership of the receiver `self`, which moves `a`
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^
help: consider cloning the value if the performance cost is acceptable
|
LL | for j in a.clone() {
| ++++++++
help: consider iterating over a slice of the `Vec<i32>`'s content to avoid moving into the `for` loop
|
LL | for j in &a {