|
1 | 1 | use super::UNUSED_ENUMERATE_INDEX; |
2 | | -use clippy_utils::diagnostics::span_lint_and_then; |
3 | | -use clippy_utils::res::MaybeDef; |
4 | | -use clippy_utils::source::snippet; |
5 | | -use clippy_utils::{pat_is_wild, sugg}; |
| 2 | +use clippy_utils::diagnostics::span_lint_hir_and_then; |
| 3 | +use clippy_utils::res::{MaybeDef, MaybeTypeckRes}; |
| 4 | +use clippy_utils::source::{SpanRangeExt, walk_span_to_context}; |
| 5 | +use clippy_utils::{expr_or_init, pat_is_wild}; |
6 | 6 | use rustc_errors::Applicability; |
7 | | -use rustc_hir::def::DefKind; |
8 | | -use rustc_hir::{Expr, ExprKind, Pat, PatKind}; |
| 7 | +use rustc_hir::{Expr, ExprKind, Pat, PatKind, TyKind}; |
9 | 8 | use rustc_lint::LateContext; |
10 | | -use rustc_span::sym; |
| 9 | +use rustc_span::{Span, SyntaxContext, sym}; |
11 | 10 |
|
12 | | -/// Checks for the `UNUSED_ENUMERATE_INDEX` lint. |
13 | | -/// |
14 | | -/// The lint is also partially implemented in `clippy_lints/src/methods/unused_enumerate_index.rs`. |
15 | | -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>, arg: &Expr<'_>, body: &'tcx Expr<'tcx>) { |
16 | | - if let PatKind::Tuple([index, elem], _) = pat.kind |
17 | | - && let ExprKind::MethodCall(_method, self_arg, [], _) = arg.kind |
18 | | - && let ty = cx.typeck_results().expr_ty(arg) |
19 | | - && pat_is_wild(cx, &index.kind, body) |
20 | | - && ty.is_diag_item(cx, sym::Enumerate) |
21 | | - && let Some((DefKind::AssocFn, call_id)) = cx.typeck_results().type_dependent_def(arg.hir_id) |
22 | | - && cx.tcx.is_diagnostic_item(sym::enumerate_method, call_id) |
| 11 | +pub(super) fn check<'tcx>( |
| 12 | + cx: &LateContext<'tcx>, |
| 13 | + iter_expr: &'tcx Expr<'tcx>, |
| 14 | + pat: &Pat<'tcx>, |
| 15 | + ty_spans: Option<(Span, Span)>, |
| 16 | + body: &'tcx Expr<'tcx>, |
| 17 | +) { |
| 18 | + if let PatKind::Tuple([idx_pat, inner_pat], _) = pat.kind |
| 19 | + && cx.typeck_results().expr_ty(iter_expr).is_diag_item(cx, sym::Enumerate) |
| 20 | + && pat_is_wild(cx, &idx_pat.kind, body) |
| 21 | + && let enumerate_call = expr_or_init(cx, iter_expr) |
| 22 | + && let ExprKind::MethodCall(_, _, [], enumerate_span) = enumerate_call.kind |
| 23 | + && let Some(enumerate_id) = cx.typeck_results().type_dependent_def_id(enumerate_call.hir_id) |
| 24 | + && cx.tcx.is_diagnostic_item(sym::enumerate_method, enumerate_id) |
| 25 | + && !enumerate_call.span.from_expansion() |
| 26 | + && !pat.span.from_expansion() |
| 27 | + && !idx_pat.span.from_expansion() |
| 28 | + && !inner_pat.span.from_expansion() |
| 29 | + && let Some(enumerate_range) = enumerate_span.map_range(cx, |_, text, range| { |
| 30 | + text.get(..range.start)? |
| 31 | + .ends_with('.') |
| 32 | + .then_some(range.start - 1..range.end) |
| 33 | + }) |
23 | 34 | { |
24 | | - span_lint_and_then( |
| 35 | + let enumerate_span = Span::new(enumerate_range.start, enumerate_range.end, SyntaxContext::root(), None); |
| 36 | + span_lint_hir_and_then( |
25 | 37 | cx, |
26 | 38 | UNUSED_ENUMERATE_INDEX, |
27 | | - arg.span, |
| 39 | + enumerate_call.hir_id, |
| 40 | + enumerate_span, |
28 | 41 | "you seem to use `.enumerate()` and immediately discard the index", |
29 | 42 | |diag| { |
30 | | - let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter"); |
| 43 | + let mut spans = Vec::with_capacity(5); |
| 44 | + spans.push((enumerate_span, String::new())); |
| 45 | + spans.push((pat.span.with_hi(inner_pat.span.lo()), String::new())); |
| 46 | + spans.push((pat.span.with_lo(inner_pat.span.hi()), String::new())); |
| 47 | + if let Some((outer, inner)) = ty_spans { |
| 48 | + spans.push((outer.with_hi(inner.lo()), String::new())); |
| 49 | + spans.push((outer.with_lo(inner.hi()), String::new())); |
| 50 | + } |
31 | 51 | diag.multipart_suggestion( |
32 | 52 | "remove the `.enumerate()` call", |
33 | | - vec![ |
34 | | - (pat.span, snippet(cx, elem.span, "..").into_owned()), |
35 | | - (arg.span, base_iter.to_string()), |
36 | | - ], |
| 53 | + spans, |
37 | 54 | Applicability::MachineApplicable, |
38 | 55 | ); |
39 | 56 | }, |
40 | 57 | ); |
41 | 58 | } |
42 | 59 | } |
| 60 | + |
| 61 | +pub(super) fn check_method<'tcx>( |
| 62 | + cx: &LateContext<'tcx>, |
| 63 | + e: &'tcx Expr<'tcx>, |
| 64 | + recv: &'tcx Expr<'tcx>, |
| 65 | + arg: &'tcx Expr<'tcx>, |
| 66 | +) { |
| 67 | + if let ExprKind::Closure(closure) = arg.kind |
| 68 | + && let body = cx.tcx.hir_body(closure.body) |
| 69 | + && let [param] = body.params |
| 70 | + && cx.ty_based_def(e).opt_parent(cx).is_diag_item(cx, sym::Iterator) |
| 71 | + && let [input] = closure.fn_decl.inputs |
| 72 | + && !arg.span.from_expansion() |
| 73 | + && !input.span.from_expansion() |
| 74 | + && !recv.span.from_expansion() |
| 75 | + && !param.span.from_expansion() |
| 76 | + { |
| 77 | + let ty_spans = if let TyKind::Tup([_, inner]) = input.kind { |
| 78 | + let Some(inner) = walk_span_to_context(inner.span, SyntaxContext::root()) else { |
| 79 | + return; |
| 80 | + }; |
| 81 | + Some((input.span, inner)) |
| 82 | + } else { |
| 83 | + None |
| 84 | + }; |
| 85 | + check(cx, recv, param.pat, ty_spans, body.value); |
| 86 | + } |
| 87 | +} |
0 commit comments