Skip to content

Commit 39cd58e

Browse files
committed
Refactor collapsible_if: Check AST before checking for macros
1 parent 01f53c2 commit 39cd58e

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,14 @@ declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF, COLLAPSIBLE_ELSE_IF]);
9393

9494
impl EarlyLintPass for CollapsibleIf {
9595
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
96-
if !expr.span.from_expansion() {
97-
check_if(cx, expr);
98-
}
99-
}
100-
}
101-
102-
fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
103-
if let ast::ExprKind::If(check, then, else_) = &expr.kind {
104-
if let Some(else_) = else_ {
105-
check_collapsible_maybe_if_let(cx, then.span, else_);
106-
} else if let ast::ExprKind::Let(..) = check.kind {
107-
// Prevent triggering on `if let a = b { if c { .. } }`.
108-
} else {
109-
check_collapsible_no_if_let(cx, expr, check, then);
96+
if let ast::ExprKind::If(cond, then, else_) = &expr.kind
97+
&& !expr.span.from_expansion()
98+
{
99+
if let Some(else_) = else_ {
100+
check_collapsible_maybe_if_let(cx, then.span, else_);
101+
} else if !matches!(cond.kind, ast::ExprKind::Let(..)) {
102+
check_collapsible_no_if_let(cx, expr, cond, then);
103+
}
110104
}
111105
}
112106
}
@@ -189,13 +183,10 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &
189183

190184
/// If the block contains only one expression, return it.
191185
fn expr_block(block: &ast::Block) -> Option<&ast::Expr> {
192-
let mut it = block.stmts.iter();
193-
194-
if let (Some(stmt), None) = (it.next(), it.next()) {
195-
match stmt.kind {
196-
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(expr),
197-
_ => None,
198-
}
186+
if let [stmt] = &*block.stmts
187+
&& let ast::StmtKind::Expr(expr) | ast::StmtKind::Semi(expr) = &stmt.kind
188+
{
189+
Some(expr)
199190
} else {
200191
None
201192
}

0 commit comments

Comments
 (0)