diff options
| author | tamaron <tamaron1203@gmail.com> | 2022-02-01 13:43:39 +0900 |
|---|---|---|
| committer | tamaron <tamaron1203@gmail.com> | 2022-02-01 13:43:39 +0900 |
| commit | 0e1cbc5cd1d2e2a52b934dda1701d46fdb4e09b8 (patch) | |
| tree | 6a23737504fed72d27ab3a0f84812fd63e2b8bd9 | |
| parent | 7bb69c0ae024eef65acb7fd6551fdd99f1563d38 (diff) | |
| download | rust-0e1cbc5cd1d2e2a52b934dda1701d46fdb4e09b8.tar.gz rust-0e1cbc5cd1d2e2a52b934dda1701d46fdb4e09b8.zip | |
fix code
| -rw-r--r-- | clippy_lints/src/loops/utils.rs | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/clippy_lints/src/loops/utils.rs b/clippy_lints/src/loops/utils.rs index eac0f03b142..9082ab94374 100644 --- a/clippy_lints/src/loops/utils.rs +++ b/clippy_lints/src/loops/utils.rs @@ -7,7 +7,7 @@ use rustc_hir::intravisit::{walk_expr, walk_local, walk_pat, walk_stmt, Visitor} use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, HirId, HirIdMap, Local, Mutability, Pat, PatKind, Stmt}; use rustc_lint::LateContext; use rustc_middle::hir::nested_filter; -use rustc_middle::ty::Ty; +use rustc_middle::ty::{self, Ty}; use rustc_span::source_map::Spanned; use rustc_span::symbol::{sym, Symbol}; use rustc_typeck::hir_ty_to_ty; @@ -332,17 +332,20 @@ pub(super) fn make_iterator_snippet(cx: &LateContext<'_>, arg: &Expr<'_>, applic } else { // (&x).into_iter() ==> x.iter() // (&mut x).into_iter() ==> x.iter_mut() - match &arg.kind { - ExprKind::AddrOf(BorrowKind::Ref, mutability, arg_inner) - if has_iter_method(cx, cx.typeck_results().expr_ty(arg_inner)).is_some() => - { - let meth_name = match mutability { + let arg_ty = cx.typeck_results().expr_ty_adjusted(arg); + match &arg_ty.kind() { + ty::Ref(_, inner_ty, mutbl) if has_iter_method(cx, inner_ty).is_some() => { + let meth_name = match mutbl { Mutability::Mut => "iter_mut", Mutability::Not => "iter", }; + let caller = match &arg.kind { + ExprKind::AddrOf(BorrowKind::Ref, _, arg_inner) => arg_inner, + _ => arg, + }; format!( "{}.{}()", - sugg::Sugg::hir_with_applicability(cx, arg_inner, "_", applic_ref).maybe_par(), + sugg::Sugg::hir_with_applicability(cx, caller, "_", applic_ref).maybe_par(), meth_name, ) }, |
