declare span variable before call

This commit is contained in:
kyoto7250 2022-07-12 11:00:22 +09:00
parent 25fe4193ec
commit 134d0dee41

View File

@ -1,5 +1,5 @@
use super::{make_iterator_snippet, IncrementVisitor, InitializeVisitor, EXPLICIT_COUNTER_LOOP};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{get_enclosing_block, is_integer_const};
use if_chain::if_chain;
@ -35,28 +35,24 @@ pub(super) fn check<'tcx>(
if is_integer_const(cx, initializer, 0);
then {
let mut applicability = Applicability::MaybeIncorrect;
let span = expr.span.with_hi(arg.span.hi());
let int_name = match ty.map(Ty::kind) {
// usize or inferred
Some(ty::Uint(UintTy::Usize)) | None => {
span_lint_and_then(
span_lint_and_sugg(
cx,
EXPLICIT_COUNTER_LOOP,
expr.span.with_hi(arg.span.hi()),
span,
&format!("the variable `{}` is used as a loop counter", name),
|diag| {
diag.span_suggestion(
expr.span.with_hi(arg.span.hi()),
"consider using",
format!(
"for ({}, {}) in {}.enumerate()",
name,
snippet_with_applicability(cx, pat.span, "item", &mut applicability),
make_iterator_snippet(cx, arg, &mut applicability),
),
applicability,
);
}
"consider using",
format!(
"for ({}, {}) in {}.enumerate()",
name,
snippet_with_applicability(cx, pat.span, "item", &mut applicability),
make_iterator_snippet(cx, arg, &mut applicability),
),
applicability,
);
return;
}
@ -68,11 +64,11 @@ pub(super) fn check<'tcx>(
span_lint_and_then(
cx,
EXPLICIT_COUNTER_LOOP,
expr.span.with_hi(arg.span.hi()),
span,
&format!("the variable `{}` is used as a loop counter", name),
|diag| {
diag.span_suggestion(
expr.span.with_hi(arg.span.hi()),
span,
"consider using",
format!(
"for ({}, {}) in (0_{}..).zip({})",