//! 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};
}
}
-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,
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;
}
}
-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 {
}
}
-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
}
}
-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)