diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 81a8c313e9d..731dd92c82a 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1461,10 +1461,19 @@ fn check_for_loop_explicit_counter<'a, 'tcx>( if visitor2.state == VarState::Warn { if let Some(name) = visitor2.name { let mut applicability = Applicability::MachineApplicable; + + // for some reason this is the only way to get the `Span` + // of the entire `for` loop + let for_span = if let ExprKind::Match(_, arms, _) = &expr.kind { + arms[0].body.span + } else { + unreachable!() + }; + span_lint_and_sugg( cx, EXPLICIT_COUNTER_LOOP, - expr.span, + for_span.with_hi(arg.span.hi()), &format!("the variable `{}` is used as a loop counter.", name), "consider using", format!( diff --git a/tests/ui/explicit_counter_loop.stderr b/tests/ui/explicit_counter_loop.stderr index c5643a857b1..1853e0c054c 100644 --- a/tests/ui/explicit_counter_loop.stderr +++ b/tests/ui/explicit_counter_loop.stderr @@ -1,46 +1,46 @@ error: the variable `_index` is used as a loop counter. - --> $DIR/explicit_counter_loop.rs:6:15 + --> $DIR/explicit_counter_loop.rs:6:5 | LL | for _v in &vec { - | ^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()` + | ^^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()` | = note: `-D clippy::explicit-counter-loop` implied by `-D warnings` error: the variable `_index` is used as a loop counter. - --> $DIR/explicit_counter_loop.rs:12:15 + --> $DIR/explicit_counter_loop.rs:12:5 | LL | for _v in &vec { - | ^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()` + | ^^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter().enumerate()` error: the variable `_index` is used as a loop counter. - --> $DIR/explicit_counter_loop.rs:17:15 + --> $DIR/explicit_counter_loop.rs:17:5 | LL | for _v in &mut vec { - | ^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter_mut().enumerate()` + | ^^^^^^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.iter_mut().enumerate()` error: the variable `_index` is used as a loop counter. - --> $DIR/explicit_counter_loop.rs:22:15 + --> $DIR/explicit_counter_loop.rs:22:5 | LL | for _v in vec { - | ^^^ help: consider using: `for (_index, _v) in vec.into_iter().enumerate()` + | ^^^^^^^^^^^^^ help: consider using: `for (_index, _v) in vec.into_iter().enumerate()` error: the variable `count` is used as a loop counter. - --> $DIR/explicit_counter_loop.rs:61:19 + --> $DIR/explicit_counter_loop.rs:61:9 | LL | for ch in text.chars() { - | ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()` + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()` error: the variable `count` is used as a loop counter. - --> $DIR/explicit_counter_loop.rs:72:19 + --> $DIR/explicit_counter_loop.rs:72:9 | LL | for ch in text.chars() { - | ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()` + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()` error: the variable `count` is used as a loop counter. - --> $DIR/explicit_counter_loop.rs:130:19 + --> $DIR/explicit_counter_loop.rs:130:9 | LL | for _i in 3..10 { - | ^^^^^ help: consider using: `for (count, _i) in 3..10.enumerate()` + | ^^^^^^^^^^^^^^^ help: consider using: `for (count, _i) in 3..10.enumerate()` error: aborting due to 7 previous errors