Skip to content

Commit e183401

Browse files
committed
[unused_enumerate_index]: Use if-let chain
1 parent 93f0a9a commit e183401

File tree

1 file changed

+29
-50
lines changed

1 file changed

+29
-50
lines changed

clippy_lints/src/loops/unused_enumerate_index.rs

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,34 @@ use rustc_lint::LateContext;
88
use rustc_middle::ty;
99

1010
/// Checks for the `UNUSED_ENUMERATE_INDEX` lint.
11-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx Expr<'_>, body: &'tcx Expr<'_>) {
12-
let PatKind::Tuple([index, elem], _) = pat.kind else {
13-
return;
14-
};
15-
16-
let ExprKind::MethodCall(_method, self_arg, [], _) = arg.kind else {
17-
return;
18-
};
19-
20-
let ty = cx.typeck_results().expr_ty(arg);
21-
22-
if !pat_is_wild(cx, &index.kind, body) {
23-
return;
24-
}
25-
26-
let name = match *ty.kind() {
27-
ty::Adt(base, _substs) => cx.tcx.def_path_str(base.did()),
28-
_ => return,
29-
};
30-
31-
if name != "std::iter::Enumerate" && name != "core::iter::Enumerate" {
32-
return;
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>, arg: &Expr<'_>, body: &'tcx Expr<'tcx>) {
12+
if let PatKind::Tuple([index, elem], _) = pat.kind
13+
&& let ExprKind::MethodCall(_method, self_arg, [], _) = arg.kind
14+
&& let ty = cx.typeck_results().expr_ty(arg)
15+
&& pat_is_wild(cx, &index.kind, body)
16+
&& let ty::Adt(base, _) = *ty.kind()
17+
&& let name = cx.tcx.def_path_str(base.did())
18+
&& (name == "std::iter::Enumerate" || name == "core::iter::Enumerate")
19+
&& let Some((DefKind::AssocFn, call_id)) = cx.typeck_results().type_dependent_def(arg.hir_id)
20+
&& let call_name = cx.tcx.def_path_str(call_id)
21+
&& (call_name == "std::iter::Iterator::enumerate" || call_name == "core::iter::Iterator::enumerate")
22+
{
23+
span_lint_and_then(
24+
cx,
25+
UNUSED_ENUMERATE_INDEX,
26+
arg.span,
27+
"you seem to use `.enumerate()` and immediately discard the index",
28+
|diag| {
29+
let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter");
30+
multispan_sugg(
31+
diag,
32+
"remove the `.enumerate()` call",
33+
vec![
34+
(pat.span, snippet(cx, elem.span, "..").into_owned()),
35+
(arg.span, base_iter.to_string()),
36+
],
37+
);
38+
},
39+
);
3340
}
34-
35-
let Some((DefKind::AssocFn, call_id)) = cx.typeck_results().type_dependent_def(arg.hir_id) else {
36-
return;
37-
};
38-
39-
let call_name = cx.tcx.def_path_str(call_id);
40-
41-
if call_name != "std::iter::Iterator::enumerate" && call_name != "core::iter::Iterator::enumerate" {
42-
return;
43-
}
44-
45-
span_lint_and_then(
46-
cx,
47-
UNUSED_ENUMERATE_INDEX,
48-
arg.span,
49-
"you seem to use `.enumerate()` and immediately discard the index",
50-
|diag| {
51-
let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter");
52-
multispan_sugg(
53-
diag,
54-
"remove the `.enumerate()` call",
55-
vec![
56-
(pat.span, snippet(cx, elem.span, "..").into_owned()),
57-
(arg.span, base_iter.to_string()),
58-
],
59-
);
60-
},
61-
);
6241
}

0 commit comments

Comments
 (0)