Skip to content

Commit d687d46

Browse files
committed
Tweak output in for loops
Do not suggest `.clone()` as we already suggest borrowing the iterated value.
1 parent c0e4817 commit d687d46

File tree

5 files changed

+11
-19
lines changed

5 files changed

+11
-19
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
397397
&& let Some(node) = hir.find(hir.local_def_id_to_hir_id(def_id))
398398
&& let Some(fn_sig) = node.fn_sig()
399399
&& let Some(ident) = node.ident()
400-
&& let Some(pos) = args.iter()
401-
.position(|arg| arg.hir_id == expr.hir_id)
400+
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
402401
&& let Some(arg) = fn_sig.decl.inputs.get(pos + offset)
403402
{
404403
let mut span: MultiSpan = arg.span.into();
@@ -425,7 +424,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
425424
}
426425
let place = &self.move_data.move_paths[mpi].place;
427426
let ty = place.ty(self.body, self.infcx.tcx).ty;
428-
self.suggest_cloning(err, ty, move_span);
427+
if let hir::Node::Expr(parent_expr) = parent
428+
&& let hir::ExprKind::Call(call_expr, _) = parent_expr.kind
429+
&& let hir::ExprKind::Path(
430+
hir::QPath::LangItem(LangItem::IntoIterIntoIter, _, _)
431+
) = call_expr.kind
432+
{
433+
// Do not suggest `.clone()` in a `for` loop, we already suggest borrowing.
434+
} else {
435+
self.suggest_cloning(err, ty, move_span);
436+
}
429437
}
430438
}
431439
if let Some(pat) = finder.pat && !seen_spans.contains(&pat.span) {

src/test/ui/issues/issue-61108.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ note: this function takes ownership of the receiver `self`, which moves `bad_let
1414
|
1515
LL | fn into_iter(self) -> Self::IntoIter;
1616
| ^^^^
17-
help: consider cloning the value if the performance cost is acceptable
18-
|
19-
LL | for l in bad_letters.clone() {
20-
| ++++++++
2117
help: consider iterating over a slice of the `Vec<char>`'s content to avoid moving into the `for` loop
2218
|
2319
LL | for l in &bad_letters {

src/test/ui/issues/issue-64559.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ note: this function takes ownership of the receiver `self`, which moves `orig`
1515
|
1616
LL | fn into_iter(self) -> Self::IntoIter;
1717
| ^^^^
18-
help: consider cloning the value if the performance cost is acceptable
19-
|
20-
LL | for _val in orig.clone() {}
21-
| ++++++++
2218
help: consider iterating over a slice of the `Vec<bool>`'s content to avoid moving into the `for` loop
2319
|
2420
LL | for _val in &orig {}

src/test/ui/moves/move-fn-self-receiver.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ LL | for _val in implicit_into_iter {}
127127
LL | implicit_into_iter;
128128
| ^^^^^^^^^^^^^^^^^^ value used here after move
129129
|
130-
help: consider cloning the value if the performance cost is acceptable
131-
|
132-
LL | for _val in implicit_into_iter.clone() {}
133-
| ++++++++
134130
help: consider iterating over a slice of the `Vec<bool>`'s content to avoid moving into the `for` loop
135131
|
136132
LL | for _val in &implicit_into_iter {}

src/test/ui/suggestions/borrow-for-loop-head.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ note: this function takes ownership of the receiver `self`, which moves `a`
2121
|
2222
LL | fn into_iter(self) -> Self::IntoIter;
2323
| ^^^^
24-
help: consider cloning the value if the performance cost is acceptable
25-
|
26-
LL | for j in a.clone() {
27-
| ++++++++
2824
help: consider iterating over a slice of the `Vec<i32>`'s content to avoid moving into the `for` loop
2925
|
3026
LL | for j in &a {

0 commit comments

Comments
 (0)