]>
Commit | Line | Data |
---|---|---|
a2a8927a | 1 | use clippy_utils::diagnostics::span_lint_and_then; |
cdc7bbd5 | 2 | use clippy_utils::source::snippet; |
add651ee | 3 | use clippy_utils::{is_trait_method, path_to_local}; |
f20569fa XL |
4 | use rustc_errors::Applicability; |
5 | use rustc_hir as hir; | |
a2a8927a | 6 | use rustc_hir::{BindingAnnotation, Node, PatKind}; |
f20569fa | 7 | use rustc_lint::LateContext; |
cdc7bbd5 | 8 | use rustc_span::sym; |
f20569fa XL |
9 | |
10 | use super::ITER_SKIP_NEXT; | |
11 | ||
cdc7bbd5 | 12 | pub(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 | } |