1 use crate::path_to_local_id;
3 use rustc_hir::def::{DefKind, Res};
4 use rustc_hir::intravisit::{self, walk_expr, NestedVisitorMap, Visitor};
5 use rustc_hir::{Arm, Block, Body, BodyId, Expr, ExprKind, HirId, Stmt, UnOp};
6 use rustc_lint::LateContext;
7 use rustc_middle::hir::map::Map;
10 /// Convenience method for creating a `Visitor` with just `visit_expr` overridden and nested
11 /// bodies (i.e. closures) are visited.
12 /// If the callback returns `true`, the expr just provided to the callback is walked.
14 pub fn expr_visitor<'tcx>(cx: &LateContext<'tcx>, f: impl FnMut(&'tcx Expr<'tcx>) -> bool) -> impl Visitor<'tcx> {
19 impl<'tcx, F: FnMut(&'tcx Expr<'tcx>) -> bool> Visitor<'tcx> for V<'tcx, F> {
21 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
22 NestedVisitorMap::OnlyBodies(self.hir)
25 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
27 walk_expr(self, expr);
31 V { hir: cx.tcx.hir(), f }
34 /// Convenience method for creating a `Visitor` with just `visit_expr` overridden and nested
35 /// bodies (i.e. closures) are not visited.
36 /// If the callback returns `true`, the expr just provided to the callback is walked.
38 pub fn expr_visitor_no_bodies<'tcx>(f: impl FnMut(&'tcx Expr<'tcx>) -> bool) -> impl Visitor<'tcx> {
40 impl<'tcx, F: FnMut(&'tcx Expr<'tcx>) -> bool> Visitor<'tcx> for V<F> {
41 type Map = intravisit::ErasedMap<'tcx>;
42 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
43 NestedVisitorMap::None
46 fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
55 /// returns `true` if expr contains match expr desugared from try
56 fn contains_try(expr: &hir::Expr<'_>) -> bool {
57 let mut found = false;
58 expr_visitor_no_bodies(|e| {
60 found = matches!(e.kind, hir::ExprKind::Match(_, _, hir::MatchSource::TryDesugar));
68 pub fn find_all_ret_expressions<'hir, F>(_cx: &LateContext<'_>, expr: &'hir hir::Expr<'hir>, callback: F) -> bool
70 F: FnMut(&'hir hir::Expr<'hir>) -> bool,
78 struct WithStmtGuarg<'a, F> {
79 val: &'a mut RetFinder<F>,
83 impl<F> RetFinder<F> {
84 fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> {
85 let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt);
93 impl<F> std::ops::Deref for WithStmtGuarg<'_, F> {
94 type Target = RetFinder<F>;
96 fn deref(&self) -> &Self::Target {
101 impl<F> std::ops::DerefMut for WithStmtGuarg<'_, F> {
102 fn deref_mut(&mut self) -> &mut Self::Target {
107 impl<F> Drop for WithStmtGuarg<'_, F> {
109 self.val.in_stmt = self.prev_in_stmt;
113 impl<'hir, F: FnMut(&'hir hir::Expr<'hir>) -> bool> intravisit::Visitor<'hir> for RetFinder<F> {
114 type Map = Map<'hir>;
116 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
117 intravisit::NestedVisitorMap::None
120 fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'_>) {
121 intravisit::walk_stmt(&mut *self.inside_stmt(true), stmt);
124 fn visit_expr(&mut self, expr: &'hir hir::Expr<'_>) {
130 hir::ExprKind::Ret(Some(expr)) => self.inside_stmt(false).visit_expr(expr),
131 _ => intravisit::walk_expr(self, expr),
135 hir::ExprKind::If(cond, then, else_opt) => {
136 self.inside_stmt(true).visit_expr(cond);
137 self.visit_expr(then);
138 if let Some(el) = else_opt {
142 hir::ExprKind::Match(cond, arms, _) => {
143 self.inside_stmt(true).visit_expr(cond);
145 self.visit_expr(arm.body);
148 hir::ExprKind::Block(..) => intravisit::walk_expr(self, expr),
149 hir::ExprKind::Ret(Some(expr)) => self.visit_expr(expr),
150 _ => self.failed |= !(self.cb)(expr),
156 !contains_try(expr) && {
157 let mut ret_finder = RetFinder {
162 ret_finder.visit_expr(expr);
167 /// A type which can be visited.
168 pub trait Visitable<'tcx> {
169 /// Calls the corresponding `visit_*` function on the visitor.
170 fn visit<V: Visitor<'tcx>>(self, visitor: &mut V);
172 macro_rules! visitable_ref {
173 ($t:ident, $f:ident) => {
174 impl Visitable<'tcx> for &'tcx $t<'tcx> {
175 fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) {
181 visitable_ref!(Arm, visit_arm);
182 visitable_ref!(Block, visit_block);
183 visitable_ref!(Body, visit_body);
184 visitable_ref!(Expr, visit_expr);
185 visitable_ref!(Stmt, visit_stmt);
187 // impl<'tcx, I: IntoIterator> Visitable<'tcx> for I
189 // I::Item: Visitable<'tcx>,
191 // fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) {
198 /// Checks if the given resolved path is used in the given body.
199 pub fn is_res_used(cx: &LateContext<'_>, res: Res, body: BodyId) -> bool {
200 let mut found = false;
201 expr_visitor(cx, |e| {
206 if let ExprKind::Path(p) = &e.kind {
207 if cx.qpath_res(p, e.hir_id) == res {
213 .visit_expr(&cx.tcx.hir().body(body).value);
217 /// Checks if the given local is used.
218 pub fn is_local_used(cx: &LateContext<'tcx>, visitable: impl Visitable<'tcx>, id: HirId) -> bool {
219 let mut is_used = false;
220 let mut visitor = expr_visitor(cx, |expr| {
222 is_used = path_to_local_id(expr, id);
226 visitable.visit(&mut visitor);
231 /// Checks if the given expression is a constant.
232 pub fn is_const_evaluatable(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool {
234 cx: &'a LateContext<'tcx>,
237 impl<'tcx> Visitor<'tcx> for V<'_, 'tcx> {
238 type Map = Map<'tcx>;
239 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
240 NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
243 fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
248 ExprKind::ConstBlock(_) => return,
251 kind: ExprKind::Path(ref p),
258 .qpath_res(p, hir_id)
260 .map_or(false, |id| self.cx.tcx.is_const_fn_raw(id)) => {},
261 ExprKind::MethodCall(..)
265 .type_dependent_def_id(e.hir_id)
266 .map_or(false, |id| self.cx.tcx.is_const_fn_raw(id)) => {},
267 ExprKind::Binary(_, lhs, rhs)
268 if self.cx.typeck_results().expr_ty(lhs).peel_refs().is_primitive_ty()
269 && self.cx.typeck_results().expr_ty(rhs).peel_refs().is_primitive_ty() => {},
270 ExprKind::Unary(UnOp::Deref, e) if self.cx.typeck_results().expr_ty(e).is_ref() => (),
271 ExprKind::Unary(_, e) if self.cx.typeck_results().expr_ty(e).peel_refs().is_primitive_ty() => (),
272 ExprKind::Index(base, _)
274 self.cx.typeck_results().expr_ty(base).peel_refs().kind(),
275 ty::Slice(_) | ty::Array(..)
277 ExprKind::Path(ref p)
279 self.cx.qpath_res(p, e.hir_id),
282 | DefKind::AssocConst
284 | DefKind::ConstParam
294 | ExprKind::Block(..)
296 | ExprKind::DropTemps(_)
297 | ExprKind::Field(..)
301 | ExprKind::Match(..)
302 | ExprKind::Repeat(..)
303 | ExprKind::Struct(..)
305 | ExprKind::Type(..) => (),
308 self.is_const = false;
316 let mut v = V { cx, is_const: true };