2021-03-04 14:06:05 +00:00
|
|
|
use crate::consts::{constant, Constant};
|
2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-16 16:06:34 +00:00
|
|
|
use clippy_utils::is_trait_method;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2021-03-04 14:06:05 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2021-03-12 17:09:19 +00:00
|
|
|
use rustc_span::sym;
|
2021-03-04 14:06:05 +00:00
|
|
|
|
|
|
|
use super::ITER_NTH_ZERO;
|
|
|
|
|
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, nth_args: &'tcx [hir::Expr<'_>]) {
|
|
|
|
if_chain! {
|
2021-03-12 17:09:19 +00:00
|
|
|
if is_trait_method(cx, expr, sym::Iterator);
|
2021-03-04 14:06:05 +00:00
|
|
|
if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), &nth_args[1]);
|
|
|
|
then {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
ITER_NTH_ZERO,
|
|
|
|
expr.span,
|
|
|
|
"called `.nth(0)` on a `std::iter::Iterator`, when `.next()` is equivalent",
|
|
|
|
"try calling `.next()` instead of `.nth(0)`",
|
|
|
|
format!("{}.next()", snippet_with_applicability(cx, nth_args[0].span, "..", &mut applicability)),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|