2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::is_trait_method;
|
|
|
|
use clippy_utils::source::snippet;
|
2021-03-12 14:30:50 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2021-03-25 18:29:11 +00:00
|
|
|
use rustc_span::sym;
|
2021-03-12 14:30:50 +00:00
|
|
|
|
|
|
|
use super::ITER_SKIP_NEXT;
|
|
|
|
|
|
|
|
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[hir::Expr<'_>]) {
|
|
|
|
// lint if caller of skip is an Iterator
|
2021-03-25 18:29:11 +00:00
|
|
|
if is_trait_method(cx, expr, sym::Iterator) {
|
2021-03-12 14:30:50 +00:00
|
|
|
if let [caller, n] = skip_args {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
ITER_SKIP_NEXT,
|
|
|
|
expr.span.trim_start(caller.span).unwrap(),
|
|
|
|
"called `skip(..).next()` on an iterator",
|
|
|
|
"use `nth` instead",
|
2021-03-25 18:29:11 +00:00
|
|
|
format!(".nth({})", snippet(cx, n.span, "..")),
|
2021-03-12 14:30:50 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|