From 3d549fb71bd76eb171543a3ddb70cd10080fbf4e Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sun, 2 Jun 2024 23:47:45 +0200 Subject: [PATCH] Also handle for_each_expr --- clippy_utils/src/visitors.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/clippy_utils/src/visitors.rs b/clippy_utils/src/visitors.rs index a3f3b32ed372..33372adae19c 100644 --- a/clippy_utils/src/visitors.rs +++ b/clippy_utils/src/visitors.rs @@ -109,30 +109,43 @@ pub fn for_each_expr<'tcx, B, C: Continue>( res: Option, } impl<'tcx, B, C: Continue, F: FnMut(&'tcx Expr<'tcx>) -> ControlFlow> Visitor<'tcx> for V { - fn visit_expr(&mut self, e: &'tcx Expr<'tcx>) { + type Result = ControlFlow<()>; + + fn visit_expr(&mut self, e: &'tcx Expr<'tcx>) -> ControlFlow<()> { if self.res.is_some() { - return; + return ControlFlow::Break(()); } match (self.f)(e) { ControlFlow::Continue(c) if c.descend() => walk_expr(self, e), - ControlFlow::Break(b) => self.res = Some(b), - ControlFlow::Continue(_) => (), + ControlFlow::Break(b) => { + self.res = Some(b); + ControlFlow::Break(()) + }, + ControlFlow::Continue(_) => ControlFlow::Continue(()), } } // Avoid unnecessary `walk_*` calls. - fn visit_ty(&mut self, _: &'tcx hir::Ty<'tcx>) {} - fn visit_pat(&mut self, _: &'tcx Pat<'tcx>) {} - fn visit_qpath(&mut self, _: &'tcx QPath<'tcx>, _: HirId, _: Span) {} + fn visit_ty(&mut self, _: &'tcx hir::Ty<'tcx>) -> ControlFlow<()> { + ControlFlow::Continue(()) + } + fn visit_pat(&mut self, _: &'tcx Pat<'tcx>) -> ControlFlow<()> { + ControlFlow::Continue(()) + } + fn visit_qpath(&mut self, _: &'tcx QPath<'tcx>, _: HirId, _: Span) -> ControlFlow<()> { + ControlFlow::Continue(()) + } // Avoid monomorphising all `visit_*` functions. - fn visit_nested_item(&mut self, _: ItemId) {} + fn visit_nested_item(&mut self, _: ItemId) -> ControlFlow<()> { + ControlFlow::Continue(()) + } } let mut v = V { f, res: None }; node.visit(&mut v); v.res } -/// Calls the given function once for each expression contained. This will enter bodies, but not +/// Calls the given function once for each expression contained. This will enter bodies, bzut not /// nested items. pub fn for_each_expr_with_closures<'tcx, B, C: Continue>( cx: &LateContext<'tcx>,