diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 8f9323056bc..c2b44ee7018 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -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, ); } } diff --git a/tests/ui/explicit_counter_loop.stderr b/tests/ui/explicit_counter_loop.stderr index 8982c28eb72..5efd51abf18 100644 --- a/tests/ui/explicit_counter_loop.stderr +++ b/tests/ui/explicit_counter_loop.stderr @@ -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