1+ use std:: ops:: ControlFlow ;
2+
13use super :: WHILE_LET_ON_ITERATOR ;
24use clippy_utils:: diagnostics:: span_lint_and_sugg;
35use clippy_utils:: source:: snippet_with_applicability;
@@ -204,35 +206,32 @@ fn uses_iter<'tcx>(cx: &LateContext<'tcx>, iter_expr: &IterExpr, container: &'tc
204206 struct V < ' a , ' b , ' tcx > {
205207 cx : & ' a LateContext < ' tcx > ,
206208 iter_expr : & ' b IterExpr ,
207- uses_iter : bool ,
208209 }
209210 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 ( ( ) )
215215 } else if let ( e, true ) = skip_fields_and_path ( e) {
216216 if let Some ( e) = e {
217- self . visit_expr ( e) ;
217+ self . visit_expr ( e)
218+ } else {
219+ ControlFlow :: Continue ( ( ) )
218220 }
219221 } else if let ExprKind :: Closure ( & Closure { body : id, .. } ) = e. kind {
220222 if is_res_used ( self . cx , self . iter_expr . path , id) {
221- self . uses_iter = true ;
223+ ControlFlow :: Break ( ( ) )
224+ } else {
225+ ControlFlow :: Continue ( ( ) )
222226 }
223227 } else {
224- walk_expr ( self , e) ;
228+ walk_expr ( self , e)
225229 }
226230 }
227231 }
228232
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 ( )
236235}
237236
238237#[ expect( clippy:: too_many_lines) ]
@@ -242,34 +241,38 @@ fn needs_mutable_borrow(cx: &LateContext<'_>, iter_expr: &IterExpr, loop_expr: &
242241 iter_expr : & ' b IterExpr ,
243242 loop_id : HirId ,
244243 after_loop : bool ,
245- used_iter : bool ,
246244 }
247245 impl < ' tcx > Visitor < ' tcx > for AfterLoopVisitor < ' _ , ' _ , ' tcx > {
248246 type NestedFilter = OnlyBodies ;
247+ type Result = ControlFlow < ( ) > ;
249248 fn nested_visit_map ( & mut self ) -> Self :: Map {
250249 self . cx . tcx . hir ( )
251250 }
252251
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 {
257253 if self . after_loop {
258254 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 ( ( ) )
260256 } else if let ( e, true ) = skip_fields_and_path ( e) {
261257 if let Some ( e) = e {
262- self . visit_expr ( e) ;
258+ self . visit_expr ( e)
259+ } else {
260+ ControlFlow :: Continue ( ( ) )
263261 }
264262 } 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+ }
266268 } else {
267- walk_expr ( self , e) ;
269+ walk_expr ( self , e)
268270 }
269271 } else if self . loop_id == e. hir_id {
270272 self . after_loop = true ;
273+ ControlFlow :: Continue ( ( ) )
271274 } else {
272- walk_expr ( self , e) ;
275+ walk_expr ( self , e)
273276 }
274277 }
275278 }
@@ -347,9 +350,8 @@ fn needs_mutable_borrow(cx: &LateContext<'_>, iter_expr: &IterExpr, loop_expr: &
347350 iter_expr,
348351 loop_id : loop_expr. hir_id ,
349352 after_loop : false ,
350- used_iter : false ,
351353 } ;
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 ( )
354356 }
355357}
0 commit comments