Skip to content

Commit fd7f79f

Browse files
fix: main_recursion enable lint in no_std crates and fix broken tests
1 parent 6110c80 commit fd7f79f

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

clippy_lints/src/main_recursion.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::is_entrypoint_fn;
23
use clippy_utils::source::snippet;
3-
use clippy_utils::{is_entrypoint_fn, is_no_std_crate};
44
use rustc_hir::{Expr, ExprKind, QPath};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::impl_lint_pass;
@@ -26,22 +26,12 @@ declare_clippy_lint! {
2626
}
2727

2828
#[derive(Default)]
29-
pub struct MainRecursion {
30-
has_no_std_attr: bool,
31-
}
29+
pub struct MainRecursion;
3230

3331
impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
3432

3533
impl LateLintPass<'_> for MainRecursion {
36-
fn check_crate(&mut self, cx: &LateContext<'_>) {
37-
self.has_no_std_attr = is_no_std_crate(cx);
38-
}
39-
4034
fn check_expr_post(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
41-
if self.has_no_std_attr {
42-
return;
43-
}
44-
4535
if let ExprKind::Call(func, []) = &expr.kind
4636
&& let ExprKind::Path(QPath::Resolved(_, path)) = &func.kind
4737
&& let Some(def_id) = path.res.opt_def_id()
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
//@check-pass
21
//@ignore-target: apple
3-
42
#![feature(rustc_attrs)]
5-
63
#[warn(clippy::main_recursion)]
74
#[allow(unconditional_recursion)]
85
#[rustc_main]
96
fn a() {
10-
println!("Hello, World!");
117
a();
8+
//~^ main_recursion
129
}
10+
11+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: recursing into entrypoint `a`
2+
--> tests/ui/crate_level_checks/entrypoint_recursion.rs:7:5
3+
|
4+
LL | a();
5+
| ^
6+
|
7+
= help: consider using another function for this recursion
8+
= note: `-D clippy::main-recursion` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::main_recursion)]`
10+
11+
error: aborting due to 1 previous error
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@compile-flags: -Cpanic=abort
2+
#![no_std]
3+
#[warn(clippy::main_recursion)]
4+
#[allow(unconditional_recursion)]
5+
fn main() {
6+
main();
7+
//~^ main_recursion
8+
}
9+
10+
#[panic_handler]
11+
fn panic(_info: &core::panic::PanicInfo) -> ! {
12+
loop {}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: recursing into entrypoint `main`
2+
--> tests/ui/crate_level_checks/no_std_main_recursion.rs:6:5
3+
|
4+
LL | main();
5+
| ^^^^
6+
|
7+
= help: consider using another function for this recursion
8+
= note: `-D clippy::main-recursion` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::main_recursion)]`
10+
11+
error: aborting due to 1 previous error
12+

0 commit comments

Comments
 (0)