Skip to content

Commit

Permalink
Ignoring empty statements in closures. Resolve rust-lang#6116
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Glusker authored and rscprof committed Apr 9, 2024
1 parent 7289391 commit 83496fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 107,10 @@ 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.stmts)
.next()
.and_then(stmt_expr)
{
return get_inner_expr(expr, prefix, context);
}
}
Expand All @@ -116,14 119,23 @@ fn get_inner_expr<'a>(
expr
}

fn iter_stmts_without_empty(
stmts: &thin_vec::ThinVec<ast::Stmt>,
) -> impl Iterator<Item = &ast::Stmt> {
stmts.iter().filter(|x| match x.kind {
crate::ast::StmtKind::Empty => false,
_ => true,
})
}

// 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.stmts).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
}
}

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
}

0 comments on commit 83496fb

Please sign in to comment.