Skip to content

Commit b21c80c

Browse files
Alexander Gluskerytmimi
Alexander Glusker
authored andcommitted
Ignoring empty statements in closures. Resolve #6116
1 parent 6f7aeed commit b21c80c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/closures.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ fn get_inner_expr<'a>(
108108
if !needs_block(block, prefix, context) {
109109
// block.stmts.len() == 1 except with `|| {{}}`;
110110
// https://github.com/rust-lang/rustfmt/issues/3844
111-
if let Some(expr) = block.stmts.first().and_then(stmt_expr) {
111+
if let Some(expr) = iter_stmts_without_empty(&block.stmts)
112+
.next()
113+
.and_then(stmt_expr)
114+
{
112115
return get_inner_expr(expr, prefix, context);
113116
}
114117
}
@@ -117,14 +120,23 @@ fn get_inner_expr<'a>(
117120
expr
118121
}
119122

123+
fn iter_stmts_without_empty(
124+
stmts: &thin_vec::ThinVec<ast::Stmt>,
125+
) -> impl Iterator<Item = &ast::Stmt> {
126+
stmts.iter().filter(|x| match x.kind {
127+
crate::ast::StmtKind::Empty => false,
128+
_ => true,
129+
})
130+
}
131+
120132
// Figure out if a block is necessary.
121133
fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -> bool {
122134
let has_attributes = block.stmts.first().map_or(false, |first_stmt| {
123135
!get_attrs_from_stmt(first_stmt).is_empty()
124136
});
125137

126138
is_unsafe_block(block)
127-
|| block.stmts.len() > 1
139+
|| iter_stmts_without_empty(&block.stmts).count() > 1
128140
|| has_attributes
129141
|| block_contains_comment(context, block)
130142
|| prefix.contains('\n')

tests/source/issue-6116/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn foo() -> fn(i32) -> i32 {
2+
|a| {
3+
;
4+
a
5+
}
6+
}
7+

tests/target/issue-6116/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo() -> fn(i32) -> i32 {
2+
|a| a
3+
}

0 commit comments

Comments
 (0)