about summary refs log tree commit diff
path: root/clippy_lints/src/main_recursion.rs
blob: 01ea2f5debea823a9a6dd5f70569d8e57f64154a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::source::snippet;
use clippy_utils::{is_entrypoint_fn, is_no_std_crate};
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;

declare_clippy_lint! {
    /// ### What it does
    /// Checks for recursion using the entrypoint.
    ///
    /// ### Why is this bad?
    /// Apart from special setups (which we could detect following attributes like #![no_std]),
    /// recursing into main() seems like an unintuitive anti-pattern we should be able to detect.
    ///
    /// ### Example
    /// ```no_run
    /// fn main() {
    ///     main();
    /// }
    /// ```
    #[clippy::version = "1.38.0"]
    pub MAIN_RECURSION,
    style,
    "recursion using the entrypoint"
}

#[derive(Default)]
pub struct MainRecursion {
    has_no_std_attr: bool,
}

impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);

impl LateLintPass<'_> for MainRecursion {
    fn check_crate(&mut self, cx: &LateContext<'_>) {
        self.has_no_std_attr = is_no_std_crate(cx);
    }

    fn check_expr_post(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
        if self.has_no_std_attr {
            return;
        }

        if let ExprKind::Call(func, []) = &expr.kind
            && let ExprKind::Path(QPath::Resolved(_, path)) = &func.kind
            && let Some(def_id) = path.res.opt_def_id()
            && is_entrypoint_fn(cx, def_id)
        {
            span_lint_and_help(
                cx,
                MAIN_RECURSION,
                func.span,
                format!("recursing into entrypoint `{}`", snippet(cx, func.span, "main")),
                None,
                "consider using another function for this recursion",
            );
        }
    }
}