-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4203 - Urriel:feat/333_calling_main, r=flip1995
Add recursion check on main function Changes: - Add MainRecursion lint to Clippy - Check for no-std setup fixes #333 changelog: Add `main_recursion` lint
- Loading branch information
Showing
10 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use rustc::hir::{Crate, Expr, ExprKind, QPath}; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_tool_lint, impl_lint_pass}; | ||
use syntax::symbol::sym; | ||
|
||
use crate::utils::{is_entrypoint_fn, snippet, span_help_and_lint}; | ||
use if_chain::if_chain; | ||
|
||
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 antipattern we should be able to detect. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```no_run | ||
/// fn main() { | ||
/// main(); | ||
/// } | ||
/// ``` | ||
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, _: &LateContext<'_, '_>, krate: &Crate) { | ||
self.has_no_std_attr = krate.attrs.iter().any(|attr| attr.path == sym::no_std); | ||
} | ||
|
||
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.node; | ||
if let ExprKind::Path(path) = &func.node; | ||
if let QPath::Resolved(_, path) = &path; | ||
if let Some(def_id) = path.res.opt_def_id(); | ||
if is_entrypoint_fn(cx, def_id); | ||
then { | ||
span_help_and_lint( | ||
cx, | ||
MAIN_RECURSION, | ||
func.span, | ||
&format!("recursing into entrypoint `{}`", snippet(cx, func.span, "main")), | ||
"consider using another function for this recursion" | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// ignore-macos | ||
// ignore-windows | ||
|
||
#![feature(main)] | ||
|
||
#[warn(clippy::main_recursion)] | ||
#[allow(unconditional_recursion)] | ||
#[main] | ||
fn a() { | ||
println!("Hello, World!"); | ||
a(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: recursing into entrypoint `a` | ||
--> $DIR/entrypoint_recursion.rs:11:5 | ||
| | ||
LL | a(); | ||
| ^ | ||
| | ||
= note: `-D clippy::main-recursion` implied by `-D warnings` | ||
= help: consider using another function for this recursion | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![feature(lang_items, link_args, start, libc)] | ||
#![link_args = "-nostartfiles"] | ||
#![no_std] | ||
|
||
use core::panic::PanicInfo; | ||
use core::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
static N: AtomicUsize = AtomicUsize::new(0); | ||
|
||
#[warn(clippy::main_recursion)] | ||
#[start] | ||
fn main(argc: isize, argv: *const *const u8) -> isize { | ||
let x = N.load(Ordering::Relaxed); | ||
N.store(x + 1, Ordering::Relaxed); | ||
|
||
if x < 3 { | ||
main(argc, argv); | ||
} | ||
|
||
0 | ||
} | ||
|
||
#[allow(clippy::empty_loop)] | ||
#[panic_handler] | ||
fn panic(_info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[warn(clippy::main_recursion)] | ||
#[allow(unconditional_recursion)] | ||
fn main() { | ||
println!("Hello, World!"); | ||
main(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: recursing into entrypoint `main` | ||
--> $DIR/std_main_recursion.rs:5:5 | ||
| | ||
LL | main(); | ||
| ^^^^ | ||
| | ||
= note: `-D clippy::main-recursion` implied by `-D warnings` | ||
= help: consider using another function for this recursion | ||
|
||
error: aborting due to previous error | ||
|