From ee92a17649f5533db7d7dd5d803b204a300c8fec Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Mon, 9 Jun 2025 23:26:25 -0400 Subject: [PATCH] fix: remove trailing spaces when rewriting same line missing comments Fixes 6568 When using `style_edition=2024` trailing whitespace was not properly removed when foramtting same line comments. --- src/missed_spans.rs | 6 +++--- tests/source/issue_6568.rs | 3 +++ tests/target/issue_6568.rs | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 tests/source/issue_6568.rs create mode 100644 tests/target/issue_6568.rs diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 384de1ce9ae..b2db474b06b 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -270,15 +270,15 @@ impl<'a> FmtVisitor<'a> { if on_same_line { match subslice.find('\n') { None => { - self.push_str(subslice); + self.push_str(subslice.trim_end()); } Some(offset) if offset + 1 == subslice.len() => { - self.push_str(&subslice[..offset]); + self.push_str(&subslice[..offset].trim_end()); } Some(offset) => { // keep first line as is: if it were too long and wrapped, it may get mixed // with the other lines. - let first_line = &subslice[..offset]; + let first_line = &subslice[..offset].trim_end(); self.push_str(first_line); self.push_str(&comment_indent.to_string_with_newline(self.config)); diff --git a/tests/source/issue_6568.rs b/tests/source/issue_6568.rs new file mode 100644 index 00000000000..8c76a8cfb79 --- /dev/null +++ b/tests/source/issue_6568.rs @@ -0,0 +1,3 @@ +// rustfmt-style_edition: 2024 + +fn f() {} // This has trailing whitespace diff --git a/tests/target/issue_6568.rs b/tests/target/issue_6568.rs new file mode 100644 index 00000000000..da59ea87196 --- /dev/null +++ b/tests/target/issue_6568.rs @@ -0,0 +1,3 @@ +// rustfmt-style_edition: 2024 + +fn f() {} // This has trailing whitespace