diff --git a/src/matches.rs b/src/matches.rs index 4e82df98de4..b6bc7b4b4ad 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -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) + && context.config.style_edition() >= StyleEdition::Edition2027 => + { + // 12 = ` => unsafe {` + shape + .sub_width(12, arm.span)? + .offset_left(pipe_offset, arm.span)? + } _ => { // 5 = ` => {` shape @@ -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) => { diff --git a/tests/source/issue_6848_style_edition_2024.rs b/tests/source/issue_6848_style_edition_2024.rs new file mode 100644 index 00000000000..96dbd76415e --- /dev/null +++ b/tests/source/issue_6848_style_edition_2024.rs @@ -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() {} diff --git a/tests/source/issue_6848_style_edition_2027.rs b/tests/source/issue_6848_style_edition_2027.rs new file mode 100644 index 00000000000..3ac29f2f81d --- /dev/null +++ b/tests/source/issue_6848_style_edition_2027.rs @@ -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() {} diff --git a/tests/target/issue_6848_style_edition_2024.rs b/tests/target/issue_6848_style_edition_2024.rs new file mode 100644 index 00000000000..ddcbc933747 --- /dev/null +++ b/tests/target/issue_6848_style_edition_2024.rs @@ -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 {}, + (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() {} diff --git a/tests/target/issue_6848_style_edition_2027.rs b/tests/target/issue_6848_style_edition_2027.rs new file mode 100644 index 00000000000..18421835b89 --- /dev/null +++ b/tests/target/issue_6848_style_edition_2027.rs @@ -0,0 +1,107 @@ +// 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() {}