]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/iter_skip_next.rs
New upstream version 1.73.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / iter_skip_next.rs
CommitLineData
a2a8927a 1use clippy_utils::diagnostics::span_lint_and_then;
cdc7bbd5 2use clippy_utils::source::snippet;
add651ee 3use clippy_utils::{is_trait_method, path_to_local};
f20569fa
XL
4use rustc_errors::Applicability;
5use rustc_hir as hir;
a2a8927a 6use rustc_hir::{BindingAnnotation, Node, PatKind};
f20569fa 7use rustc_lint::LateContext;
cdc7bbd5 8use rustc_span::sym;
f20569fa
XL
9
10use super::ITER_SKIP_NEXT;
11
cdc7bbd5 12pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
f20569fa 13 // lint if caller of skip is an Iterator
cdc7bbd5 14 if is_trait_method(cx, expr, sym::Iterator) {
a2a8927a
XL
15 let mut application = Applicability::MachineApplicable;
16 span_lint_and_then(
cdc7bbd5
XL
17 cx,
18 ITER_SKIP_NEXT,
19 expr.span.trim_start(recv.span).unwrap(),
20 "called `skip(..).next()` on an iterator",
a2a8927a
XL
21 |diag| {
22 if_chain! {
23 if let Some(id) = path_to_local(recv);
064997fb 24 if let Node::Pat(pat) = cx.tcx.hir().get(id);
a2a8927a 25 if let PatKind::Binding(ann, _, _, _) = pat.kind;
f2b60f7d 26 if ann != BindingAnnotation::MUT;
a2a8927a
XL
27 then {
28 application = Applicability::Unspecified;
29 diag.span_help(
30 pat.span,
9c376795 31 format!("for this change `{}` has to be mutable", snippet(cx, pat.span, "..")),
a2a8927a
XL
32 );
33 }
34 }
35
36 diag.span_suggestion(
37 expr.span.trim_start(recv.span).unwrap(),
38 "use `nth` instead",
39 format!(".nth({})", snippet(cx, arg.span, "..")),
40 application,
41 );
42 },
cdc7bbd5 43 );
f20569fa
XL
44 }
45}