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
13 changes: 12 additions & 1 deletion src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ fn rewrite_match_arm(
.sub_width(7 + label_len, arm.span)?
.offset_left(pipe_offset, arm.span)?
}
ast::ExprKind::Block(block, None)
if is_unsafe_block(block)
Comment on lines +289 to +290

@ytmimi ytmimi Sep 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ever possible to have an unsafe block with a label?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'a: unsafe{ 5 } gives compile error - after 'a -> a loop or block is expected.

&& context.config.style_edition() >= StyleEdition::Edition2027 =>
{
// 12 = ` => unsafe {`
shape
.sub_width(12, arm.span)?
.offset_left(pipe_offset, arm.span)?
Comment on lines +293 to +296

@ytmimi ytmimi Sep 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know the block is empty, should we:

// 13 = ` => unsafe {}`
sub_width(13, arm.span)

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we can

}
_ => {
// 5 = ` => {`
shape
Expand Down Expand Up @@ -526,9 +535,11 @@ pub(crate) fn rewrite_match_body(
body_shape.width,
);

let enforce_empty_block_width =
is_empty_block && context.config.style_edition() >= StyleEdition::Edition2027;
match rewrite {
Ok(ref body_str)
if is_block
if (is_block && !enforce_empty_block_width)
|| (!body_str.contains('\n')
&& unicode_str_width(body_str) <= body_shape.width) =>
{
Expand Down
90 changes: 90 additions & 0 deletions tests/source/issue_6848_style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// rustfmt-style_edition: 2024
// rustfmt-max_width: 80

#![feature(gen_blocks, try_blocks)]

#[derive(Clone, Copy)]
enum ExampleTypeX {
VariantAlphaSampleXYZ,
VariantBetaXYZ,
VariantABCD,
VariantABCDE,
VariantABCDEF,
VariantABCDEFG,
VariantABCDEFGH,
VariantABCDEFGHI,
VariantABCDEFGHIJ,
VariantABCDEFGHIJK,
}

fn demo(lhs: ExampleTypeX, rhs: ExampleTypeX) {
//unsafe block
match (lhs, rhs) {
(
ExampleTypeX::VariantAlphaSampleXYZ,
ExampleTypeX::VariantBetaXYZ,
) => unsafe {
}
_ => {}
}

match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCD, ExampleTypeX::VariantBetaXYZ) => unsafe {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => unsafe {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe {},
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()},
(ExampleTypeX::VariantABCDEFGHIJKL, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()},
(ExampleTypeX::VariantABCDEFGHIJKLMNO, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()},
(ExampleTypeX::VariantABCDEFHGHI, ExampleTypeX::VariantBetaXYZ) => 'a: {},
_ => {}
}

// const block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => const {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => const {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => const {},
_ => {}
}

// async block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => async {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => async {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => async {},
_ => {}
}

// gen block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => gen {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => gen {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => gen {},
_ => {}
}

// try block
match (lhs, rhs) {
//1 char below the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => try {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => try {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => try {},
_ => {}
}
}

fn main() {}
90 changes: 90 additions & 0 deletions tests/source/issue_6848_style_edition_2027.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// rustfmt-style_edition: 2027
// rustfmt-max_width: 80

#![feature(gen_blocks, try_blocks)]

#[derive(Clone, Copy)]
enum ExampleTypeX {
VariantAlphaSampleXYZ,
VariantBetaXYZ,
VariantABCD,
VariantABCDE,
VariantABCDEF,
VariantABCDEFG,
VariantABCDEFGH,
VariantABCDEFGHI,
VariantABCDEFGHIJ,
VariantABCDEFGHIJK,
}

fn demo(lhs: ExampleTypeX, rhs: ExampleTypeX) {
//unsafe block
match (lhs, rhs) {
(
ExampleTypeX::VariantAlphaSampleXYZ,
ExampleTypeX::VariantBetaXYZ,
) => unsafe {
}
_ => {}
}

match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCD, ExampleTypeX::VariantBetaXYZ) => unsafe {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => unsafe {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe {},
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()},
(ExampleTypeX::VariantABCDEFGHIJKL, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()},
(ExampleTypeX::VariantABCDEFGHIJKLMNO, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()},
(ExampleTypeX::VariantABCDEFHGHI, ExampleTypeX::VariantBetaXYZ) => 'a: {},
_ => {}
}

// const block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => const {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => const {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => const {},
_ => {}
}

// async block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => async {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => async {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => async {},
_ => {}
}

// gen block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => gen {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => gen {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => gen {},
_ => {}
}

// try block
match (lhs, rhs) {
//1 char below the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => try {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => try {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => try {},
_ => {}
}
}

fn main() {}
102 changes: 102 additions & 0 deletions tests/target/issue_6848_style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// rustfmt-style_edition: 2024
// rustfmt-max_width: 80

#![feature(gen_blocks, try_blocks)]

#[derive(Clone, Copy)]
enum ExampleTypeX {
VariantAlphaSampleXYZ,
VariantBetaXYZ,
VariantABCD,
VariantABCDE,
VariantABCDEF,
VariantABCDEFG,
VariantABCDEFGH,
VariantABCDEFGHI,
VariantABCDEFGHIJ,
VariantABCDEFGHIJK,
}

fn demo(lhs: ExampleTypeX, rhs: ExampleTypeX) {
//unsafe block
match (lhs, rhs) {
(ExampleTypeX::VariantAlphaSampleXYZ, ExampleTypeX::VariantBetaXYZ) => unsafe {
},
_ => {}
}

match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCD, ExampleTypeX::VariantBetaXYZ) => unsafe {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => unsafe {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe {},

@ytmimi ytmimi Sep 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand why there's this inconsistency between the unsafe formatting and const, async, gen and try examples below.

Do we need to update the pattern matching for these other kinds of blocks in rewrite_match_arm?

View changes since the review

@AsthaMishra AsthaMishra Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pattern matching seems correct we are doing same thing in expr.rs as well and for ast::ExprKind::Block unsafe cases are handled in if branch.

we are just missing pattern arm for unsafe block,

rustfmt/src/matches.rs

Lines 280 to 295 in 7bc6cd7

let pat_shape = match &arm.body.as_ref().unknown_error()?.kind {
ast::ExprKind::Block(_, Some(label)) => {
// Some block with a label ` => 'label: {`
// 7 = ` => : {`
let label_len = label.ident.as_str().len();
shape
.sub_width(7 + label_len, arm.span)?
.offset_left(pipe_offset, arm.span)?
}
_ => {
// 5 = ` => {`
shape
.sub_width(5, arm.span)?
.offset_left(pipe_offset, arm.span)?
}
};

lable block works, - ast::ExprKind::Block(_, Some(label))
const , async, gen and try - these works - default arm

(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe {
non_empty_block()
},
(ExampleTypeX::VariantABCDEFGHIJKL, ExampleTypeX::VariantBetaXYZ) => unsafe {
non_empty_block()
},
(
ExampleTypeX::VariantABCDEFGHIJKLMNO,
ExampleTypeX::VariantBetaXYZ,
) => unsafe { non_empty_block() },
(ExampleTypeX::VariantABCDEFHGHI, ExampleTypeX::VariantBetaXYZ) => 'a: {}
_ => {}
}

// const block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => const {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => const {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => {
const {}
}
_ => {}
}

// async block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => async {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => async {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => {
async {}
}
_ => {}
}

// gen block
match (lhs, rhs) {
// 1 char below the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => gen {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => gen {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => {
gen {}
}
_ => {}
}

// try block
match (lhs, rhs) {
//1 char below the max_width limit
(ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => try {},
// everything properly fits on 1 line at exactly the max_width limit
(ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => try {},
// 1 char over the max_width limit
(ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => {
try {}
}
_ => {}
}
}

fn main() {}
Loading