]>
git.proxmox.com Git - rustc.git/blob - 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
;
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}
;
12 use super::INTO_ITER_ON_REF
;
19 receiver
: &hir
::Expr
<'_
>,
21 let self_ty
= cx
.typeck_results().expr_ty_adjusted(receiver
);
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
);
33 "this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`",
37 method_name
.to_string(),
38 Applicability
::MachineApplicable
,
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
,
50 let method_name
= match mutbl
{
51 hir
::Mutability
::Not
=> "iter",
52 hir
::Mutability
::Mut
=> "iter_mut",
54 (ty_name
, method_name
)