From 01fdd5de2e7741d00d8313f781e39d09093b938f Mon Sep 17 00:00:00 2001 From: vallentin Date: Mon, 18 Jan 2021 19:37:15 +0100 Subject: [PATCH 1/3] Fixed trailing comment being moved when comma is inserted (fixes #4654) --- src/formatting/lists.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/formatting/lists.rs b/src/formatting/lists.rs index 760c1f62946..093b14f7573 100644 --- a/src/formatting/lists.rs +++ b/src/formatting/lists.rs @@ -632,23 +632,28 @@ pub(crate) fn extract_post_comment( post_snippet: &str, comment_end: usize, separator: &str, + leave_last: bool, ) -> Option { let white_space: &[_] = &[' ', '\t']; // Cleanup post-comment: strip separators and whitespace. - let post_snippet = post_snippet[..comment_end].trim(); - let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') { - post_snippet[1..].trim_matches(white_space) - } else if let Some(post_snippet) = post_snippet.strip_prefix(separator) { + let (post_snippet, comment_end) = post_snippet.split_at(comment_end); + let post_snippet_trimmed = post_snippet.trim(); + + let post_snippet_trimmed = if post_snippet_trimmed.starts_with(|c| c == ',' || c == ':') { + post_snippet_trimmed[1..].trim_matches(white_space) + } else if let Some(post_snippet) = post_snippet_trimmed.strip_prefix(separator) { post_snippet.trim_matches(white_space) } // not comment or over two lines - else if post_snippet.ends_with(',') - && (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n')) + else if post_snippet_trimmed.ends_with(',') + && (!post_snippet_trimmed.starts_with("//") || post_snippet_trimmed.contains('\n')) { - post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space) + post_snippet_trimmed[..(post_snippet_trimmed.len() - 1)].trim_matches(white_space) + } else if comment_end == ")" && !leave_last { + post_snippet_trimmed } else { - post_snippet + post_snippet.trim_start_matches(white_space).trim_end() }; // FIXME(#3441): post_snippet includes 'const' now // it should not include here @@ -776,7 +781,8 @@ where self.inner.peek().is_none(), ); let new_lines = has_extra_newline(post_snippet, comment_end); - let post_comment = extract_post_comment(post_snippet, comment_end, self.separator); + let post_comment = + extract_post_comment(post_snippet, comment_end, self.separator, self.leave_last); self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32); From 6a32db163f4a7a58ec20fbf298788199ca2b0e6a Mon Sep 17 00:00:00 2001 From: vallentin Date: Mon, 18 Jan 2021 20:42:22 +0100 Subject: [PATCH 2/3] Added 4654 test case --- tests/source/issue-4654.rs | 45 ++++++++++++++++++++++++++++++++++++++ tests/target/issue-4654.rs | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/source/issue-4654.rs create mode 100644 tests/target/issue-4654.rs diff --git a/tests/source/issue-4654.rs b/tests/source/issue-4654.rs new file mode 100644 index 00000000000..cf1e4434e15 --- /dev/null +++ b/tests/source/issue-4654.rs @@ -0,0 +1,45 @@ +struct Foo { + bar: () + // Comment +} + +struct Bar { + baz: () +/* +Comment +*/ +} + +struct Baz( + () + // Comment +); + +fn main() { + let _ = Foo { + bar: () + // Comment + }; + + let _ = Bar { + baz: () + /* + Comment + */ + }; + + let _ = Baz( + () + + // Comment + ); + + match a { +0 => {} + // Foo + 1 => {} // Bar + // Baz + 2 => {} +// Qux + } +} diff --git a/tests/target/issue-4654.rs b/tests/target/issue-4654.rs new file mode 100644 index 00000000000..792bf811e53 --- /dev/null +++ b/tests/target/issue-4654.rs @@ -0,0 +1,44 @@ +struct Foo { + bar: (), + // Comment +} + +struct Bar { + baz: (), + /* + Comment + */ +} + +struct Baz( + (), + // Comment +); + +fn main() { + let _ = Foo { + bar: (), + // Comment + }; + + let _ = Bar { + baz: (), + /* + Comment + */ + }; + + let _ = Baz( + (), + // Comment + ); + + match a { + 0 => {} + // Foo + 1 => {} // Bar + // Baz + 2 => {} + // Qux + } +} From 1d599915808ff381240d870a4eabe9a2ff79f1bd Mon Sep 17 00:00:00 2001 From: vallentin Date: Mon, 18 Jan 2021 20:44:17 +0100 Subject: [PATCH 3/3] Fixed 3532 test case --- tests/target/issue-3532.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/target/issue-3532.rs b/tests/target/issue-3532.rs index f41902620cd..726b55bda57 100644 --- a/tests/target/issue-3532.rs +++ b/tests/target/issue-3532.rs @@ -1,6 +1,7 @@ fn foo(a: T) { match a { 1 => {} - 0 => {} // _ => panic!("doesn't format!"), + 0 => {} + // _ => panic!("doesn't format!"), } }