1 use crate::path_to_local_id;
3 use rustc_hir::intravisit::{self, walk_expr, NestedVisitorMap, Visitor};
4 use rustc_hir::{def::Res, Arm, Block, Body, BodyId, Expr, ExprKind, HirId, Stmt};
5 use rustc_lint::LateContext;
6 use rustc_middle::hir::map::Map;
8 /// Convenience method for creating a `Visitor` with just `visit_expr` overridden and nested
9 /// bodies (i.e. closures) are visited.
10 /// If the callback returns `true`, the expr just provided to the callback is walked.
12 pub fn expr_visitor<'tcx>(cx: &LateContext<'tcx>, f: impl FnMut(&'tcx Expr<'tcx>) -> bool) -> impl Visitor<'tcx> {
17 impl<'tcx, F: FnMut(&'tcx Expr<'tcx>) -> bool> Visitor<'tcx> for V<'tcx, F> {
19 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
20 NestedVisitorMap::OnlyBodies(self.hir)
23 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
25 walk_expr(self, expr);
29 V { hir: cx.tcx.hir(), f }
32 /// Convenience method for creating a `Visitor` with just `visit_expr` overridden and nested
33 /// bodies (i.e. closures) are not visited.
34 /// If the callback returns `true`, the expr just provided to the callback is walked.
36 pub fn expr_visitor_no_bodies<'tcx>(f: impl FnMut(&'tcx Expr<'tcx>) -> bool) -> impl Visitor<'tcx> {
38 impl<'tcx, F: FnMut(&'tcx Expr<'tcx>) -> bool> Visitor<'tcx> for V<F> {
39 type Map = intravisit::ErasedMap<'tcx>;
40 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
41 NestedVisitorMap::None
44 fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
53 /// returns `true` if expr contains match expr desugared from try
54 fn contains_try(expr: &hir::Expr<'_>) -> bool {
55 let mut found = false;
56 expr_visitor_no_bodies(|e| {
58 found = matches!(e.kind, hir::ExprKind::Match(_, _, hir::MatchSource::TryDesugar));
66 pub fn find_all_ret_expressions<'hir, F>(_cx: &LateContext<'_>, expr: &'hir hir::Expr<'hir>, callback: F) -> bool
68 F: FnMut(&'hir hir::Expr<'hir>) -> bool,
76 struct WithStmtGuarg<'a, F> {
77 val: &'a mut RetFinder<F>,
81 impl<F> RetFinder<F> {
82 fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> {
83 let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt);
91 impl<F> std::ops::Deref for WithStmtGuarg<'_, F> {
92 type Target = RetFinder<F>;
94 fn deref(&self) -> &Self::Target {
99 impl<F> std::ops::DerefMut for WithStmtGuarg<'_, F> {
100 fn deref_mut(&mut self) -> &mut Self::Target {
105 impl<F> Drop for WithStmtGuarg<'_, F> {
107 self.val.in_stmt = self.prev_in_stmt;
111 impl<'hir, F: FnMut(&'hir hir::Expr<'hir>) -> bool> intravisit::Visitor<'hir> for RetFinder<F> {
112 type Map = Map<'hir>;
114 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
115 intravisit::NestedVisitorMap::None
118 fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'_>) {
119 intravisit::walk_stmt(&mut *self.inside_stmt(true), stmt);
122 fn visit_expr(&mut self, expr: &'hir hir::Expr<'_>) {
128 hir::ExprKind::Ret(Some(expr)) => self.inside_stmt(false).visit_expr(expr),
129 _ => intravisit::walk_expr(self, expr),
133 hir::ExprKind::If(cond, then, else_opt) => {
134 self.inside_stmt(true).visit_expr(cond);
135 self.visit_expr(then);
136 if let Some(el) = else_opt {
140 hir::ExprKind::Match(cond, arms, _) => {
141 self.inside_stmt(true).visit_expr(cond);
143 self.visit_expr(arm.body);
146 hir::ExprKind::Block(..) => intravisit::walk_expr(self, expr),
147 hir::ExprKind::Ret(Some(expr)) => self.visit_expr(expr),
148 _ => self.failed |= !(self.cb)(expr),
154 !contains_try(expr) && {
155 let mut ret_finder = RetFinder {
160 ret_finder.visit_expr(expr);
165 /// A type which can be visited.
166 pub trait Visitable<'tcx> {
167 /// Calls the corresponding `visit_*` function on the visitor.
168 fn visit<V: Visitor<'tcx>>(self, visitor: &mut V);
170 macro_rules! visitable_ref {
171 ($t:ident, $f:ident) => {
172 impl Visitable<'tcx> for &'tcx $t<'tcx> {
173 fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) {
179 visitable_ref!(Arm, visit_arm);
180 visitable_ref!(Block, visit_block);
181 visitable_ref!(Body, visit_body);
182 visitable_ref!(Expr, visit_expr);
183 visitable_ref!(Stmt, visit_stmt);
185 // impl<'tcx, I: IntoIterator> Visitable<'tcx> for I
187 // I::Item: Visitable<'tcx>,
189 // fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) {
196 /// Checks if the given resolved path is used in the given body.
197 pub fn is_res_used(cx: &LateContext<'_>, res: Res, body: BodyId) -> bool {
198 let mut found = false;
199 expr_visitor(cx, |e| {
204 if let ExprKind::Path(p) = &e.kind {
205 if cx.qpath_res(p, e.hir_id) == res {
211 .visit_expr(&cx.tcx.hir().body(body).value);
215 /// Checks if the given local is used.
216 pub fn is_local_used(cx: &LateContext<'tcx>, visitable: impl Visitable<'tcx>, id: HirId) -> bool {
217 let mut is_used = false;
218 let mut visitor = expr_visitor(cx, |expr| {
220 is_used = path_to_local_id(expr, id);
224 visitable.visit(&mut visitor);