1 use crate::path_to_local_id;
3 use rustc_hir::intravisit::{self, walk_expr, ErasedMap, NestedVisitorMap, Visitor};
4 use rustc_hir::{def::Res, Arm, Block, Body, BodyId, Destination, Expr, ExprKind, HirId, Stmt};
5 use rustc_lint::LateContext;
6 use rustc_middle::hir::map::Map;
8 /// returns `true` if expr contains match expr desugared from try
9 fn contains_try(expr: &hir::Expr<'_>) -> bool {
14 impl<'hir> intravisit::Visitor<'hir> for TryFinder {
17 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
18 intravisit::NestedVisitorMap::None
21 fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
26 hir::ExprKind::Match(_, _, hir::MatchSource::TryDesugar) => self.found = true,
27 _ => intravisit::walk_expr(self, expr),
32 let mut visitor = TryFinder { found: false };
33 visitor.visit_expr(expr);
37 pub fn find_all_ret_expressions<'hir, F>(_cx: &LateContext<'_>, expr: &'hir hir::Expr<'hir>, callback: F) -> bool
39 F: FnMut(&'hir hir::Expr<'hir>) -> bool,
47 struct WithStmtGuarg<'a, F> {
48 val: &'a mut RetFinder<F>,
52 impl<F> RetFinder<F> {
53 fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> {
54 let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt);
62 impl<F> std::ops::Deref for WithStmtGuarg<'_, F> {
63 type Target = RetFinder<F>;
65 fn deref(&self) -> &Self::Target {
70 impl<F> std::ops::DerefMut for WithStmtGuarg<'_, F> {
71 fn deref_mut(&mut self) -> &mut Self::Target {
76 impl<F> Drop for WithStmtGuarg<'_, F> {
78 self.val.in_stmt = self.prev_in_stmt;
82 impl<'hir, F: FnMut(&'hir hir::Expr<'hir>) -> bool> intravisit::Visitor<'hir> for RetFinder<F> {
85 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
86 intravisit::NestedVisitorMap::None
89 fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'_>) {
90 intravisit::walk_stmt(&mut *self.inside_stmt(true), stmt);
93 fn visit_expr(&mut self, expr: &'hir hir::Expr<'_>) {
99 hir::ExprKind::Ret(Some(expr)) => self.inside_stmt(false).visit_expr(expr),
100 _ => intravisit::walk_expr(self, expr),
104 hir::ExprKind::If(cond, then, else_opt) => {
105 self.inside_stmt(true).visit_expr(cond);
106 self.visit_expr(then);
107 if let Some(el) = else_opt {
111 hir::ExprKind::Match(cond, arms, _) => {
112 self.inside_stmt(true).visit_expr(cond);
114 self.visit_expr(arm.body);
117 hir::ExprKind::Block(..) => intravisit::walk_expr(self, expr),
118 hir::ExprKind::Ret(Some(expr)) => self.visit_expr(expr),
119 _ => self.failed |= !(self.cb)(expr),
125 !contains_try(expr) && {
126 let mut ret_finder = RetFinder {
131 ret_finder.visit_expr(expr);
136 pub struct LocalUsedVisitor<'hir> {
138 pub local_hir_id: HirId,
142 impl<'hir> LocalUsedVisitor<'hir> {
143 pub fn new(cx: &LateContext<'hir>, local_hir_id: HirId) -> Self {
151 fn check<T>(&mut self, t: T, visit: fn(&mut Self, T)) -> bool {
153 std::mem::replace(&mut self.used, false)
156 pub fn check_arm(&mut self, arm: &'hir Arm<'_>) -> bool {
157 self.check(arm, Self::visit_arm)
160 pub fn check_body(&mut self, body: &'hir Body<'_>) -> bool {
161 self.check(body, Self::visit_body)
164 pub fn check_expr(&mut self, expr: &'hir Expr<'_>) -> bool {
165 self.check(expr, Self::visit_expr)
168 pub fn check_stmt(&mut self, stmt: &'hir Stmt<'_>) -> bool {
169 self.check(stmt, Self::visit_stmt)
173 impl<'v> Visitor<'v> for LocalUsedVisitor<'v> {
176 fn visit_expr(&mut self, expr: &'v Expr<'v>) {
180 if path_to_local_id(expr, self.local_hir_id) {
183 walk_expr(self, expr);
187 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
188 NestedVisitorMap::OnlyBodies(self.hir)
192 /// A type which can be visited.
193 pub trait Visitable<'tcx> {
194 /// Calls the corresponding `visit_*` function on the visitor.
195 fn visit<V: Visitor<'tcx>>(self, visitor: &mut V);
197 macro_rules! visitable_ref {
198 ($t:ident, $f:ident) => {
199 impl Visitable<'tcx> for &'tcx $t<'tcx> {
200 fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) {
206 visitable_ref!(Block, visit_block);
208 /// Calls the given function for each break expression.
209 pub fn visit_break_exprs<'tcx>(
210 node: impl Visitable<'tcx>,
211 f: impl FnMut(&'tcx Expr<'tcx>, Destination, Option<&'tcx Expr<'tcx>>),
214 impl<'tcx, F: FnMut(&'tcx Expr<'tcx>, Destination, Option<&'tcx Expr<'tcx>>)> Visitor<'tcx> for V<F> {
215 type Map = ErasedMap<'tcx>;
216 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
217 NestedVisitorMap::None
220 fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
221 if let ExprKind::Break(dest, sub_expr) = e.kind {
222 self.0(e, dest, sub_expr);
228 node.visit(&mut V(f));
231 /// Checks if the given resolved path is used in the given body.
232 pub fn is_res_used(cx: &LateContext<'_>, res: Res, body: BodyId) -> bool {
234 cx: &'a LateContext<'tcx>,
238 impl Visitor<'tcx> for V<'_, 'tcx> {
239 type Map = Map<'tcx>;
240 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
241 NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
244 fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
249 if let ExprKind::Path(p) = &e.kind {
250 if self.cx.qpath_res(p, e.hir_id) == self.res {
259 let mut v = V { cx, res, found: false };
260 v.visit_expr(&cx.tcx.hir().body(body).value);