1
+ use std:: ops:: ControlFlow ;
2
+
1
3
use super :: WHILE_LET_ON_ITERATOR ;
2
4
use clippy_utils:: diagnostics:: span_lint_and_sugg;
3
5
use clippy_utils:: source:: snippet_with_applicability;
@@ -204,35 +206,32 @@ fn uses_iter<'tcx>(cx: &LateContext<'tcx>, iter_expr: &IterExpr, container: &'tc
204
206
struct V < ' a , ' b , ' tcx > {
205
207
cx : & ' a LateContext < ' tcx > ,
206
208
iter_expr : & ' b IterExpr ,
207
- uses_iter : bool ,
208
209
}
209
210
impl < ' tcx > Visitor < ' tcx > for V < ' _ , ' _ , ' tcx > {
210
- fn visit_expr ( & mut self , e : & ' tcx Expr < ' _ > ) {
211
- if self . uses_iter {
212
- // return
213
- } else if is_expr_same_child_or_parent_field ( self . cx , e, & self . iter_expr . fields , self . iter_expr . path ) {
214
- self . uses_iter = true ;
211
+ type Result = ControlFlow < ( ) > ;
212
+ fn visit_expr ( & mut self , e : & ' tcx Expr < ' _ > ) -> Self :: Result {
213
+ if is_expr_same_child_or_parent_field ( self . cx , e, & self . iter_expr . fields , self . iter_expr . path ) {
214
+ ControlFlow :: Break ( ( ) )
215
215
} else if let ( e, true ) = skip_fields_and_path ( e) {
216
216
if let Some ( e) = e {
217
- self . visit_expr ( e) ;
217
+ self . visit_expr ( e)
218
+ } else {
219
+ ControlFlow :: Continue ( ( ) )
218
220
}
219
221
} else if let ExprKind :: Closure ( & Closure { body : id, .. } ) = e. kind {
220
222
if is_res_used ( self . cx , self . iter_expr . path , id) {
221
- self . uses_iter = true ;
223
+ ControlFlow :: Break ( ( ) )
224
+ } else {
225
+ ControlFlow :: Continue ( ( ) )
222
226
}
223
227
} else {
224
- walk_expr ( self , e) ;
228
+ walk_expr ( self , e)
225
229
}
226
230
}
227
231
}
228
232
229
- let mut v = V {
230
- cx,
231
- iter_expr,
232
- uses_iter : false ,
233
- } ;
234
- v. visit_expr ( container) ;
235
- v. uses_iter
233
+ let mut v = V { cx, iter_expr } ;
234
+ v. visit_expr ( container) . is_break ( )
236
235
}
237
236
238
237
#[ expect( clippy:: too_many_lines) ]
@@ -242,34 +241,38 @@ fn needs_mutable_borrow(cx: &LateContext<'_>, iter_expr: &IterExpr, loop_expr: &
242
241
iter_expr : & ' b IterExpr ,
243
242
loop_id : HirId ,
244
243
after_loop : bool ,
245
- used_iter : bool ,
246
244
}
247
245
impl < ' tcx > Visitor < ' tcx > for AfterLoopVisitor < ' _ , ' _ , ' tcx > {
248
246
type NestedFilter = OnlyBodies ;
247
+ type Result = ControlFlow < ( ) > ;
249
248
fn nested_visit_map ( & mut self ) -> Self :: Map {
250
249
self . cx . tcx . hir ( )
251
250
}
252
251
253
- fn visit_expr ( & mut self , e : & ' tcx Expr < ' _ > ) {
254
- if self . used_iter {
255
- return ;
256
- }
252
+ fn visit_expr ( & mut self , e : & ' tcx Expr < ' _ > ) -> Self :: Result {
257
253
if self . after_loop {
258
254
if is_expr_same_child_or_parent_field ( self . cx , e, & self . iter_expr . fields , self . iter_expr . path ) {
259
- self . used_iter = true ;
255
+ ControlFlow :: Break ( ( ) )
260
256
} else if let ( e, true ) = skip_fields_and_path ( e) {
261
257
if let Some ( e) = e {
262
- self . visit_expr ( e) ;
258
+ self . visit_expr ( e)
259
+ } else {
260
+ ControlFlow :: Continue ( ( ) )
263
261
}
264
262
} else if let ExprKind :: Closure ( & Closure { body : id, .. } ) = e. kind {
265
- self . used_iter = is_res_used ( self . cx , self . iter_expr . path , id) ;
263
+ if is_res_used ( self . cx , self . iter_expr . path , id) {
264
+ ControlFlow :: Break ( ( ) )
265
+ } else {
266
+ ControlFlow :: Continue ( ( ) )
267
+ }
266
268
} else {
267
- walk_expr ( self , e) ;
269
+ walk_expr ( self , e)
268
270
}
269
271
} else if self . loop_id == e. hir_id {
270
272
self . after_loop = true ;
273
+ ControlFlow :: Continue ( ( ) )
271
274
} else {
272
- walk_expr ( self , e) ;
275
+ walk_expr ( self , e)
273
276
}
274
277
}
275
278
}
@@ -347,9 +350,8 @@ fn needs_mutable_borrow(cx: &LateContext<'_>, iter_expr: &IterExpr, loop_expr: &
347
350
iter_expr,
348
351
loop_id : loop_expr. hir_id ,
349
352
after_loop : false ,
350
- used_iter : false ,
351
353
} ;
352
- v. visit_expr ( cx. tcx . hir ( ) . body ( cx. enclosing_body . unwrap ( ) ) . value ) ;
353
- v . used_iter
354
+ v. visit_expr ( cx. tcx . hir ( ) . body ( cx. enclosing_body . unwrap ( ) ) . value )
355
+ . is_break ( )
354
356
}
355
357
}
0 commit comments