@@ -4,11 +4,11 @@ use clippy_utils::higher::ForLoop;
44use clippy_utils:: source:: SpanRangeExt ;
55use clippy_utils:: ty:: { get_iterator_item_ty, implements_trait} ;
66use clippy_utils:: visitors:: for_each_expr_without_closures;
7- use clippy_utils:: { can_mut_borrow_both, fn_def_id, get_parent_expr, path_to_local } ;
7+ use clippy_utils:: { can_mut_borrow_both, fn_def_id, get_parent_expr, is_path_mutable } ;
88use core:: ops:: ControlFlow ;
99use rustc_errors:: Applicability ;
1010use rustc_hir:: def_id:: DefId ;
11- use rustc_hir:: { BindingMode , Expr , ExprKind , Node , PatKind } ;
11+ use rustc_hir:: { Expr , ExprKind } ;
1212use rustc_lint:: LateContext ;
1313use rustc_span:: { Symbol , sym} ;
1414
@@ -42,22 +42,7 @@ pub fn check_for_loop_iter(
4242 && !clone_or_copy_needed
4343 && let Some ( receiver_snippet) = receiver. span . get_source_text ( cx)
4444 {
45- // Issue 12098
46- // https://github.com/rust-lang/rust-clippy/issues/12098
47- // if the assignee have `mut borrow` conflict with the iteratee
48- // the lint should not execute, former didn't consider the mut case
49-
5045 // check whether `expr` is mutable
51- fn is_mutable ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
52- if let Some ( hir_id) = path_to_local ( expr)
53- && let Node :: Pat ( pat) = cx. tcx . hir_node ( hir_id)
54- {
55- matches ! ( pat. kind, PatKind :: Binding ( BindingMode :: MUT , ..) )
56- } else {
57- true
58- }
59- }
60-
6146 fn is_caller_or_fields_change ( cx : & LateContext < ' _ > , body : & Expr < ' _ > , caller : & Expr < ' _ > ) -> bool {
6247 let mut change = false ;
6348 if let ExprKind :: Block ( block, ..) = body. kind {
@@ -82,7 +67,12 @@ pub fn check_for_loop_iter(
8267 while let ExprKind :: MethodCall ( _, caller, _, _) = child. kind {
8368 child = caller;
8469 }
85- if is_mutable ( cx, child) && is_caller_or_fields_change ( cx, body, child) {
70+
71+ // Issue 12098
72+ // https://github.com/rust-lang/rust-clippy/issues/12098
73+ // if the assignee have `mut borrow` conflict with the iteratee
74+ // the lint should not execute, former didn't consider the mut case
75+ if is_path_mutable ( cx, child) . unwrap_or ( true ) && is_caller_or_fields_change ( cx, body, child) {
8676 // skip lint
8777 return true ;
8878 }
0 commit comments