Skip to content

Commit

Permalink
Rustup to ea0dc92 II
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarton committed Jul 1, 2016
1 parent e02f8e1 commit 0150544
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
34 changes: 11 additions & 23 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ fn check_if(cx: &EarlyContext, expr: &ast::Expr) {
fn check_collapsible_maybe_if_let(cx: &EarlyContext, else_: &ast::Expr) {
if_let_chain! {[
let ast::ExprKind::Block(ref block) = else_.node,
block.stmts.is_empty(),
let Some(ref else_) = block.expr,
let Some(ref else_) = expr_block(block),
!in_macro(cx, else_.span),
], {
match else_.node {
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
Expand All @@ -96,7 +96,7 @@ fn check_collapsible_no_if_let(
then: &ast::Block,
) {
if_let_chain! {[
let Some(inner) = single_stmt_of_block(then),
let Some(inner) = expr_block(then),
let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node,
], {
if expr.span.expn_id != inner.span.expn_id {
Expand Down Expand Up @@ -128,28 +128,16 @@ fn check_to_string(cx: &EarlyContext, e: &ast::Expr) -> Cow<'static, str> {
}
}

fn single_stmt_of_block(block: &ast::Block) -> Option<&ast::Expr> {
if block.stmts.len() == 1 && block.expr.is_none() {
if let ast::StmtKind::Expr(ref expr, _) = block.stmts[0].node {
single_stmt_of_expr(expr)
} else {
None
}
} else if block.stmts.is_empty() {
if let Some(ref p) = block.expr {
Some(p)
} else {
None
/// If the block contains only one expression, returns it.
fn expr_block(block: &ast::Block) -> Option<&ast::Expr> {
let mut it = block.stmts.iter();

if let (Some(stmt), None) = (it.next(), it.next()) {
match stmt.node {
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(expr),
_ => None,
}
} else {
None
}
}

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)
}
}
5 changes: 5 additions & 0 deletions tests/compile-fail/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ fn main() {
println!("world!")
}
}

if true {
} else {
assert!(true); // assert! is just an `if`
}
}

0 comments on commit 0150544

Please sign in to comment.