Skip to content

add newline when the last line has comment #4019

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 1 commit 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
6 changes: 6 additions & 0 deletions rustfmt-core/rustfmt-lib/src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ impl<'a> FmtVisitor<'a> {
self.last_pos = end;
return;
}

let last_line_str = self.buffer.lines().last().unwrap_or("");
if !missing_snippet.starts_with("\n") && last_line_str.find("//").is_some() {
self.push_str("\n");
}

self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
}

Expand Down
9 changes: 9 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue-3988.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-normalize_doc_attributes: true
// rustfmt-max_width: 10

mod a {
mod b {
#[doc = "test"]fn foo() -> () {
}
}
}
10 changes: 10 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-3988.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-normalize_doc_attributes: true
// rustfmt-max_width: 10

mod a {
mod b {
///test
fn foo() -> (){
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This bug is caused by the case when the rustfmt fails to format because the width is greater than max_width in rustfmt.toml.
In this case, this line is greater than 10. rustfmt seems to remove the white space and emits the not formatted string. I think that's the reason why the implementation exists after comment on the same line.

Copy link
Member

Choose a reason for hiding this comment

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

The indentation of foo looks/feels a bit odd 🤔

I feel like we may need a slightly different solution in order to maintain the target indent (since this exceeds the max_width anyway)

}
}
}