]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/into_iter_on_ref.rs
Rollup merge of #105615 - WaffleLapkin:remove_opt_scope_span_mention, r=compiler...
[rust.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 `.{method_name}()` and will not consume the `{kind}`",
34                 ),
35                 "call directly",
36                 method_name.to_string(),
37                 Applicability::MachineApplicable,
38             );
39         }
40     }
41 }
42
43 fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
44     has_iter_method(cx, self_ref_ty).map(|ty_name| {
45         let ty::Ref(_, _, mutbl) = self_ref_ty.kind() else {
46             unreachable!()
47         };
48         let method_name = match mutbl {
49             hir::Mutability::Not => "iter",
50             hir::Mutability::Mut => "iter_mut",
51         };
52         (ty_name, method_name)
53     })
54 }