Skip to content

Commit 55b78ae

Browse files
committed
Rustup to ea0dc92 II
1 parent 3c4af49 commit 55b78ae

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

clippy_lints/src/collapsible_if.rs

+11-23
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ fn check_if(cx: &EarlyContext, expr: &ast::Expr) {
7272
fn check_collapsible_maybe_if_let(cx: &EarlyContext, else_: &ast::Expr) {
7373
if_let_chain! {[
7474
let ast::ExprKind::Block(ref block) = else_.node,
75-
block.stmts.is_empty(),
76-
let Some(ref else_) = block.expr,
75+
let Some(ref else_) = expr_block(block),
76+
!in_macro(cx, else_.span),
7777
], {
7878
match else_.node {
7979
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
@@ -96,7 +96,7 @@ fn check_collapsible_no_if_let(
9696
then: &ast::Block,
9797
) {
9898
if_let_chain! {[
99-
let Some(inner) = single_stmt_of_block(then),
99+
let Some(inner) = expr_block(then),
100100
let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node,
101101
], {
102102
if expr.span.expn_id != inner.span.expn_id {
@@ -128,28 +128,16 @@ fn check_to_string(cx: &EarlyContext, e: &ast::Expr) -> Cow<'static, str> {
128128
}
129129
}
130130

131-
fn single_stmt_of_block(block: &ast::Block) -> Option<&ast::Expr> {
132-
if block.stmts.len() == 1 && block.expr.is_none() {
133-
if let ast::StmtKind::Expr(ref expr, _) = block.stmts[0].node {
134-
single_stmt_of_expr(expr)
135-
} else {
136-
None
137-
}
138-
} else if block.stmts.is_empty() {
139-
if let Some(ref p) = block.expr {
140-
Some(p)
141-
} else {
142-
None
131+
/// If the block contains only one expression, returns it.
132+
fn expr_block(block: &ast::Block) -> Option<&ast::Expr> {
133+
let mut it = block.stmts.iter();
134+
135+
if let (Some(stmt), None) = (it.next(), it.next()) {
136+
match stmt.node {
137+
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(expr),
138+
_ => None,
143139
}
144140
} else {
145141
None
146142
}
147143
}
148-
149-
fn single_stmt_of_expr(expr: &ast::Expr) -> Option<&ast::Expr> {
150-
if let ast::ExprKind::Block(ref block) = expr.node {
151-
single_stmt_of_block(block)
152-
} else {
153-
Some(expr)
154-
}
155-
}

tests/compile-fail/collapsible_if.rs

+5
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,9 @@ fn main() {
139139
println!("world!")
140140
}
141141
}
142+
143+
if true {
144+
} else {
145+
assert!(true); // assert! is just an `if`
146+
}
142147
}

0 commit comments

Comments
 (0)