1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::source::snippet;
3 use if_chain::if_chain;
4 use rustc_hir::{Crate, Expr, ExprKind, QPath};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_tool_lint, impl_lint_pass};
8 use crate::utils::{is_entrypoint_fn, is_no_std_crate};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for recursion using the entrypoint.
13 /// **Why is this bad?** Apart from special setups (which we could detect following attributes like #![no_std]),
14 /// recursing into main() seems like an unintuitive antipattern we should be able to detect.
16 /// **Known problems:** None.
26 "recursion using the entrypoint"
30 pub struct MainRecursion {
31 has_no_std_attr: bool,
34 impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
36 impl LateLintPass<'_> for MainRecursion {
37 fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
38 self.has_no_std_attr = is_no_std_crate(cx);
41 fn check_expr_post(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
42 if self.has_no_std_attr {
47 if let ExprKind::Call(func, _) = &expr.kind;
48 if let ExprKind::Path(QPath::Resolved(_, path)) = &func.kind;
49 if let Some(def_id) = path.res.opt_def_id();
50 if is_entrypoint_fn(cx, def_id);
56 &format!("recursing into entrypoint `{}`", snippet(cx, func.span, "main")),
58 "consider using another function for this recursion"