|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use rustc_hir::def::{DefKind, Res}; |
| 3 | +use rustc_hir::{Expr, ExprKind, QPath}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::declare_lint_pass; |
| 6 | + |
| 7 | +declare_clippy_lint! { |
| 8 | + /// ### What it does |
| 9 | + /// Checks for functions that call themselves from their body. |
| 10 | + /// ### Why restrict this? |
| 11 | + /// In Safety Critical contexts, recursive calls can lead to catastrophic crashes if they happen to overflow |
| 12 | + /// the stack. Recursive calls must therefore be tightly vetted. |
| 13 | + /// ### Notes |
| 14 | + /// |
| 15 | + /// #### Control Flow |
| 16 | + /// This lint only checks for the existence of recursive calls; it doesn't discriminate based on conditional |
| 17 | + /// control flow. Recursive calls that are considered safe should instead be vetted and documented accordingly. |
| 18 | + /// |
| 19 | + /// #### How to vet recursive calls |
| 20 | + /// It is recommended that this lint be used in `deny` mode, together with #. |
| 21 | + /// |
| 22 | + /// Once that is done, recursive calls can be vetted accordingly: |
| 23 | + /// |
| 24 | + /// ```no_run |
| 25 | + /// fn i_call_myself_in_a_bounded_way(bound: u8) { |
| 26 | + /// if bound > 0 { |
| 27 | + /// #[expect( |
| 28 | + /// clippy::direct_recursion, |
| 29 | + /// reason = "Author has audited this function and determined that its recursive call is fine." |
| 30 | + /// )] |
| 31 | + /// i_call_myself_in_a_bounded_way(bound - 1); |
| 32 | + /// } |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + /// |
| 36 | + /// Note the use of an `expect` attribute and a `reason` to go along with it. |
| 37 | + /// |
| 38 | + /// * The `expect` attribute (instead of `allow`) ensures that the lint being allowed is enabled. This serves as a |
| 39 | + /// double-check of this lint being used where the author believes it is active. |
| 40 | + /// * The `reason` field is required by the `clippy::allow_attributes_without_reason` lint. This is very useful for ensuring |
| 41 | + /// that vetting work is documented. |
| 42 | + /// |
| 43 | + /// Recursive calls that are vetted to be correct should always be annotated in such a way. |
| 44 | + #[clippy::version = "1.89.0"] |
| 45 | + pub DIRECT_RECURSION, |
| 46 | + restriction, |
| 47 | + "functions shall not call themselves directly" |
| 48 | +} |
| 49 | +declare_lint_pass!(DirectRecursion => [DIRECT_RECURSION]); |
| 50 | + |
| 51 | +impl<'tcx> LateLintPass<'tcx> for DirectRecursion { |
| 52 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 53 | + if let ExprKind::Call(call_expr, _) = &expr.kind |
| 54 | + && let body_def_id = cx.tcx.hir_enclosing_body_owner(call_expr.hir_id) |
| 55 | + && let ExprKind::Path(c_expr_path) = call_expr.kind |
| 56 | + && let QPath::Resolved(_lhs, path) = c_expr_path |
| 57 | + && let Res::Def(DefKind::Fn, fn_path_id) = path.res |
| 58 | + && fn_path_id == body_def_id.into() |
| 59 | + { |
| 60 | + span_lint( |
| 61 | + cx, |
| 62 | + DIRECT_RECURSION, |
| 63 | + expr.span, |
| 64 | + "this function contains a call to itself", |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments