Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn get_inner_expr<'a>(
if !needs_block(block, prefix, context) {
// block.stmts.len() == 1 except with `|| {{}}`;
// https://github.com/rust-lang/rustfmt/issues/3844
if let Some(expr) = block.stmts.first().and_then(stmt_expr) {
if let Some(expr) = iter_stmts_without_empty(&block).next().and_then(stmt_expr) {
return get_inner_expr(expr, prefix, context);
}
}
Expand All @@ -117,14 +117,21 @@ fn get_inner_expr<'a>(
expr
}

fn iter_stmts_without_empty(block: &ast::Block) -> impl Iterator<Item = &ast::Stmt> {
block
.stmts
.iter()
.filter(|stmt| !matches!(stmt.kind, ast::StmtKind::Empty))
}

// Figure out if a block is necessary.
fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -> bool {
let has_attributes = block.stmts.first().map_or(false, |first_stmt| {
!get_attrs_from_stmt(first_stmt).is_empty()
});

is_unsafe_block(block)
|| block.stmts.len() > 1
|| iter_stmts_without_empty(&block).count() > 1
|| has_attributes
|| block_contains_comment(context, block)
|| prefix.contains('\n')
Expand Down
7 changes: 7 additions & 0 deletions tests/source/issue-6116/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn foo() -> fn(i32) -> i32 {
|a| {
;
a
}
}

8 changes: 8 additions & 0 deletions tests/source/issue-6116/main2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn bar() -> fn(i32) -> i32 {
|a| {
;
a;
b
}
}

8 changes: 8 additions & 0 deletions tests/source/issue-6116/main3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn foo() -> fn(i32) -> i32 {
|a| {
;
;
;;;;
a
}
}
7 changes: 7 additions & 0 deletions tests/source/issue-6116/main4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn foo() -> fn(i32) -> i32 {
|a| {
/*comment before empty statement */;
a
}
}

3 changes: 3 additions & 0 deletions tests/target/issue-6116/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo() -> fn(i32) -> i32 {
|a| a
}
6 changes: 6 additions & 0 deletions tests/target/issue-6116/main2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn bar() -> fn(i32) -> i32 {
|a| {
a;
b
}
}
3 changes: 3 additions & 0 deletions tests/target/issue-6116/main3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo() -> fn(i32) -> i32 {
|a| a
}
6 changes: 6 additions & 0 deletions tests/target/issue-6116/main4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn foo() -> fn(i32) -> i32 {
|a| {
/*comment before empty statement */
a
}
}
Loading