]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/main_recursion.rs
modify code
[rust.git] / clippy_lints / src / main_recursion.rs
index 7854873509eac355d66130da3440c6b37d58157d..fad8fa467d4b80f23917e969d0a865fdde3a6906 100644 (file)
@@ -1,24 +1,26 @@
-use rustc_hir::{Crate, Expr, ExprKind, QPath};
+use clippy_utils::diagnostics::span_lint_and_help;
+use clippy_utils::source::snippet;
+use clippy_utils::{is_entrypoint_fn, is_no_std_crate};
+use if_chain::if_chain;
+use rustc_hir::{Expr, ExprKind, QPath};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_tool_lint, impl_lint_pass};
 
-use crate::utils::{is_entrypoint_fn, is_no_std_crate, snippet, span_lint_and_help};
-use if_chain::if_chain;
-
 declare_clippy_lint! {
-    /// **What it does:** Checks for recursion using the entrypoint.
+    /// ### 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]),
+    /// ### Why is this bad?
+    /// Apart from special setups (which we could detect following attributes like #![no_std]),
     /// recursing into main() seems like an unintuitive antipattern we should be able to detect.
     ///
-    /// **Known problems:** None.
-    ///
-    /// **Example:**
+    /// ### Example
     /// ```no_run
     /// fn main() {
     ///     main();
     /// }
     /// ```
+    #[clippy::version = "1.38.0"]
     pub MAIN_RECURSION,
     style,
     "recursion using the entrypoint"
@@ -31,20 +33,19 @@ pub struct MainRecursion {
 
 impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
 
-impl LateLintPass<'_, '_> for MainRecursion {
-    fn check_crate(&mut self, _: &LateContext<'_, '_>, krate: &Crate<'_>) {
-        self.has_no_std_attr = is_no_std_crate(krate);
+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<'_>) {
+    fn check_expr_post(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
         if self.has_no_std_attr {
             return;
         }
 
         if_chain! {
             if let ExprKind::Call(func, _) = &expr.kind;
-            if let ExprKind::Path(path) = &func.kind;
-            if let QPath::Resolved(_, path) = &path;
+            if let ExprKind::Path(QPath::Resolved(_, path)) = &func.kind;
             if let Some(def_id) = path.res.opt_def_id();
             if is_entrypoint_fn(cx, def_id);
             then {
@@ -53,6 +54,7 @@ fn check_expr_post(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
                     MAIN_RECURSION,
                     func.span,
                     &format!("recursing into entrypoint `{}`", snippet(cx, func.span, "main")),
+                    None,
                     "consider using another function for this recursion"
                 )
             }