From b864b8cf76e0431396182664820e02cde37f1ed0 Mon Sep 17 00:00:00 2001 From: mujpao Date: Mon, 8 Nov 2021 20:33:52 -0800 Subject: [PATCH 1/2] Remove extra whitespace after match arrow When using `control_brace_style = 'AlwaysNextLine'` and `match_arm_blocks = false`, rustfmt would add extra whitespace if the match body didn't fit on one line. This adds a check for this case. --- src/matches.rs | 3 +++ tests/source/issue-4844.rs | 13 +++++++++++++ tests/target/issue-4844.rs | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/source/issue-4844.rs create mode 100644 tests/target/issue-4844.rs diff --git a/src/matches.rs b/src/matches.rs index 22d23fc1cdb..e6fe505728a 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -436,6 +436,9 @@ fn rewrite_match_body( }; let block_sep = match context.config.control_brace_style() { + ControlBraceStyle::AlwaysNextLine if !context.config.match_arm_blocks() => { + format!("{}", body_prefix) + } ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix), _ if body_prefix.is_empty() => "".to_owned(), _ if forbid_same_line || !arrow_comment.is_empty() => { diff --git a/tests/source/issue-4844.rs b/tests/source/issue-4844.rs new file mode 100644 index 00000000000..08faecf79a3 --- /dev/null +++ b/tests/source/issue-4844.rs @@ -0,0 +1,13 @@ +// rustfmt-control_brace_style: AlwaysNextLine +// rustfmt-match_arm_blocks: false + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/target/issue-4844.rs b/tests/target/issue-4844.rs new file mode 100644 index 00000000000..05b9e0d4308 --- /dev/null +++ b/tests/target/issue-4844.rs @@ -0,0 +1,12 @@ +// rustfmt-control_brace_style: AlwaysNextLine +// rustfmt-match_arm_blocks: false + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} From 9efb792957dfb97a6162614c672bc69f0ee5473b Mon Sep 17 00:00:00 2001 From: mujpao Date: Fri, 12 Nov 2021 14:43:38 -0800 Subject: [PATCH 2/2] Add more tests for match_arm_blocks --- .../false-always-next-line.rs} | 0 .../match_arm_blocks/false-always-same-line.rs | 12 ++++++++++++ .../match_arm_blocks/false-closing-next-line.rs | 13 +++++++++++++ .../match_arm_blocks/true-always-next-line.rs | 12 ++++++++++++ .../match_arm_blocks/true-always-same-line.rs | 10 ++++++++++ .../match_arm_blocks/true-closing-next-line.rs | 12 ++++++++++++ .../false-always-next-line.rs} | 0 .../match_arm_blocks/false-always-same-line.rs | 10 ++++++++++ .../match_arm_blocks/false-closing-next-line.rs | 11 +++++++++++ .../match_arm_blocks/true-always-next-line.rs | 13 +++++++++++++ .../match_arm_blocks/true-always-same-line.rs | 9 +++++++++ .../match_arm_blocks/true-closing-next-line.rs | 11 +++++++++++ 12 files changed, 113 insertions(+) rename tests/source/{issue-4844.rs => match_arm_blocks/false-always-next-line.rs} (100%) create mode 100644 tests/source/match_arm_blocks/false-always-same-line.rs create mode 100644 tests/source/match_arm_blocks/false-closing-next-line.rs create mode 100644 tests/source/match_arm_blocks/true-always-next-line.rs create mode 100644 tests/source/match_arm_blocks/true-always-same-line.rs create mode 100644 tests/source/match_arm_blocks/true-closing-next-line.rs rename tests/target/{issue-4844.rs => match_arm_blocks/false-always-next-line.rs} (100%) create mode 100644 tests/target/match_arm_blocks/false-always-same-line.rs create mode 100644 tests/target/match_arm_blocks/false-closing-next-line.rs create mode 100644 tests/target/match_arm_blocks/true-always-next-line.rs create mode 100644 tests/target/match_arm_blocks/true-always-same-line.rs create mode 100644 tests/target/match_arm_blocks/true-closing-next-line.rs diff --git a/tests/source/issue-4844.rs b/tests/source/match_arm_blocks/false-always-next-line.rs similarity index 100% rename from tests/source/issue-4844.rs rename to tests/source/match_arm_blocks/false-always-next-line.rs diff --git a/tests/source/match_arm_blocks/false-always-same-line.rs b/tests/source/match_arm_blocks/false-always-same-line.rs new file mode 100644 index 00000000000..1c084b7aa88 --- /dev/null +++ b/tests/source/match_arm_blocks/false-always-same-line.rs @@ -0,0 +1,12 @@ +// rustfmt-match_arm_blocks: false + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/source/match_arm_blocks/false-closing-next-line.rs b/tests/source/match_arm_blocks/false-closing-next-line.rs new file mode 100644 index 00000000000..129afa7e52c --- /dev/null +++ b/tests/source/match_arm_blocks/false-closing-next-line.rs @@ -0,0 +1,13 @@ +// rustfmt-control_brace_style: ClosingNextLine +// rustfmt-match_arm_blocks: false + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/source/match_arm_blocks/true-always-next-line.rs b/tests/source/match_arm_blocks/true-always-next-line.rs new file mode 100644 index 00000000000..aa1e6fe447a --- /dev/null +++ b/tests/source/match_arm_blocks/true-always-next-line.rs @@ -0,0 +1,12 @@ +// rustfmt-control_brace_style: AlwaysNextLine + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/source/match_arm_blocks/true-always-same-line.rs b/tests/source/match_arm_blocks/true-always-same-line.rs new file mode 100644 index 00000000000..ccc321599a7 --- /dev/null +++ b/tests/source/match_arm_blocks/true-always-same-line.rs @@ -0,0 +1,10 @@ +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/source/match_arm_blocks/true-closing-next-line.rs b/tests/source/match_arm_blocks/true-closing-next-line.rs new file mode 100644 index 00000000000..375bff9af83 --- /dev/null +++ b/tests/source/match_arm_blocks/true-closing-next-line.rs @@ -0,0 +1,12 @@ +// rustfmt-control_brace_style: ClosingNextLine + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/target/issue-4844.rs b/tests/target/match_arm_blocks/false-always-next-line.rs similarity index 100% rename from tests/target/issue-4844.rs rename to tests/target/match_arm_blocks/false-always-next-line.rs diff --git a/tests/target/match_arm_blocks/false-always-same-line.rs b/tests/target/match_arm_blocks/false-always-same-line.rs new file mode 100644 index 00000000000..81cd73aacff --- /dev/null +++ b/tests/target/match_arm_blocks/false-always-same-line.rs @@ -0,0 +1,10 @@ +// rustfmt-match_arm_blocks: false + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo { + "100000000000000000000000" => + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/target/match_arm_blocks/false-closing-next-line.rs b/tests/target/match_arm_blocks/false-closing-next-line.rs new file mode 100644 index 00000000000..ac9d6accc7a --- /dev/null +++ b/tests/target/match_arm_blocks/false-closing-next-line.rs @@ -0,0 +1,11 @@ +// rustfmt-control_brace_style: ClosingNextLine +// rustfmt-match_arm_blocks: false + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo { + "100000000000000000000000" => + fooooooo.len() == 1 && fooooooo.contains("222222222222222222"), + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/target/match_arm_blocks/true-always-next-line.rs b/tests/target/match_arm_blocks/true-always-next-line.rs new file mode 100644 index 00000000000..c982f27c4f8 --- /dev/null +++ b/tests/target/match_arm_blocks/true-always-next-line.rs @@ -0,0 +1,13 @@ +// rustfmt-control_brace_style: AlwaysNextLine + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo + { + "100000000000000000000000" => + { + fooooooo.len() == 1 && fooooooo.contains("222222222222222222") + } + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/target/match_arm_blocks/true-always-same-line.rs b/tests/target/match_arm_blocks/true-always-same-line.rs new file mode 100644 index 00000000000..d76bc9eebc3 --- /dev/null +++ b/tests/target/match_arm_blocks/true-always-same-line.rs @@ -0,0 +1,9 @@ +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo { + "100000000000000000000000" => { + fooooooo.len() == 1 && fooooooo.contains("222222222222222222") + } + _ => unreachable!("Should not happen"), + }; +} diff --git a/tests/target/match_arm_blocks/true-closing-next-line.rs b/tests/target/match_arm_blocks/true-closing-next-line.rs new file mode 100644 index 00000000000..51a3facdb24 --- /dev/null +++ b/tests/target/match_arm_blocks/true-closing-next-line.rs @@ -0,0 +1,11 @@ +// rustfmt-control_brace_style: ClosingNextLine + +fn main() { + let fooooooo = "100000000000000000000000"; + let _bar = match fooooooo { + "100000000000000000000000" => { + fooooooo.len() == 1 && fooooooo.contains("222222222222222222") + } + _ => unreachable!("Should not happen"), + }; +}