use crate::path_to_local_id;
use rustc_hir as hir;
-use rustc_hir::intravisit::{self, walk_expr, NestedVisitorMap, Visitor};
-use rustc_hir::{Arm, Body, Expr, HirId, Stmt};
+use rustc_hir::intravisit::{self, walk_expr, ErasedMap, NestedVisitorMap, Visitor};
+use rustc_hir::{Arm, Block, Body, Destination, Expr, ExprKind, HirId, Stmt};
use rustc_lint::LateContext;
use rustc_middle::hir::map::Map;
NestedVisitorMap::OnlyBodies(self.hir)
}
}
+
+pub trait Visitable<'tcx> {
+ fn visit<V: Visitor<'tcx>>(self, v: &mut V);
+}
+impl Visitable<'tcx> for &'tcx Expr<'tcx> {
+ fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
+ v.visit_expr(self)
+ }
+}
+impl Visitable<'tcx> for &'tcx Block<'tcx> {
+ fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
+ v.visit_block(self)
+ }
+}
+impl<'tcx> Visitable<'tcx> for &'tcx Stmt<'tcx> {
+ fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
+ v.visit_stmt(self)
+ }
+}
+impl<'tcx> Visitable<'tcx> for &'tcx Body<'tcx> {
+ fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
+ v.visit_body(self)
+ }
+}
+impl<'tcx> Visitable<'tcx> for &'tcx Arm<'tcx> {
+ fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
+ v.visit_arm(self)
+ }
+}
+
+pub fn visit_break_exprs<'tcx>(
+ node: impl Visitable<'tcx>,
+ f: impl FnMut(&'tcx Expr<'tcx>, Destination, Option<&'tcx Expr<'tcx>>),
+) {
+ struct V<F>(F);
+ impl<'tcx, F: FnMut(&'tcx Expr<'tcx>, Destination, Option<&'tcx Expr<'tcx>>)> Visitor<'tcx> for V<F> {
+ type Map = ErasedMap<'tcx>;
+ fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
+ NestedVisitorMap::None
+ }
+
+ fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
+ if let ExprKind::Break(dest, sub_expr) = e.kind {
+ self.0(e, dest, sub_expr)
+ }
+ walk_expr(self, e);
+ }
+ }
+
+ node.visit(&mut V(f));
+}