1 use rustc_hir::{Crate, Expr, ExprKind, QPath};
2 use rustc_lint::{LateContext, LateLintPass};
3 use rustc_session::{declare_tool_lint, impl_lint_pass};
5 use crate::utils::{is_entrypoint_fn, is_no_std_crate, snippet, span_lint_and_help};
6 use if_chain::if_chain;
9 /// **What it does:** Checks for recursion using the entrypoint.
11 /// **Why is this bad?** Apart from special setups (which we could detect following attributes like #![no_std]),
12 /// recursing into main() seems like an unintuitive antipattern we should be able to detect.
14 /// **Known problems:** None.
24 "recursion using the entrypoint"
28 pub struct MainRecursion {
29 has_no_std_attr: bool,
32 impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
34 impl LateLintPass<'_, '_> for MainRecursion {
35 fn check_crate(&mut self, _: &LateContext<'_, '_>, krate: &Crate<'_>) {
36 self.has_no_std_attr = is_no_std_crate(krate);
39 fn check_expr_post(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
40 if self.has_no_std_attr {
45 if let ExprKind::Call(func, _) = &expr.kind;
46 if let ExprKind::Path(path) = &func.kind;
47 if let QPath::Resolved(_, path) = &path;
48 if let Some(def_id) = path.res.opt_def_id();
49 if is_entrypoint_fn(cx, def_id);
55 &format!("recursing into entrypoint `{}`", snippet(cx, func.span, "main")),
57 "consider using another function for this recursion"