Skip to content

Commit 2b82c71

Browse files
committed
use span_lint_and_sugg in explicit_counter_loop
1 parent 9698e41 commit 2b82c71

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

clippy_lints/src/loops.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1490,21 +1490,31 @@ fn check_for_loop_explicit_counter<'a, 'tcx>(
14901490

14911491
if visitor2.state == VarState::Warn {
14921492
if let Some(name) = visitor2.name {
1493-
span_lint(
1493+
let mut applicability = Applicability::MachineApplicable;
1494+
span_lint_and_sugg(
14941495
cx,
14951496
EXPLICIT_COUNTER_LOOP,
14961497
expr.span,
1497-
&format!(
1498-
"the variable `{0}` is used as a loop counter. Consider using `for ({0}, \
1499-
{1}) in {2}.enumerate()` or similar iterators",
1498+
&format!("the variable `{}` is used as a loop counter.", name),
1499+
"consider using",
1500+
format!(
1501+
"for ({}, {}) in {}.enumerate()",
15001502
name,
1501-
snippet(cx, pat.span, "_"),
1503+
snippet_with_applicability(cx, pat.span, "item", &mut applicability),
15021504
if higher::range(cx, arg).is_some() {
1503-
format!("({})", snippet(cx, arg.span, "_"))
1505+
format!(
1506+
"({})",
1507+
snippet_with_applicability(cx, arg.span, "_", &mut applicability)
1508+
)
15041509
} else {
1505-
format!("{}", sugg::Sugg::hir(cx, arg, "_").maybe_par())
1510+
format!(
1511+
"{}",
1512+
sugg::Sugg::hir_with_applicability(cx, arg, "_", &mut applicability)
1513+
.maybe_par()
1514+
)
15061515
}
15071516
),
1517+
applicability,
15081518
);
15091519
}
15101520
}

tests/ui/explicit_counter_loop.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
error: the variable `_index` is used as a loop counter. Consider using `for (_index, _v) in (&vec).enumerate()` or similar iterators
1+
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-
| ^^^^
5+
| ^^^^ help: consider using: `for (_index, _v) in (&vec).enumerate()`
66
|
77
= note: `-D clippy::explicit-counter-loop` implied by `-D warnings`
88

9-
error: the variable `_index` is used as a loop counter. Consider using `for (_index, _v) in (&vec).enumerate()` or similar iterators
9+
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-
| ^^^^
13+
| ^^^^ help: consider using: `for (_index, _v) in (&vec).enumerate()`
1414

15-
error: the variable `count` is used as a loop counter. Consider using `for (count, ch) in text.chars().enumerate()` or similar iterators
15+
error: the variable `count` is used as a loop counter.
1616
--> $DIR/explicit_counter_loop.rs:51:19
1717
|
1818
LL | for ch in text.chars() {
19-
| ^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
2020

21-
error: the variable `count` is used as a loop counter. Consider using `for (count, ch) in text.chars().enumerate()` or similar iterators
21+
error: the variable `count` is used as a loop counter.
2222
--> $DIR/explicit_counter_loop.rs:62:19
2323
|
2424
LL | for ch in text.chars() {
25-
| ^^^^^^^^^^^^
25+
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
2626

27-
error: the variable `count` is used as a loop counter. Consider using `for (count, _i) in (3..10).enumerate()` or similar iterators
27+
error: the variable `count` is used as a loop counter.
2828
--> $DIR/explicit_counter_loop.rs:120:19
2929
|
3030
LL | for _i in 3..10 {
31-
| ^^^^^
31+
| ^^^^^ help: consider using: `for (count, _i) in (3..10).enumerate()`
3232

3333
error: aborting due to 5 previous errors
3434

0 commit comments

Comments
 (0)