|
1 | 1 | use super::UNUSED_ENUMERATE_INDEX;
|
2 | 2 | use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
|
3 | 3 | use clippy_utils::source::snippet;
|
4 |
| -use clippy_utils::{pat_is_wild, sugg}; |
| 4 | +use clippy_utils::{match_def_path, pat_is_wild, sugg}; |
5 | 5 | use rustc_hir::def::DefKind;
|
6 | 6 | use rustc_hir::{Expr, ExprKind, Pat, PatKind};
|
7 | 7 | use rustc_lint::LateContext;
|
8 | 8 | use rustc_middle::ty;
|
9 | 9 |
|
10 | 10 | /// 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 | + && match_def_path(cx, base.did(), &clippy_utils::paths::CORE_ITER_ENUMERATE_STRUCT) |
| 18 | + && let Some((DefKind::AssocFn, call_id)) = cx.typeck_results().type_dependent_def(arg.hir_id) |
| 19 | + && match_def_path(cx, call_id, &clippy_utils::paths::CORE_ITER_ENUMERATE_METHOD) |
| 20 | + { |
| 21 | + span_lint_and_then( |
| 22 | + cx, |
| 23 | + UNUSED_ENUMERATE_INDEX, |
| 24 | + arg.span, |
| 25 | + "you seem to use `.enumerate()` and immediately discard the index", |
| 26 | + |diag| { |
| 27 | + let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter"); |
| 28 | + multispan_sugg( |
| 29 | + diag, |
| 30 | + "remove the `.enumerate()` call", |
| 31 | + vec![ |
| 32 | + (pat.span, snippet(cx, elem.span, "..").into_owned()), |
| 33 | + (arg.span, base_iter.to_string()), |
| 34 | + ], |
| 35 | + ); |
| 36 | + }, |
| 37 | + ); |
33 | 38 | }
|
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 |
| - ); |
62 | 39 | }
|
0 commit comments