Make CollapsibleIf an EarlyLintPass

It doesn't need any `hir` feature and `ast` is much more stable.
This commit is contained in:
mcarton 2016-06-21 23:17:18 +02:00
parent 7fa38f6787
commit f37c9adbd9
No known key found for this signature in database
GPG Key ID: 5E427C794CBA45E8
2 changed files with 16 additions and 17 deletions

View File

@ -13,9 +13,9 @@
//! This lint is **warn** by default
use rustc::lint::*;
use rustc::hir::*;
use std::borrow::Cow;
use syntax::codemap::Spanned;
use syntax::ast;
use utils::{in_macro, snippet, snippet_block, span_lint_and_then};
@ -45,23 +45,22 @@ fn get_lints(&self) -> LintArray {
}
}
impl LateLintPass for CollapsibleIf {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
impl EarlyLintPass for CollapsibleIf {
fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
if !in_macro(cx, expr.span) {
check_if(cx, expr)
}
}
}
fn check_if(cx: &LateContext, e: &Expr) {
if let ExprIf(ref check, ref then, ref else_) = e.node {
fn check_if(cx: &EarlyContext, e: &ast::Expr) {
if let ast::ExprKind::If(ref check, ref then, ref else_) = e.node {
if let Some(ref else_) = *else_ {
if_let_chain! {[
let ExprBlock(ref block) = else_.node,
let ast::ExprKind::Block(ref block) = else_.node,
block.stmts.is_empty(),
block.rules == BlockCheckMode::DefaultBlock,
let Some(ref else_) = block.expr,
let ExprIf(_, _, _) = else_.node
let ast::ExprKind::If(_, _, _) = else_.node
], {
span_lint_and_then(cx,
COLLAPSIBLE_IF,
@ -70,7 +69,7 @@ fn check_if(cx: &LateContext, e: &Expr) {
db.span_suggestion(block.span, "try", snippet_block(cx, else_.span, "..").into_owned());
});
}}
} else if let Some(&Expr { node: ExprIf(ref check_inner, ref content, None), span: sp, .. }) =
} else if let Some(&ast::Expr { node: ast::ExprKind::If(ref check_inner, ref content, None), span: sp, .. }) =
single_stmt_of_block(then) {
if e.span.expn_id != sp.expn_id {
return;
@ -87,14 +86,14 @@ fn check_if(cx: &LateContext, e: &Expr) {
}
}
fn requires_brackets(e: &Expr) -> bool {
fn requires_brackets(e: &ast::Expr) -> bool {
match e.node {
ExprBinary(Spanned { node: n, .. }, _, _) if n == BiEq => false,
ast::ExprKind::Binary(Spanned { node: n, .. }, _, _) if n == ast::BinOpKind::Eq => false,
_ => true,
}
}
fn check_to_string(cx: &LateContext, e: &Expr) -> Cow<'static, str> {
fn check_to_string(cx: &EarlyContext, e: &ast::Expr) -> Cow<'static, str> {
if requires_brackets(e) {
format!("({})", snippet(cx, e.span, "..")).into()
} else {
@ -102,9 +101,9 @@ fn check_to_string(cx: &LateContext, e: &Expr) -> Cow<'static, str> {
}
}
fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
fn single_stmt_of_block(block: &ast::Block) -> Option<&ast::Expr> {
if block.stmts.len() == 1 && block.expr.is_none() {
if let StmtExpr(ref expr, _) = block.stmts[0].node {
if let ast::StmtKind::Expr(ref expr, _) = block.stmts[0].node {
single_stmt_of_expr(expr)
} else {
None
@ -120,8 +119,8 @@ fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
}
}
fn single_stmt_of_expr(expr: &Expr) -> Option<&Expr> {
if let ExprBlock(ref block) = expr.node {
fn single_stmt_of_expr(expr: &ast::Expr) -> Option<&ast::Expr> {
if let ast::ExprKind::Block(ref block) = expr.node {
single_stmt_of_block(block)
} else {
Some(expr)

View File

@ -189,7 +189,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
reg.register_late_lint_pass(box len_zero::LenZero);
reg.register_late_lint_pass(box misc::CmpOwned);
reg.register_late_lint_pass(box attrs::AttrPass);
reg.register_late_lint_pass(box collapsible_if::CollapsibleIf);
reg.register_early_lint_pass(box collapsible_if::CollapsibleIf);
reg.register_late_lint_pass(box block_in_if_condition::BlockInIfCondition);
reg.register_late_lint_pass(box misc::ModuloOne);
reg.register_late_lint_pass(box unicode::Unicode);