@@ -72,8 +72,8 @@ fn check_if(cx: &EarlyContext, expr: &ast::Expr) {
72
72
fn check_collapsible_maybe_if_let ( cx : & EarlyContext , else_ : & ast:: Expr ) {
73
73
if_let_chain ! { [
74
74
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 ) ,
77
77
] , {
78
78
match else_. node {
79
79
ast:: ExprKind :: If ( ..) | ast:: ExprKind :: IfLet ( ..) => {
@@ -96,7 +96,7 @@ fn check_collapsible_no_if_let(
96
96
then : & ast:: Block ,
97
97
) {
98
98
if_let_chain ! { [
99
- let Some ( inner) = single_stmt_of_block ( then) ,
99
+ let Some ( inner) = expr_block ( then) ,
100
100
let ast:: ExprKind :: If ( ref check_inner, ref content, None ) = inner. node,
101
101
] , {
102
102
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> {
128
128
}
129
129
}
130
130
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 ,
143
139
}
144
140
} else {
145
141
None
146
142
}
147
143
}
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
- }
0 commit comments