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 //! This lint is **warn** by default
use rustc::lint::*; use rustc::lint::*;
use rustc::hir::*;
use std::borrow::Cow; use std::borrow::Cow;
use syntax::codemap::Spanned; use syntax::codemap::Spanned;
use syntax::ast;
use utils::{in_macro, snippet, snippet_block, span_lint_and_then}; use utils::{in_macro, snippet, snippet_block, span_lint_and_then};
@ -45,23 +45,22 @@ impl LintPass for CollapsibleIf {
} }
} }
impl LateLintPass for CollapsibleIf { impl EarlyLintPass for CollapsibleIf {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
if !in_macro(cx, expr.span) { if !in_macro(cx, expr.span) {
check_if(cx, expr) check_if(cx, expr)
} }
} }
} }
fn check_if(cx: &LateContext, e: &Expr) { fn check_if(cx: &EarlyContext, e: &ast::Expr) {
if let ExprIf(ref check, ref then, ref else_) = e.node { if let ast::ExprKind::If(ref check, ref then, ref else_) = e.node {
if let Some(ref else_) = *else_ { if let Some(ref else_) = *else_ {
if_let_chain! {[ if_let_chain! {[
let ExprBlock(ref block) = else_.node, let ast::ExprKind::Block(ref block) = else_.node,
block.stmts.is_empty(), block.stmts.is_empty(),
block.rules == BlockCheckMode::DefaultBlock,
let Some(ref else_) = block.expr, let Some(ref else_) = block.expr,
let ExprIf(_, _, _) = else_.node let ast::ExprKind::If(_, _, _) = else_.node
], { ], {
span_lint_and_then(cx, span_lint_and_then(cx,
COLLAPSIBLE_IF, 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()); 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) { single_stmt_of_block(then) {
if e.span.expn_id != sp.expn_id { if e.span.expn_id != sp.expn_id {
return; 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 { match e.node {
ExprBinary(Spanned { node: n, .. }, _, _) if n == BiEq => false, ast::ExprKind::Binary(Spanned { node: n, .. }, _, _) if n == ast::BinOpKind::Eq => false,
_ => true, _ => 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) { if requires_brackets(e) {
format!("({})", snippet(cx, e.span, "..")).into() format!("({})", snippet(cx, e.span, "..")).into()
} else { } 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 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) single_stmt_of_expr(expr)
} else { } else {
None None
@ -120,8 +119,8 @@ fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
} }
} }
fn single_stmt_of_expr(expr: &Expr) -> Option<&Expr> { fn single_stmt_of_expr(expr: &ast::Expr) -> Option<&ast::Expr> {
if let ExprBlock(ref block) = expr.node { if let ast::ExprKind::Block(ref block) = expr.node {
single_stmt_of_block(block) single_stmt_of_block(block)
} else { } else {
Some(expr) 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 len_zero::LenZero);
reg.register_late_lint_pass(box misc::CmpOwned); reg.register_late_lint_pass(box misc::CmpOwned);
reg.register_late_lint_pass(box attrs::AttrPass); 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 block_in_if_condition::BlockInIfCondition);
reg.register_late_lint_pass(box misc::ModuloOne); reg.register_late_lint_pass(box misc::ModuloOne);
reg.register_late_lint_pass(box unicode::Unicode); reg.register_late_lint_pass(box unicode::Unicode);