use span_lint_and_sugg in explicit_counter_loop

This commit is contained in:
rail 2019-03-28 08:51:57 +09:00
parent 9698e41994
commit 2b82c71b55
2 changed files with 27 additions and 17 deletions

View File

@ -1490,21 +1490,31 @@ fn check_for_loop_explicit_counter<'a, 'tcx>(
if visitor2.state == VarState::Warn {
if let Some(name) = visitor2.name {
span_lint(
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
EXPLICIT_COUNTER_LOOP,
expr.span,
&format!(
"the variable `{0}` is used as a loop counter. Consider using `for ({0}, \
{1}) in {2}.enumerate()` or similar iterators",
&format!("the variable `{}` is used as a loop counter.", name),
"consider using",
format!(
"for ({}, {}) in {}.enumerate()",
name,
snippet(cx, pat.span, "_"),
snippet_with_applicability(cx, pat.span, "item", &mut applicability),
if higher::range(cx, arg).is_some() {
format!("({})", snippet(cx, arg.span, "_"))
format!(
"({})",
snippet_with_applicability(cx, arg.span, "_", &mut applicability)
)
} else {
format!("{}", sugg::Sugg::hir(cx, arg, "_").maybe_par())
format!(
"{}",
sugg::Sugg::hir_with_applicability(cx, arg, "_", &mut applicability)
.maybe_par()
)
}
),
applicability,
);
}
}

View File

@ -1,34 +1,34 @@
error: the variable `_index` is used as a loop counter. Consider using `for (_index, _v) in (&vec).enumerate()` or similar iterators
error: the variable `_index` is used as a loop counter.
--> $DIR/explicit_counter_loop.rs:6:15
|
LL | for _v in &vec {
| ^^^^
| ^^^^ help: consider using: `for (_index, _v) in (&vec).enumerate()`
|
= note: `-D clippy::explicit-counter-loop` implied by `-D warnings`
error: the variable `_index` is used as a loop counter. Consider using `for (_index, _v) in (&vec).enumerate()` or similar iterators
error: the variable `_index` is used as a loop counter.
--> $DIR/explicit_counter_loop.rs:12:15
|
LL | for _v in &vec {
| ^^^^
| ^^^^ help: consider using: `for (_index, _v) in (&vec).enumerate()`
error: the variable `count` is used as a loop counter. Consider using `for (count, ch) in text.chars().enumerate()` or similar iterators
error: the variable `count` is used as a loop counter.
--> $DIR/explicit_counter_loop.rs:51:19
|
LL | for ch in text.chars() {
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
error: the variable `count` is used as a loop counter. Consider using `for (count, ch) in text.chars().enumerate()` or similar iterators
error: the variable `count` is used as a loop counter.
--> $DIR/explicit_counter_loop.rs:62:19
|
LL | for ch in text.chars() {
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ help: consider using: `for (count, ch) in text.chars().enumerate()`
error: the variable `count` is used as a loop counter. Consider using `for (count, _i) in (3..10).enumerate()` or similar iterators
error: the variable `count` is used as a loop counter.
--> $DIR/explicit_counter_loop.rs:120:19
|
LL | for _i in 3..10 {
| ^^^^^
| ^^^^^ help: consider using: `for (count, _i) in (3..10).enumerate()`
error: aborting due to 5 previous errors