Skip to content

Commit 72f3439

Browse files
committed
Suggest calling iter if needed in explicit_counter_loop
1 parent 77092a5 commit 72f3439

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

clippy_lints/src/loops.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ use syntax_pos::{BytePos, Symbol};
2727

2828
use crate::utils::paths;
2929
use crate::utils::{
30-
get_enclosing_block, get_parent_expr, has_iter_method, higher, is_integer_const, is_refutable, last_path_segment,
31-
match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt, snippet_with_applicability,
32-
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, SpanlessEq,
30+
get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
31+
is_integer_const, is_refutable, last_path_segment, match_trait_method, match_type, match_var, multispan_sugg,
32+
snippet, snippet_opt, snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg,
33+
span_lint_and_then, SpanlessEq,
3334
};
3435

3536
declare_clippy_lint! {
@@ -1470,17 +1471,7 @@ fn check_for_loop_explicit_counter<'a, 'tcx>(
14701471
"for ({}, {}) in {}.enumerate()",
14711472
name,
14721473
snippet_with_applicability(cx, pat.span, "item", &mut applicability),
1473-
if higher::range(cx, arg).is_some() {
1474-
format!(
1475-
"({})",
1476-
snippet_with_applicability(cx, arg.span, "_", &mut applicability)
1477-
)
1478-
} else {
1479-
format!(
1480-
"{}",
1481-
sugg::Sugg::hir_with_applicability(cx, arg, "_", &mut applicability).maybe_par()
1482-
)
1483-
}
1474+
make_iterator_snippet(cx, arg, &mut applicability),
14841475
),
14851476
applicability,
14861477
);
@@ -1490,6 +1481,39 @@ fn check_for_loop_explicit_counter<'a, 'tcx>(
14901481
}
14911482
}
14921483

1484+
/// If `arg` was the argument to a `for` loop, return the "cleanest" way of writing the
1485+
/// actual `Iterator` that the loop uses.
1486+
fn make_iterator_snippet(cx: &LateContext<'_, '_>, arg: &Expr, applic_ref: &mut Applicability) -> String {
1487+
let impls_iterator = get_trait_def_id(cx, &paths::ITERATOR)
1488+
.map_or(false, |id| implements_trait(cx, cx.tables.expr_ty(arg), id, &[]));
1489+
if impls_iterator {
1490+
format!(
1491+
"{}",
1492+
sugg::Sugg::hir_with_applicability(cx, arg, "_", applic_ref).maybe_par()
1493+
)
1494+
} else {
1495+
// (&x).into_iter() ==> x.iter()
1496+
// (&mut x).into_iter() ==> x.iter_mut()
1497+
match &arg.kind {
1498+
ExprKind::AddrOf(mutability, arg_inner) if has_iter_method(cx, cx.tables.expr_ty(&arg_inner)).is_some() => {
1499+
let meth_name = match mutability {
1500+
MutMutable => "iter_mut",
1501+
MutImmutable => "iter",
1502+
};
1503+
format!(
1504+
"{}.{}()",
1505+
sugg::Sugg::hir_with_applicability(cx, &arg_inner, "_", applic_ref).maybe_par(),
1506+
meth_name,
1507+
)
1508+
},
1509+
_ => format!(
1510+
"{}.into_iter()",
1511+
sugg::Sugg::hir_with_applicability(cx, arg, "_", applic_ref).maybe_par()
1512+
),
1513+
}
1514+
}
1515+
}
1516+
14931517
/// Checks for the `FOR_KV_MAP` lint.
14941518
fn check_for_loop_over_map_kv<'a, 'tcx>(
14951519
cx: &LateContext<'a, 'tcx>,

tests/ui/explicit_counter_loop.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ fn main() {
1212
for _v in &vec {
1313
_index += 1
1414
}
15+
16+
let mut _index = 0;
17+
for _v in &mut vec {
18+
_index += 1;
19+
}
20+
21+
let mut _index = 0;
22+
for _v in vec {
23+
_index += 1;
24+
}
1525
}
1626

1727
mod issue_1219 {

tests/ui/explicit_counter_loop.stderr

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,45 @@ error: the variable `_index` is used as a loop counter.
22
--> $DIR/explicit_counter_loop.rs:6:15
33
|
44
LL | for _v in &vec {
5-
| ^^^^ help: consider using: `for (_index, _v) in (&vec).enumerate()`
5+
| ^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()`
66
|
77
= note: `-D clippy::explicit-counter-loop` implied by `-D warnings`
88

99
error: the variable `_index` is used as a loop counter.
1010
--> $DIR/explicit_counter_loop.rs:12:15
1111
|
1212
LL | for _v in &vec {
13-
| ^^^^ help: consider using: `for (_index, _v) in (&vec).enumerate()`
13+
| ^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()`
14+
15+
error: the variable `_index` is used as a loop counter.
16+
--> $DIR/explicit_counter_loop.rs:17:15
17+
|
18+
LL | for _v in &mut vec {
19+
| ^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter_mut().enumerate()`
20+
21+
error: the variable `_index` is used as a loop counter.
22+
--> $DIR/explicit_counter_loop.rs:22:15
23+
|
24+
LL | for _v in vec {
25+
| ^^^ help: consider using: `for (_index, _v) in vec.into_iter().enumerate()`
1426

1527
error: the variable `count` is used as a loop counter.
16-
--> $DIR/explicit_counter_loop.rs:51:19
28+
--> $DIR/explicit_counter_loop.rs:61:19
1729
|
1830
LL | for ch in text.chars() {
1931
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
2032

2133
error: the variable `count` is used as a loop counter.
22-
--> $DIR/explicit_counter_loop.rs:62:19
34+
--> $DIR/explicit_counter_loop.rs:72:19
2335
|
2436
LL | for ch in text.chars() {
2537
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
2638

2739
error: the variable `count` is used as a loop counter.
28-
--> $DIR/explicit_counter_loop.rs:120:19
40+
--> $DIR/explicit_counter_loop.rs:130:19
2941
|
3042
LL | for _i in 3..10 {
31-
| ^^^^^ help: consider using: `for (count, _i) in (3..10).enumerate()`
43+
| ^^^^^ help: consider using: `for (count, _i) in 3..10.enumerate()`
3244

33-
error: aborting due to 5 previous errors
45+
error: aborting due to 7 previous errors
3446

0 commit comments

Comments
 (0)