|
| 1 | +use clippy_utils::diagnostics::{span_lint, span_lint_hir}; |
| 2 | +use clippy_utils::get_parent_expr; |
| 3 | +use rustc_hir::def::{DefKind, Res}; |
| 4 | +use rustc_hir::{Body, Expr, ExprKind, QPath}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty::Instance; |
| 7 | +use rustc_session::impl_lint_pass; |
| 8 | +use rustc_span::def_id::DefId; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for functions that call themselves from their body. |
| 13 | + /// |
| 14 | + /// ### Why restrict this? |
| 15 | + /// In Safety Critical contexts, recursive calls can lead to catastrophic |
| 16 | + /// crashes if they happen to overflow the stack. |
| 17 | + /// |
| 18 | + /// In most contexts, this is not an issue, so this lint is allow-by-default. |
| 19 | + /// |
| 20 | + /// ### Notes |
| 21 | + /// |
| 22 | + /// #### Triggers |
| 23 | + /// There are two things that trigger this lint: |
| 24 | + /// - Function calls from a function (or method) to itself, |
| 25 | + /// - Function pointer bindings from a function (or method) to itself. |
| 26 | + /// |
| 27 | + /// #### Independent of control flow |
| 28 | + /// This lint triggers whenever the conditions above are met, regardless of |
| 29 | + /// control flow and other such constructs. |
| 30 | + /// |
| 31 | + /// #### Blessing a recursive call |
| 32 | + /// The user may choose to bless a recursive call or binding using the |
| 33 | + /// attribute #[clippy::allowed_recursion] |
| 34 | + /// |
| 35 | + /// #### Indirect calls |
| 36 | + /// This lint **does not** detect indirect recursive calls. |
| 37 | + /// |
| 38 | + /// ### Examples |
| 39 | + /// This function will trigger the lint: |
| 40 | + /// ```no_run |
| 41 | + /// fn i_call_myself_in_a_bounded_way(bound: u8) { |
| 42 | + /// if bound > 0 { |
| 43 | + /// // This line will trigger the lint |
| 44 | + /// i_call_myself_in_a_bounded_way(bound - 1); |
| 45 | + /// } |
| 46 | + /// } |
| 47 | + /// ``` |
| 48 | + /// Using #[clippy::allowed_recursion] lets it pass: |
| 49 | + /// ```no_run |
| 50 | + /// fn i_call_myself_in_a_bounded_way(bound: u8) { |
| 51 | + /// if bound > 0 { |
| 52 | + /// #[clippy::allowed_recursion] |
| 53 | + /// i_call_myself_in_a_bounded_way(bound - 1); |
| 54 | + /// } |
| 55 | + /// } |
| 56 | + /// ``` |
| 57 | + /// This triggers the lint when `fibo` is bound to a function pointer |
| 58 | + /// inside `fibo`'s body |
| 59 | + /// ```no_run |
| 60 | + /// fn fibo(a: u32) -> u32 { |
| 61 | + /// if a < 2 { a } else { (a - 2..a).map(fibo).sum() } |
| 62 | + /// } |
| 63 | + /// ``` |
| 64 | + #[clippy::version = "1.89.0"] |
| 65 | + pub DIRECT_RECURSION, |
| 66 | + restriction, |
| 67 | + "functions shall not call themselves directly" |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Default)] |
| 71 | +pub struct DirectRecursion { |
| 72 | + fn_id_stack: Vec<DefId>, |
| 73 | +} |
| 74 | + |
| 75 | +impl_lint_pass!(DirectRecursion => [DIRECT_RECURSION]); |
| 76 | + |
| 77 | +impl<'tcx> LateLintPass<'tcx> for DirectRecursion { |
| 78 | + /// Whenever we enter a Body, we push its owner's `DefId` into the stack |
| 79 | + fn check_body(&mut self, cx: &LateContext<'tcx>, body: &Body<'_>) { |
| 80 | + self.fn_id_stack |
| 81 | + .push(cx.tcx.hir_body_owner_def_id(body.id()).to_def_id()); |
| 82 | + } |
| 83 | + |
| 84 | + /// We then revert this when we exit said `Body` |
| 85 | + fn check_body_post(&mut self, _: &LateContext<'tcx>, _: &Body<'_>) { |
| 86 | + _ = self.fn_id_stack.pop(); |
| 87 | + } |
| 88 | + |
| 89 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 90 | + match expr.kind { |
| 91 | + ExprKind::MethodCall(_, _, _, _) => { |
| 92 | + let typeck = cx.typeck_results(); |
| 93 | + if let Some(basic_id) = typeck.type_dependent_def_id(expr.hir_id) { |
| 94 | + // This finds default Trait implementations of methods |
| 95 | + if self.fn_id_stack.contains(&basic_id) { |
| 96 | + span_lint(cx, DIRECT_RECURSION, expr.span, "this method contains a call to itself"); |
| 97 | + } |
| 98 | + // Whereas this finds non-default implementations |
| 99 | + else if let args = typeck.node_args(expr.hir_id) |
| 100 | + && let Ok(Some(fn_def)) = Instance::try_resolve(cx.tcx, cx.typing_env(), basic_id, args) |
| 101 | + && let type_resolved_def_id = fn_def.def_id() |
| 102 | + && self.fn_id_stack.contains(&type_resolved_def_id) |
| 103 | + { |
| 104 | + span_lint(cx, DIRECT_RECURSION, expr.span, "this method contains a call to itself"); |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + ExprKind::Path(QPath::TypeRelative(_, _)) => { |
| 109 | + // I'm still not sure this is proper. |
| 110 | + // It definitely finds the right `DefId`, though. |
| 111 | + let typeck = cx.typeck_results(); |
| 112 | + if let Some(id) = typeck.type_dependent_def_id(expr.hir_id) |
| 113 | + && let args = typeck.node_args(expr.hir_id) |
| 114 | + && let Ok(Some(fn_def)) = Instance::try_resolve(cx.tcx, cx.typing_env(), id, args) |
| 115 | + { |
| 116 | + let type_resolved_def_id = fn_def.def_id(); |
| 117 | + |
| 118 | + if self.fn_id_stack.contains(&type_resolved_def_id) { |
| 119 | + emit_lint(cx, expr); |
| 120 | + } |
| 121 | + } |
| 122 | + }, |
| 123 | + // This branch takes care of finding bindings of function and method names |
| 124 | + // into fn pointers. |
| 125 | + ExprKind::Path(QPath::Resolved(_, path)) => { |
| 126 | + // Now we know that this Path is fully resolved. |
| 127 | + // We now must check if it points to a function or a method's definition. |
| 128 | + if let Res::Def(DefKind::Fn | DefKind::AssocFn, fn_path_id) = path.res |
| 129 | + // 1) Now we know that the path we've found is of a function or method definition. |
| 130 | + // |
| 131 | + // 2) We will now check if it corresponds to the path of a function we're inside |
| 132 | + // of. |
| 133 | + // |
| 134 | + // 3) Thankfully, we've kept track of the functions that surround us, in |
| 135 | + //`self.fn_id_stack`. |
| 136 | + // |
| 137 | + // 4) If the path that we've captured from `expr` coincides with one of the functions |
| 138 | + // in the stack, then we know we have a recursive loop. |
| 139 | + |
| 140 | + && self.fn_id_stack.contains(&fn_path_id) |
| 141 | + { |
| 142 | + emit_lint(cx, expr); |
| 143 | + } |
| 144 | + }, |
| 145 | + _ => {}, |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +fn emit_lint<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 151 | + let (node_id, span, msg) = if let Some(parent_expr) = get_parent_expr(cx, expr) |
| 152 | + && let ExprKind::Call(func, _) = parent_expr.kind |
| 153 | + && func.hir_id == expr.hir_id |
| 154 | + { |
| 155 | + ( |
| 156 | + parent_expr.hir_id, |
| 157 | + parent_expr.span, |
| 158 | + "this function contains a call to itself", |
| 159 | + ) |
| 160 | + } else { |
| 161 | + (expr.hir_id, expr.span, "this function creates a reference to itself") |
| 162 | + }; |
| 163 | + span_lint_hir(cx, DIRECT_RECURSION, node_id, span, msg); |
| 164 | +} |
0 commit comments