Skip to content

Commit c4e648f

Browse files
authored
Do not flatten match arm block with leading attributes (#4124)
1 parent eb2bb07 commit c4e648f

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

rustfmt-core/rustfmt-lib/src/matches.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,11 @@ fn flatten_arm_body<'a>(
299299
if let Some(ref block) = block_can_be_flattened(context, body) {
300300
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].kind {
301301
if let ast::ExprKind::Block(..) = expr.kind {
302-
flatten_arm_body(context, expr, None)
302+
if expr.attrs.is_empty() {
303+
flatten_arm_body(context, expr, None)
304+
} else {
305+
(true, body)
306+
}
303307
} else {
304308
let cond_becomes_muti_line = opt_shape
305309
.and_then(|shape| rewrite_cond(context, expr, shape))

rustfmt-core/rustfmt-lib/tests/source/match.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,22 @@ fn issue_3774() {
568568
}
569569
}
570570
}
571+
572+
// #4109
573+
fn issue_4109() {
574+
match () {
575+
_ => {
576+
#[cfg(debug_assertions)]
577+
{
578+
println!("Foo");
579+
}
580+
}
581+
}
582+
583+
match () {
584+
_ => {
585+
#[allow(unsafe_code)]
586+
unsafe {}
587+
}
588+
}
589+
}

rustfmt-core/rustfmt-lib/tests/target/match.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,22 @@ fn issue_3774() {
605605
}
606606
}
607607
}
608+
609+
// #4109
610+
fn issue_4109() {
611+
match () {
612+
_ => {
613+
#[cfg(debug_assertions)]
614+
{
615+
println!("Foo");
616+
}
617+
}
618+
}
619+
620+
match () {
621+
_ => {
622+
#[allow(unsafe_code)]
623+
unsafe {}
624+
}
625+
}
626+
}

0 commit comments

Comments
 (0)