-pub struct LocalUsedVisitor<'hir> {
- hir: Map<'hir>,
- pub local_hir_id: HirId,
- pub used: bool,
-}
-
-impl<'hir> LocalUsedVisitor<'hir> {
- pub fn new(cx: &LateContext<'hir>, local_hir_id: HirId) -> Self {
- Self {
- hir: cx.tcx.hir(),
- local_hir_id,
- used: false,
- }
- }
-
- fn check<T>(&mut self, t: T, visit: fn(&mut Self, T)) -> bool {
- visit(self, t);
- std::mem::replace(&mut self.used, false)
- }
-
- pub fn check_arm(&mut self, arm: &'hir Arm<'_>) -> bool {
- self.check(arm, Self::visit_arm)
- }
-
- pub fn check_body(&mut self, body: &'hir Body<'_>) -> bool {
- self.check(body, Self::visit_body)
- }
-
- pub fn check_expr(&mut self, expr: &'hir Expr<'_>) -> bool {
- self.check(expr, Self::visit_expr)
- }
-
- pub fn check_stmt(&mut self, stmt: &'hir Stmt<'_>) -> bool {
- self.check(stmt, Self::visit_stmt)
- }
-}
-
-impl<'v> Visitor<'v> for LocalUsedVisitor<'v> {
- type Map = Map<'v>;
-
- fn visit_expr(&mut self, expr: &'v Expr<'v>) {
- if self.used {
- return;
- }
- if path_to_local_id(expr, self.local_hir_id) {
- self.used = true;
- } else {
- walk_expr(self, expr);
- }
- }
-
- fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
- NestedVisitorMap::OnlyBodies(self.hir)
- }
-}
-