mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +00:00
Fix suggestion span in explicit_counter_loop
This commit is contained in:
parent
72f3439346
commit
4578e5e15e
2 changed files with 24 additions and 15 deletions
|
@ -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!(
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue