From 9698e41994fad31474a20e2dc5f8664364404e1b Mon Sep 17 00:00:00 2001 From: rail <12975677+rail-rain@users.noreply.github.com> Date: Sat, 23 Mar 2019 15:36:48 +0900 Subject: [PATCH] Change explicit_counter_loop's message to add parentheses if necessary --- clippy_lints/src/loops.rs | 6 +++++- tests/ui/explicit_counter_loop.rs | 9 +++++++++ tests/ui/explicit_counter_loop.stderr | 12 +++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index d88f3e678..8f9323056 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -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()) + } ), ); } diff --git a/tests/ui/explicit_counter_loop.rs b/tests/ui/explicit_counter_loop.rs index 5efac85cf..71ef1e867 100644 --- a/tests/ui/explicit_counter_loop.rs +++ b/tests/ui/explicit_counter_loop.rs @@ -113,3 +113,12 @@ mod issue_3308 { } } } + +mod issue_1670 { + pub fn test() { + let mut count = 0; + for _i in 3..10 { + count += 1; + } + } +} diff --git a/tests/ui/explicit_counter_loop.stderr b/tests/ui/explicit_counter_loop.stderr index 84ca0db83..8982c28eb 100644 --- a/tests/ui/explicit_counter_loop.stderr +++ b/tests/ui/explicit_counter_loop.stderr @@ -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