Auto merge of #8382 - tamaroning:suggest_iter_instead_of_into_iter, r=giraffate
[explicit_counter_loop] suggests `.into_iter()`, despite that triggering [into_iter_on_ref] in some cases I have modified `fn make_iterator_snippet` in clippy_lints/src/loops/utils.rs ,so this change has some little influence on another lint [manual_flatten] . fixes #8155 --- changelog: Fix that [`explicit_counter_loop`] suggests `into_iter()` despite that triggering [`into_iter_on_ref`] in some cases
This commit is contained in:
commit
bef92b864d
4 changed files with 26 additions and 25 deletions
|
|
@ -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,18 +332,21 @@ 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 method_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(),
|
||||
meth_name,
|
||||
sugg::Sugg::hir_with_applicability(cx, caller, "_", applic_ref).maybe_par(),
|
||||
method_name,
|
||||
)
|
||||
},
|
||||
_ => format!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue