Do not flatten match arm block with leading attributes

This is a backport of #4124.

Fixes #4109
This commit is contained in:
David Lattimore 2021-12-30 10:13:45 +11:00 committed by Caleb Cartwright
parent f935f0cf89
commit 4a053f206f
3 changed files with 43 additions and 1 deletions

View File

@ -322,7 +322,11 @@ fn flatten_arm_body<'a>(
if let Some(block) = block_can_be_flattened(context, body) { if let Some(block) = block_can_be_flattened(context, body) {
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].kind { if let ast::StmtKind::Expr(ref expr) = block.stmts[0].kind {
if let ast::ExprKind::Block(..) = expr.kind { if let ast::ExprKind::Block(..) = expr.kind {
flatten_arm_body(context, expr, None) if expr.attrs.is_empty() {
flatten_arm_body(context, expr, None)
} else {
(true, body)
}
} else { } else {
let cond_becomes_muti_line = opt_shape let cond_becomes_muti_line = opt_shape
.and_then(|shape| rewrite_cond(context, expr, shape)) .and_then(|shape| rewrite_cond(context, expr, shape))

View File

@ -568,3 +568,22 @@ fn issue_3774() {
} }
} }
} }
// #4109
fn issue_4109() {
match () {
_ => {
#[cfg(debug_assertions)]
{
println!("Foo");
}
}
}
match () {
_ => {
#[allow(unsafe_code)]
unsafe {}
}
}
}

View File

@ -608,3 +608,22 @@ fn issue_3774() {
} }
} }
} }
// #4109
fn issue_4109() {
match () {
_ => {
#[cfg(debug_assertions)]
{
println!("Foo");
}
}
}
match () {
_ => {
#[allow(unsafe_code)]
unsafe {}
}
}
}