Add recursion check on main function

Changes:
- Add MainRecursion lint to clippy
- Check for no-std setup

fixes #333
This commit is contained in:
Vincent Dal Maso
2019-06-13 10:58:35 +02:00
committed by flip1995
parent ca6a9beb31
commit 4eab691db6
6 changed files with 104 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
use syntax::ast::{Crate, Expr, ExprKind};
use syntax::symbol::sym;
use rustc::lint::{LintArray, LintPass, EarlyLintPass, EarlyContext};
use rustc::{declare_tool_lint, impl_lint_pass};
use if_chain::if_chain;
use crate::utils::span_help_and_lint;
declare_clippy_lint! {
pub MAIN_RECURSION,
pedantic,
"function named `foo`, which is not a descriptive name"
}
pub struct MainRecursion {
has_no_std_attr: bool
}
impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
impl MainRecursion {
pub fn new() -> MainRecursion {
MainRecursion {
has_no_std_attr: false
}
}
}
impl EarlyLintPass for MainRecursion {
fn check_crate(&mut self, _: &EarlyContext<'_>, krate: &Crate) {
self.has_no_std_attr = krate.attrs.iter().any(|attr| attr.path == sym::no_std);
}
fn check_expr(&mut self, cx: &EarlyContext<'_>, 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 *path == sym::main;
then {
span_help_and_lint(
cx,
MAIN_RECURSION,
expr.span,
"You are recursing into main()",
"Consider using another function for this recursion"
)
}
}
}
}