Skip to content

ssue #2896 - trim extra whitespaces with comment at end of line #4391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Trim white spaces at end of lines */
/* Trim whitespace at end of lines */

let mut c = String::from("");
for line in snippet.to_owned().split('\n') {
if c != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if c != "" {
if !c.is_empty() {

c.push('\n')
};
c.push_str(line.trim_end());
}
Some(c.to_owned())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c is already a String, is to_owned needed?

} else {
Some(new)
}
Expand Down
21 changes: 21 additions & 0 deletions tests/source/issue-2896.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {} }