@@ -5,11 +5,12 @@ use clippy_utils::source::snippet;
5
5
use clippy_utils:: ty:: is_type_diagnostic_item;
6
6
use clippy_utils:: visitors:: for_each_expr;
7
7
use clippy_utils:: { eq_expr_value, path_to_local, path_to_local_id} ;
8
- use rustc_ast:: { BorrowKind , LitKind , Mutability } ;
8
+ use rustc_ast:: { LitKind , Mutability } ;
9
9
use rustc_errors:: Applicability ;
10
10
use rustc_hir:: { Block , Expr , ExprKind , LetStmt , Node , UnOp } ;
11
11
use rustc_lint:: { LateContext , LateLintPass } ;
12
12
use rustc_middle:: ty:: adjustment:: { Adjust , AutoBorrow , AutoBorrowMutability } ;
13
+ use rustc_middle:: ty:: { self } ;
13
14
use rustc_session:: declare_lint_pass;
14
15
use rustc_span:: sym;
15
16
@@ -53,19 +54,16 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
53
54
&& let ExprKind :: MethodCall ( method, conditional_receiver, _, _) = unary_inner. kind
54
55
&& method. ident . as_str ( ) == "is_empty"
55
56
&& let typeck_results = cx. typeck_results ( )
56
- // do not lint on mutable auto borrows (https://github.com/rust-lang/rust-clippy/pull/12464#discussion_r1600352696)
57
- && let adjustments = typeck_results. expr_adjustments ( conditional_receiver)
58
- && !adjustments. iter ( ) . any ( |adjustment| {
59
- matches ! ( adjustment. kind, Adjust :: Borrow ( AutoBorrow :: Ref ( _, AutoBorrowMutability :: Mut {
60
- allow_two_phase_borrow: _
61
- } ) ) )
62
- } )
63
- // do not lint if receiver is a mutable reference
64
- && let ExprKind :: AddrOf ( BorrowKind :: Ref , Mutability :: Mut , _) = conditional_receiver. kind
65
- && let expr_ty = typeck_results. expr_ty ( conditional_receiver) . peel_refs ( )
66
- && ( expr_ty. is_slice ( ) || expr_ty. is_array ( ) || is_type_diagnostic_item ( cx, expr_ty, sym:: Vec ) )
57
+ && let expr_ty = typeck_results. expr_ty ( conditional_receiver)
58
+ && let peeled = expr_ty. peel_refs ( )
59
+ && ( peeled. is_slice ( ) || peeled. is_array ( ) || is_type_diagnostic_item ( cx, peeled, sym:: Vec ) )
67
60
&& let ExprKind :: Block ( block, _) = if_expr. then . kind
68
61
{
62
+ // do not lint if conditional receiver is mutable reference
63
+ if let ty:: Ref ( _, _, Mutability :: Mut ) = expr_ty. kind ( ) {
64
+ return ;
65
+ }
66
+
69
67
let result = process_indexing ( cx, block, conditional_receiver) ;
70
68
71
69
if let Some ( r) = result
@@ -178,6 +176,8 @@ fn process_indexing<'a>(
178
176
// if res == Some(()), then mutation occurred
179
177
// & therefore we should not lint on this
180
178
let res = for_each_expr ( block. stmts , |x| {
179
+ let adjustments = cx. typeck_results ( ) . expr_adjustments ( x) ;
180
+
181
181
if let ExprKind :: Index ( receiver, index, _) = x. kind
182
182
&& let ExprKind :: Lit ( lit) = index. kind
183
183
&& let LitKind :: Int ( val, _) = lit. node
@@ -195,6 +195,14 @@ fn process_indexing<'a>(
195
195
} else {
196
196
extra_exprs. push ( x) ;
197
197
} ;
198
+ } else if adjustments. iter ( ) . any ( |adjustment| {
199
+ matches ! (
200
+ adjustment. kind,
201
+ Adjust :: Borrow ( AutoBorrow :: Ref ( _, AutoBorrowMutability :: Mut { .. } ) )
202
+ )
203
+ } ) {
204
+ // do not lint on mutable auto borrows (https://github.com/rust-lang/rust-clippy/pull/12464#discussion_r1600352696)
205
+ return ControlFlow :: Break ( ( ) ) ;
198
206
} else if let ExprKind :: AddrOf ( _, Mutability :: Mut , val) = x. kind
199
207
&& eq_expr_value ( cx, conditional_receiver, val)
200
208
{
0 commit comments