Skip to content

Commit 5587fd9

Browse files
author
Alexander Glusker
committed
Ignoring empty statements in closures. Resolve #6116
1 parent 202fa22 commit 5587fd9

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
@@ -107,7 +107,10 @@ fn get_inner_expr<'a>(
107107
if !needs_block(block, prefix, context) {
108108
// block.stmts.len() == 1 except with `|| {{}}`;
109109
// https://github.com/rust-lang/rustfmt/issues/3844
110-
if let Some(expr) = block.stmts.first().and_then(stmt_expr) {
110+
if let Some(expr) = iter_stmts_without_empty(&block.stmts)
111+
.next()
112+
.and_then(stmt_expr)
113+
{
111114
return get_inner_expr(expr, prefix, context);
112115
}
113116
}
@@ -116,14 +119,23 @@ fn get_inner_expr<'a>(
116119
expr
117120
}
118121

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

125137
is_unsafe_block(block)
126-
|| block.stmts.len() > 1
138+
|| iter_stmts_without_empty(&block.stmts).count() > 1
127139
|| has_attributes
128140
|| block_contains_comment(context, block)
129141
|| 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)