Skip to content

Commit 4918d4f

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

File tree

5 files changed

+12
-19
lines changed

5 files changed

+12
-19
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::{
77
};
88
use rustc_hir as hir;
99
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
10+
// use rustc_hir::{AsyncGeneratorKind, GeneratorKind, LoopSource::ForLoop};
1011
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
1112
use rustc_infer::infer::TyCtxtInferExt;
1213
use rustc_infer::traits::ObligationCause;
@@ -397,8 +398,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
397398
&& let Some(node) = hir.find(hir.local_def_id_to_hir_id(def_id))
398399
&& let Some(fn_sig) = node.fn_sig()
399400
&& let Some(ident) = node.ident()
400-
&& let Some(pos) = args.iter()
401-
.position(|arg| arg.hir_id == expr.hir_id)
401+
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
402402
&& let Some(arg) = fn_sig.decl.inputs.get(pos + offset)
403403
{
404404
let mut span: MultiSpan = arg.span.into();
@@ -425,7 +425,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
425425
}
426426
let place = &self.move_data.move_paths[mpi].place;
427427
let ty = place.ty(self.body, self.infcx.tcx).ty;
428-
self.suggest_cloning(err, ty, move_span);
428+
if let hir::Node::Expr(parent_expr) = parent
429+
&& let hir::ExprKind::Call(call_expr, _) = parent_expr.kind
430+
&& let hir::ExprKind::Path(
431+
hir::QPath::LangItem(hir::LangItem::IntoIterIntoIter, _, _)
432+
) = call_expr.kind
433+
{
434+
// Do not suggest `.clone()` in a `for` loop, we already suggest borrowing.
435+
} else {
436+
self.suggest_cloning(err, ty, move_span);
437+
}
429438
}
430439
}
431440
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)