]> git.lizzy.rs Git - rust.git/commitdiff
Make `CollapsibleIf` an `EarlyLintPass`
authormcarton <cartonmartin+git@gmail.com>
Tue, 21 Jun 2016 21:17:18 +0000 (23:17 +0200)
committermcarton <cartonmartin+git@gmail.com>
Wed, 29 Jun 2016 15:09:39 +0000 (17:09 +0200)
It doesn't need any `hir` feature and `ast` is much more stable.

clippy_lints/src/collapsible_if.rs
clippy_lints/src/lib.rs

index 38e04723e53024780edb4c8a3994428a18c878cc..9bdc35596bf4e940ec436df737ecc791b7854dd9 100644 (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)
index 3fd94661f67e26341c40d9379a20ee73b067c018..e980f4d081dfd0deaf1c4334ddba5089f40a3177 100644 (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);