From 41e6929862c837877e9c93e52af5f182ef499ee6 Mon Sep 17 00:00:00 2001 From: David Bar-On Date: Wed, 19 Aug 2020 18:52:12 +0300 Subject: [PATCH 1/2] Resolve issue #2896 - trim extra blanks at end of line when following comment moved to new line --- src/formatting/comment.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/formatting/comment.rs b/src/formatting/comment.rs index dba19b12afb..43b03197580 100644 --- a/src/formatting/comment.rs +++ b/src/formatting/comment.rs @@ -1604,7 +1604,15 @@ pub(crate) fn recover_comment_removed( let snippet = context.snippet(span); let includes_comment = contains_comment(snippet); if snippet != new && includes_comment && changed_comment_content(snippet, &new) { - Some(snippet.to_owned()) + /* Trim white spaces at end of lines */ + let mut c = String::from(""); + for line in snippet.to_owned().split('\n') { + if c != "" { + c.push('\n') + }; + c.push_str(line.trim_end()); + } + Some(c.to_owned()) } else { Some(new) } From 9e8eb5f317facbdd2dd3acb369c8c6b1c2a990ce Mon Sep 17 00:00:00 2001 From: David Bar-On Date: Mon, 24 Aug 2020 16:29:59 +0300 Subject: [PATCH 2/2] Add test cases to tests/source/issue-2896.rs file for cases solved by pull request #4391 --- tests/source/issue-2896.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/source/issue-2896.rs b/tests/source/issue-2896.rs index f648e64b1e3..9a7aedf8547 100644 --- a/tests/source/issue-2896.rs +++ b/tests/source/issue-2896.rs @@ -159,3 +159,24 @@ fn main() { } }).unwrap(); } + + + +/******************************************************************************************* + * Added test cases for related issues that are supposed to be solved by Pull request #4391 + * ******************************************************************************************/ + +// ** Note the extra blank at the end of the 2nd line +fn main() { + if 0 == 1 + /* x */ as i32 {} } + +// ** Note the extra blank at the end of the 2nd line +fn main() { + if 0 == ' ' + as i32 {} } + +// ** Note the extra blank at the end of the 3rd line +fn main() { + if 0 == ' ' /* x */ + as i32 {} } \ No newline at end of file