]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/range_zip_with_len.rs
Auto merge of #105436 - nnethercote:inline-place_contents_drop_state_cannot_differ...
[rust.git] / src / tools / clippy / clippy_lints / src / methods / range_zip_with_len.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::source::snippet;
3 use clippy_utils::{higher, SpanlessEq};
4 use clippy_utils::{is_integer_const, is_trait_method};
5 use if_chain::if_chain;
6 use rustc_hir::{Expr, ExprKind, QPath};
7 use rustc_lint::LateContext;
8 use rustc_span::sym;
9
10 use super::RANGE_ZIP_WITH_LEN;
11
12 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, zip_arg: &'tcx Expr<'_>) {
13     if_chain! {
14         if is_trait_method(cx, expr, sym::Iterator);
15         // range expression in `.zip()` call: `0..x.len()`
16         if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::Range::hir(zip_arg);
17         if is_integer_const(cx, start, 0);
18         // `.len()` call
19         if let ExprKind::MethodCall(len_path, len_recv, [], _) = end.kind;
20         if len_path.ident.name == sym::len;
21         // `.iter()` and `.len()` called on same `Path`
22         if let ExprKind::Path(QPath::Resolved(_, iter_path)) = recv.kind;
23         if let ExprKind::Path(QPath::Resolved(_, len_path)) = len_recv.kind;
24         if SpanlessEq::new(cx).eq_path_segments(iter_path.segments, len_path.segments);
25         then {
26             span_lint(cx,
27                 RANGE_ZIP_WITH_LEN,
28                 expr.span,
29                 &format!("it is more idiomatic to use `{}.iter().enumerate()`",
30                     snippet(cx, recv.span, "_"))
31             );
32         }
33     }
34 }