]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/methods/into_iter_on_ref.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / into_iter_on_ref.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::is_trait_method;
3 use clippy_utils::ty::has_iter_method;
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir as hir;
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, Ty};
9 use rustc_span::source_map::Span;
10 use rustc_span::symbol::{sym, Symbol};
11
12 use super::INTO_ITER_ON_REF;
13
14 pub(super) fn check(
15 cx: &LateContext<'_>,
16 expr: &hir::Expr<'_>,
17 method_span: Span,
18 method_name: Symbol,
19 receiver: &hir::Expr<'_>,
20 ) {
21 let self_ty = cx.typeck_results().expr_ty_adjusted(receiver);
22 if_chain! {
23 if let ty::Ref(..) = self_ty.kind();
24 if method_name == sym::into_iter;
25 if is_trait_method(cx, expr, sym::IntoIterator);
26 if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ty);
27 then {
28 span_lint_and_sugg(
29 cx,
30 INTO_ITER_ON_REF,
31 method_span,
32 &format!(
33 "this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`",
34 method_name, kind,
35 ),
36 "call directly",
37 method_name.to_string(),
38 Applicability::MachineApplicable,
39 );
40 }
41 }
42 }
43
44 fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
45 has_iter_method(cx, self_ref_ty).map(|ty_name| {
46 let mutbl = match self_ref_ty.kind() {
47 ty::Ref(_, _, mutbl) => mutbl,
48 _ => unreachable!(),
49 };
50 let method_name = match mutbl {
51 hir::Mutability::Not => "iter",
52 hir::Mutability::Mut => "iter_mut",
53 };
54 (ty_name, method_name)
55 })
56 }