|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
2 | 2 | use clippy_utils::ty::has_iter_method;
|
3 | 3 | use clippy_utils::{match_trait_method, paths};
|
| 4 | +use if_chain::if_chain; |
4 | 5 | use rustc_errors::Applicability;
|
5 | 6 | use rustc_hir as hir;
|
6 | 7 | use rustc_lint::LateContext;
|
7 | 8 | use rustc_middle::ty::{self, Ty};
|
8 | 9 | use rustc_span::source_map::Span;
|
9 |
| -use rustc_span::symbol::Symbol; |
| 10 | +use rustc_span::symbol::{sym, Symbol}; |
10 | 11 |
|
11 | 12 | use super::INTO_ITER_ON_REF;
|
12 | 13 |
|
13 |
| -pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_>, method_span: Span) { |
14 |
| - if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) { |
15 |
| - return; |
16 |
| - } |
17 |
| - if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) { |
18 |
| - span_lint_and_sugg( |
19 |
| - cx, |
20 |
| - INTO_ITER_ON_REF, |
21 |
| - method_span, |
22 |
| - &format!( |
23 |
| - "this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`", |
24 |
| - method_name, kind, |
25 |
| - ), |
26 |
| - "call directly", |
27 |
| - method_name.to_string(), |
28 |
| - Applicability::MachineApplicable, |
29 |
| - ); |
| 14 | +pub(super) fn check( |
| 15 | + cx: &LateContext<'_>, |
| 16 | + expr: &hir::Expr<'_>, |
| 17 | + method_span: Span, |
| 18 | + method_name: Symbol, |
| 19 | + args: &[hir::Expr<'_>], |
| 20 | +) { |
| 21 | + let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]); |
| 22 | + if_chain! { |
| 23 | + if let ty::Ref(..) = self_ty.kind(); |
| 24 | + if method_name == sym::into_iter; |
| 25 | + if match_trait_method(cx, expr, &paths::INTO_ITERATOR); |
| 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 | + } |
30 | 41 | }
|
31 | 42 | }
|
32 | 43 |
|
|
0 commit comments