Skip to content

Commit 4578e5e

Browse files
committed
Fix suggestion span in explicit_counter_loop
1 parent 72f3439 commit 4578e5e

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

clippy_lints/src/loops.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,10 +1461,19 @@ fn check_for_loop_explicit_counter<'a, 'tcx>(
14611461
if visitor2.state == VarState::Warn {
14621462
if let Some(name) = visitor2.name {
14631463
let mut applicability = Applicability::MachineApplicable;
1464+
1465+
// for some reason this is the only way to get the `Span`
1466+
// of the entire `for` loop
1467+
let for_span = if let ExprKind::Match(_, arms, _) = &expr.kind {
1468+
arms[0].body.span
1469+
} else {
1470+
unreachable!()
1471+
};
1472+
14641473
span_lint_and_sugg(
14651474
cx,
14661475
EXPLICIT_COUNTER_LOOP,
1467-
expr.span,
1476+
for_span.with_hi(arg.span.hi()),
14681477
&format!("the variable `{}` is used as a loop counter.", name),
14691478
"consider using",
14701479
format!(

tests/ui/explicit_counter_loop.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
error: the variable `_index` is used as a loop counter.
2-
--> $DIR/explicit_counter_loop.rs:6:15
2+
--> $DIR/explicit_counter_loop.rs:6:5
33
|
44
LL | for _v in &vec {
5-
| ^^^^ help: consider using: `for (_index, _v) in vec.iter().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.
10-
--> $DIR/explicit_counter_loop.rs:12:15
10+
--> $DIR/explicit_counter_loop.rs:12:5
1111
|
1212
LL | for _v in &vec {
13-
| ^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()`
13+
| ^^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()`
1414

1515
error: the variable `_index` is used as a loop counter.
16-
--> $DIR/explicit_counter_loop.rs:17:15
16+
--> $DIR/explicit_counter_loop.rs:17:5
1717
|
1818
LL | for _v in &mut vec {
19-
| ^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter_mut().enumerate()`
19+
| ^^^^^^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter_mut().enumerate()`
2020

2121
error: the variable `_index` is used as a loop counter.
22-
--> $DIR/explicit_counter_loop.rs:22:15
22+
--> $DIR/explicit_counter_loop.rs:22:5
2323
|
2424
LL | for _v in vec {
25-
| ^^^ help: consider using: `for (_index, _v) in vec.into_iter().enumerate()`
25+
| ^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.into_iter().enumerate()`
2626

2727
error: the variable `count` is used as a loop counter.
28-
--> $DIR/explicit_counter_loop.rs:61:19
28+
--> $DIR/explicit_counter_loop.rs:61:9
2929
|
3030
LL | for ch in text.chars() {
31-
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
31+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
3232

3333
error: the variable `count` is used as a loop counter.
34-
--> $DIR/explicit_counter_loop.rs:72:19
34+
--> $DIR/explicit_counter_loop.rs:72:9
3535
|
3636
LL | for ch in text.chars() {
37-
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
37+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
3838

3939
error: the variable `count` is used as a loop counter.
40-
--> $DIR/explicit_counter_loop.rs:130:19
40+
--> $DIR/explicit_counter_loop.rs:130:9
4141
|
4242
LL | for _i in 3..10 {
43-
| ^^^^^ help: consider using: `for (count, _i) in 3..10.enumerate()`
43+
| ^^^^^^^^^^^^^^^ help: consider using: `for (count, _i) in 3..10.enumerate()`
4444

4545
error: aborting due to 7 previous errors
4646

0 commit comments

Comments
 (0)