Skip to content

Commit f37c9ad

Browse files
committed
Make CollapsibleIf an EarlyLintPass
It doesn't need any `hir` feature and `ast` is much more stable.
1 parent 7fa38f6 commit f37c9ad

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
//! This lint is **warn** by default
1414
1515
use rustc::lint::*;
16-
use rustc::hir::*;
1716
use std::borrow::Cow;
1817
use syntax::codemap::Spanned;
18+
use syntax::ast;
1919

2020
use utils::{in_macro, snippet, snippet_block, span_lint_and_then};
2121

@@ -45,23 +45,22 @@ impl LintPass for CollapsibleIf {
4545
}
4646
}
4747

48-
impl LateLintPass for CollapsibleIf {
49-
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
48+
impl EarlyLintPass for CollapsibleIf {
49+
fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
5050
if !in_macro(cx, expr.span) {
5151
check_if(cx, expr)
5252
}
5353
}
5454
}
5555

56-
fn check_if(cx: &LateContext, e: &Expr) {
57-
if let ExprIf(ref check, ref then, ref else_) = e.node {
56+
fn check_if(cx: &EarlyContext, e: &ast::Expr) {
57+
if let ast::ExprKind::If(ref check, ref then, ref else_) = e.node {
5858
if let Some(ref else_) = *else_ {
5959
if_let_chain! {[
60-
let ExprBlock(ref block) = else_.node,
60+
let ast::ExprKind::Block(ref block) = else_.node,
6161
block.stmts.is_empty(),
62-
block.rules == BlockCheckMode::DefaultBlock,
6362
let Some(ref else_) = block.expr,
64-
let ExprIf(_, _, _) = else_.node
63+
let ast::ExprKind::If(_, _, _) = else_.node
6564
], {
6665
span_lint_and_then(cx,
6766
COLLAPSIBLE_IF,
@@ -70,7 +69,7 @@ fn check_if(cx: &LateContext, e: &Expr) {
7069
db.span_suggestion(block.span, "try", snippet_block(cx, else_.span, "..").into_owned());
7170
});
7271
}}
73-
} else if let Some(&Expr { node: ExprIf(ref check_inner, ref content, None), span: sp, .. }) =
72+
} else if let Some(&ast::Expr { node: ast::ExprKind::If(ref check_inner, ref content, None), span: sp, .. }) =
7473
single_stmt_of_block(then) {
7574
if e.span.expn_id != sp.expn_id {
7675
return;
@@ -87,24 +86,24 @@ fn check_if(cx: &LateContext, e: &Expr) {
8786
}
8887
}
8988

90-
fn requires_brackets(e: &Expr) -> bool {
89+
fn requires_brackets(e: &ast::Expr) -> bool {
9190
match e.node {
92-
ExprBinary(Spanned { node: n, .. }, _, _) if n == BiEq => false,
91+
ast::ExprKind::Binary(Spanned { node: n, .. }, _, _) if n == ast::BinOpKind::Eq => false,
9392
_ => true,
9493
}
9594
}
9695

97-
fn check_to_string(cx: &LateContext, e: &Expr) -> Cow<'static, str> {
96+
fn check_to_string(cx: &EarlyContext, e: &ast::Expr) -> Cow<'static, str> {
9897
if requires_brackets(e) {
9998
format!("({})", snippet(cx, e.span, "..")).into()
10099
} else {
101100
snippet(cx, e.span, "..")
102101
}
103102
}
104103

105-
fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
104+
fn single_stmt_of_block(block: &ast::Block) -> Option<&ast::Expr> {
106105
if block.stmts.len() == 1 && block.expr.is_none() {
107-
if let StmtExpr(ref expr, _) = block.stmts[0].node {
106+
if let ast::StmtKind::Expr(ref expr, _) = block.stmts[0].node {
108107
single_stmt_of_expr(expr)
109108
} else {
110109
None
@@ -120,8 +119,8 @@ fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
120119
}
121120
}
122121

123-
fn single_stmt_of_expr(expr: &Expr) -> Option<&Expr> {
124-
if let ExprBlock(ref block) = expr.node {
122+
fn single_stmt_of_expr(expr: &ast::Expr) -> Option<&ast::Expr> {
123+
if let ast::ExprKind::Block(ref block) = expr.node {
125124
single_stmt_of_block(block)
126125
} else {
127126
Some(expr)

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
189189
reg.register_late_lint_pass(box len_zero::LenZero);
190190
reg.register_late_lint_pass(box misc::CmpOwned);
191191
reg.register_late_lint_pass(box attrs::AttrPass);
192-
reg.register_late_lint_pass(box collapsible_if::CollapsibleIf);
192+
reg.register_early_lint_pass(box collapsible_if::CollapsibleIf);
193193
reg.register_late_lint_pass(box block_in_if_condition::BlockInIfCondition);
194194
reg.register_late_lint_pass(box misc::ModuloOne);
195195
reg.register_late_lint_pass(box unicode::Unicode);

0 commit comments

Comments
 (0)