summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/methods/range_zip_with_len.rs
blob: 867a3b4023770e6e162bc2227960022866d10cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use clippy_utils::diagnostics::span_lint;
use clippy_utils::source::snippet;
use clippy_utils::{higher, SpanlessEq};
use clippy_utils::{is_integer_const, is_trait_method};
use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::LateContext;
use rustc_span::sym;

use super::RANGE_ZIP_WITH_LEN;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, zip_arg: &'tcx Expr<'_>) {
    if_chain! {
        if is_trait_method(cx, expr, sym::Iterator);
        // range expression in `.zip()` call: `0..x.len()`
        if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::Range::hir(zip_arg);
        if is_integer_const(cx, start, 0);
        // `.len()` call
        if let ExprKind::MethodCall(len_path, len_recv, [], _) = end.kind;
        if len_path.ident.name == sym::len;
        // `.iter()` and `.len()` called on same `Path`
        if let ExprKind::Path(QPath::Resolved(_, iter_path)) = recv.kind;
        if let ExprKind::Path(QPath::Resolved(_, len_path)) = len_recv.kind;
        if SpanlessEq::new(cx).eq_path_segments(iter_path.segments, len_path.segments);
        then {
            span_lint(cx,
                RANGE_ZIP_WITH_LEN,
                expr.span,
                &format!("it is more idiomatic to use `{}.iter().enumerate()`",
                    snippet(cx, recv.span, "_"))
            );
        }
    }
}