Skip to content
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

fix: remove multiple trailing lines in comments #6163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Remove multiple trailing lines in comments
Previously, rustfmt would remove one blank, trailing line in a comment
every time it ran, so formatting was not idempotent if a comment had
multiple trailing lines. Comments are only reformatted in this way if
`comment::rewrite_comment_inner` is called, which is enabled by any of the
config options `normalize_comments`, `wrap_comments`, or
`format_code_in_doc_comments` (for doc comments only). Absent those
config options, no trailing line reformatting occurs at all.

In this commit, when the existing condition that detects a blank, trailing
line is true, any preceding blank lines are removed from the reformatted
comment. A source/target "system test" was added to prevent regression.

Signed-off-by: Ross Williams <[email protected]>
  • Loading branch information
overhacked committed Jul 31, 2024
commit c7504a2269b831438672a436d97777e540060faf
8 changes: 7 additions & 1 deletion src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 811,13 @@ impl<'a> CommentRewrite<'a> {
} else if self.is_prev_line_multi_line && !line.is_empty() {
self.result.push(' ')
} else if is_last && line.is_empty() {
// trailing blank lines are unwanted
// Trailing blank lines are unwanted; if the last line is blank, look for additional,
// preceding blank lines to remove, also.
let trailing_line = self.comment_line_separator.trim_end_matches(' ');
overhacked marked this conversation as resolved.
Show resolved Hide resolved
while self.result.ends_with(trailing_line) {
self.result
.truncate(self.result.len() - trailing_line.len());
}
if !self.closer.is_empty() {
self.result.push_str(&self.indent_str);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/source/comment_trailing_lines.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
// rustfmt-wrap_comments: true

//! Module comment with multiple trailing lines
//!
//!

/// Doc comment with multiple trailing lines
///
///
pub mod foo {}

/// Doc comment with one trailing line
///
pub mod bar {}

/*
* Block comment with multiple trailing lines
*
*
*/
pub mod block {}
overhacked marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 14 additions & 0 deletions tests/target/comment_trailing_lines.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@
// rustfmt-wrap_comments: true

//! Module comment with multiple trailing lines

/// Doc comment with multiple trailing lines
pub mod foo {}

/// Doc comment with one trailing line
pub mod bar {}

/*
* Block comment with multiple trailing lines
*/
pub mod block {}