2021-12-30 14:10:43 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::is_trait_method;
|
2021-12-30 14:10:43 +00:00
|
|
|
use clippy_utils::path_to_local;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet;
|
2021-03-12 14:30:50 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
2021-12-30 14:10:43 +00:00
|
|
|
use rustc_hir::{BindingAnnotation, Node, PatKind};
|
2021-03-12 14:30:50 +00:00
|
|
|
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;
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
|
2021-03-12 14:30:50 +00:00
|
|
|
// 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-12-30 14:10:43 +00:00
|
|
|
let mut application = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_then(
|
2021-04-08 15:50:13 +00:00
|
|
|
cx,
|
|
|
|
ITER_SKIP_NEXT,
|
|
|
|
expr.span.trim_start(recv.span).unwrap(),
|
|
|
|
"called `skip(..).next()` on an iterator",
|
2021-12-30 14:10:43 +00:00
|
|
|
|diag| {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(id) = path_to_local(recv);
|
2022-06-28 18:15:30 +00:00
|
|
|
if let Node::Pat(pat) = cx.tcx.hir().get(id);
|
2021-12-30 14:10:43 +00:00
|
|
|
if let PatKind::Binding(ann, _, _, _) = pat.kind;
|
2022-08-30 22:36:53 +00:00
|
|
|
if ann != BindingAnnotation::MUT;
|
2021-12-30 14:10:43 +00:00
|
|
|
then {
|
|
|
|
application = Applicability::Unspecified;
|
|
|
|
diag.span_help(
|
|
|
|
pat.span,
|
|
|
|
&format!("for this change `{}` has to be mutable", snippet(cx, pat.span, "..")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diag.span_suggestion(
|
|
|
|
expr.span.trim_start(recv.span).unwrap(),
|
|
|
|
"use `nth` instead",
|
|
|
|
format!(".nth({})", snippet(cx, arg.span, "..")),
|
|
|
|
application,
|
|
|
|
);
|
|
|
|
},
|
2021-04-08 15:50:13 +00:00
|
|
|
);
|
2021-03-12 14:30:50 +00:00
|
|
|
}
|
|
|
|
}
|