2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-16 11:06:34 -05:00
|
|
|
use clippy_utils::is_trait_method;
|
2021-03-14 18:17:44 -05:00
|
|
|
use clippy_utils::source::snippet;
|
2021-03-06 03:45:25 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2021-03-12 11:09:19 -06:00
|
|
|
use rustc_span::sym;
|
2021-03-06 03:45:25 -06:00
|
|
|
|
|
|
|
use super::ITER_SKIP_NEXT;
|
|
|
|
|
2021-03-10 23:40:20 -06:00
|
|
|
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
|
2021-03-06 03:45:25 -06:00
|
|
|
// lint if caller of skip is an Iterator
|
2021-03-12 11:09:19 -06:00
|
|
|
if is_trait_method(cx, expr, sym::Iterator) {
|
2021-03-10 23:40:20 -06:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
ITER_SKIP_NEXT,
|
|
|
|
expr.span.trim_start(recv.span).unwrap(),
|
|
|
|
"called `skip(..).next()` on an iterator",
|
|
|
|
"use `nth` instead",
|
|
|
|
format!(".nth({})", snippet(cx, arg.span, "..")),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2021-03-06 03:45:25 -06:00
|
|
|
}
|
|
|
|
}
|