]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/iter_nth_zero.rs
Auto merge of #82680 - jturner314:div_euclid-docs, r=JohnTitor
[rust.git] / src / tools / clippy / clippy_lints / src / methods / iter_nth_zero.rs
1 use crate::consts::{constant, Constant};
2 use crate::utils::{match_trait_method, paths, snippet_with_applicability, span_lint_and_sugg};
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir as hir;
6 use rustc_lint::LateContext;
7
8 use super::ITER_NTH_ZERO;
9
10 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, nth_args: &'tcx [hir::Expr<'_>]) {
11     if_chain! {
12         if match_trait_method(cx, expr, &paths::ITERATOR);
13         if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), &nth_args[1]);
14         then {
15             let mut applicability = Applicability::MachineApplicable;
16             span_lint_and_sugg(
17                 cx,
18                 ITER_NTH_ZERO,
19                 expr.span,
20                 "called `.nth(0)` on a `std::iter::Iterator`, when `.next()` is equivalent",
21                 "try calling `.next()` instead of `.nth(0)`",
22                 format!("{}.next()", snippet_with_applicability(cx, nth_args[0].span, "..", &mut applicability)),
23                 applicability,
24             );
25         }
26     }
27 }