1 use rustc::hir::{Crate, Expr, ExprKind, QPath};
2 use rustc::impl_lint_pass;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc_session::declare_tool_lint;
5 use syntax::ast::AttrKind;
6 use syntax::symbol::sym;
8 use crate::utils::{is_entrypoint_fn, snippet, span_help_and_lint};
9 use if_chain::if_chain;
11 declare_clippy_lint! {
12 /// **What it does:** Checks for recursion using the entrypoint.
14 /// **Why is this bad?** Apart from special setups (which we could detect following attributes like #![no_std]),
15 /// recursing into main() seems like an unintuitive antipattern we should be able to detect.
17 /// **Known problems:** None.
27 "recursion using the entrypoint"
31 pub struct MainRecursion {
32 has_no_std_attr: bool,
35 impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
37 impl LateLintPass<'_, '_> for MainRecursion {
38 fn check_crate(&mut self, _: &LateContext<'_, '_>, krate: &Crate) {
39 self.has_no_std_attr = krate.attrs.iter().any(|attr| {
40 if let AttrKind::Normal(ref attr) = attr.kind {
41 attr.path == sym::no_std
48 fn check_expr_post(&mut self, cx: &LateContext<'_, '_>, expr: &Expr) {
49 if self.has_no_std_attr {
54 if let ExprKind::Call(func, _) = &expr.kind;
55 if let ExprKind::Path(path) = &func.kind;
56 if let QPath::Resolved(_, path) = &path;
57 if let Some(def_id) = path.res.opt_def_id();
58 if is_entrypoint_fn(cx, def_id);
64 &format!("recursing into entrypoint `{}`", snippet(cx, func.span, "main")),
65 "consider using another function for this recursion"