Change explicit_counter_loop's message to add parentheses if necessary

This commit is contained in:
rail 2019-03-23 15:36:48 +09:00
parent bd6c2df066
commit 9698e41994
3 changed files with 23 additions and 4 deletions

View file

@ -1499,7 +1499,11 @@ fn check_for_loop_explicit_counter<'a, 'tcx>(
{1}) in {2}.enumerate()` or similar iterators",
name,
snippet(cx, pat.span, "_"),
snippet(cx, arg.span, "_")
if higher::range(cx, arg).is_some() {
format!("({})", snippet(cx, arg.span, "_"))
} else {
format!("{}", sugg::Sugg::hir(cx, arg, "_").maybe_par())
}
),
);
}

View file

@ -113,3 +113,12 @@ mod issue_3308 {
}
}
}
mod issue_1670 {
pub fn test() {
let mut count = 0;
for _i in 3..10 {
count += 1;
}
}
}

View file

@ -1,4 +1,4 @@
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. Consider using `for (_index, _v) in (&vec).enumerate()` or similar iterators
--> $DIR/explicit_counter_loop.rs:6:15
|
LL | for _v in &vec {
@ -6,7 +6,7 @@ LL | for _v in &vec {
|
= 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. Consider using `for (_index, _v) in (&vec).enumerate()` or similar iterators
--> $DIR/explicit_counter_loop.rs:12:15
|
LL | for _v in &vec {
@ -24,5 +24,11 @@ error: the variable `count` is used as a loop counter. Consider using `for (coun
LL | for ch in text.chars() {
| ^^^^^^^^^^^^
error: aborting due to 4 previous errors
error: the variable `count` is used as a loop counter. Consider using `for (count, _i) in (3..10).enumerate()` or similar iterators
--> $DIR/explicit_counter_loop.rs:120:19
|
LL | for _i in 3..10 {
| ^^^^^
error: aborting due to 5 previous errors