]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/iter_nth.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / iter_nth.rs
CommitLineData
cdc7bbd5 1use super::utils::derefs_to_slice;
f20569fa 2use crate::methods::iter_nth_zero;
cdc7bbd5
XL
3use clippy_utils::diagnostics::span_lint_and_help;
4use clippy_utils::ty::is_type_diagnostic_item;
f20569fa
XL
5use rustc_hir as hir;
6use rustc_lint::LateContext;
7use rustc_span::symbol::sym;
8
9use super::ITER_NTH;
10
11pub(super) fn check<'tcx>(
12 cx: &LateContext<'tcx>,
13 expr: &hir::Expr<'_>,
cdc7bbd5
XL
14 iter_recv: &'tcx hir::Expr<'tcx>,
15 nth_recv: &hir::Expr<'_>,
16 nth_arg: &hir::Expr<'_>,
f20569fa
XL
17 is_mut: bool,
18) {
f20569fa 19 let mut_str = if is_mut { "_mut" } else { "" };
cdc7bbd5 20 let caller_type = if derefs_to_slice(cx, iter_recv, cx.typeck_results().expr_ty(iter_recv)).is_some() {
f20569fa 21 "slice"
cdc7bbd5 22 } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(iter_recv), sym::vec_type) {
f20569fa 23 "Vec"
cdc7bbd5 24 } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(iter_recv), sym::vecdeque_type) {
f20569fa
XL
25 "VecDeque"
26 } else {
cdc7bbd5 27 iter_nth_zero::check(cx, expr, nth_recv, nth_arg);
f20569fa
XL
28 return; // caller is not a type that we want to lint
29 };
30
31 span_lint_and_help(
32 cx,
33 ITER_NTH,
34 expr.span,
35 &format!("called `.iter{0}().nth()` on a {1}", mut_str, caller_type),
36 None,
37 &format!("calling `.get{}()` is both faster and more readable", mut_str),
38 );
39}