]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/iter_nth.rs
Auto merge of #6957 - camsteffen:eq-ty-kind, r=flip1995
[rust.git] / clippy_lints / src / methods / iter_nth.rs
1 use super::utils::derefs_to_slice;
2 use crate::methods::iter_nth_zero;
3 use clippy_utils::diagnostics::span_lint_and_help;
4 use clippy_utils::ty::is_type_diagnostic_item;
5 use rustc_hir as hir;
6 use rustc_lint::LateContext;
7 use rustc_span::symbol::sym;
8
9 use super::ITER_NTH;
10
11 pub(super) fn check<'tcx>(
12     cx: &LateContext<'tcx>,
13     expr: &hir::Expr<'_>,
14     nth_and_iter_args: &[&'tcx [hir::Expr<'tcx>]],
15     is_mut: bool,
16 ) {
17     let iter_args = nth_and_iter_args[1];
18     let mut_str = if is_mut { "_mut" } else { "" };
19     let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() {
20         "slice"
21     } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) {
22         "Vec"
23     } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vecdeque_type) {
24         "VecDeque"
25     } else {
26         let nth_args = nth_and_iter_args[0];
27         iter_nth_zero::check(cx, expr, &nth_args);
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 }