rust/clippy_lints/src/methods/iter_skip_next.rs
zetanumbers fe37cc1d97 Move some methods from tcx.hir() to tcx
Renamings:
- find -> opt_hir_node
- get -> hir_node
- find_by_def_id -> opt_hir_node_by_def_id
- get_by_def_id -> hir_node_by_def_id

Fix rebase changes using removed methods

Use `tcx.hir_node_by_def_id()` whenever possible in compiler

Fix clippy errors

Fix compiler

Apply suggestions from code review

Co-authored-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>

Add FIXME for `tcx.hir()` returned type about its removal

Simplify with with `tcx.hir_node_by_def_id`
2023-12-12 06:40:29 -08:00

43 lines
1.6 KiB
Rust

use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::{is_trait_method, path_to_local};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::{BindingAnnotation, Node, PatKind};
use rustc_lint::LateContext;
use rustc_span::sym;
use super::ITER_SKIP_NEXT;
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
// lint if caller of skip is an Iterator
if is_trait_method(cx, expr, sym::Iterator) {
let mut application = Applicability::MachineApplicable;
span_lint_and_then(
cx,
ITER_SKIP_NEXT,
expr.span.trim_start(recv.span).unwrap(),
"called `skip(..).next()` on an iterator",
|diag| {
if let Some(id) = path_to_local(recv)
&& let Node::Pat(pat) = cx.tcx.hir_node(id)
&& let PatKind::Binding(ann, _, _, _) = pat.kind
&& ann != BindingAnnotation::MUT
{
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,
);
},
);
}
}