Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ impl<'a> FmtVisitor<'a> {
.rev()
.find(|rev_c| ![' ', '\t'].contains(rev_c));

let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
let starts_output_line = !self.buffer.is_empty() && self.buffer.ends_with('\n');

@matthewhughes934 matthewhughes934 Sep 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand this fix from a glance, can you explain what is the importance of self.buffer ending in a newline?

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

self.buffer ending in \n means the next comment starts a new output line. For /*A*/ /*B*/, writing /*A*/ adds a newline via the _ => arm at the end of process_comment. But last_char still sees the closing / of /*A*/ in the original source, so fix_indent is false and /*B*/ gets one space instead of the block indent. starts_output_line catches this and applies self.block_indent on the first pass, so reformatting no longer changes the output.

I hope this clears it up

let fix_indent =
starts_output_line || last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
let mut on_same_line = false;

let comment_indent = if fix_indent {
Expand Down
36 changes: 36 additions & 0 deletions tests/source/issue-7019.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
fn two() -> usize {
/*A*/ /*B*/
1 + 2
}

fn three() -> usize {
/*A*/ /*B*/ /*C*/
1 + 2
}

fn nested() -> usize {
{
{
/*A*/ /*B*/ /*C*/
1 + 2
}
}
}

fn between_statements() -> usize {
let a = 1;
/*A*/ /*B*/
let b = 2;
a + b
}

fn trailing_inline() -> usize {
let a = 1; /*trailing*/
a
}

fn already_canonical() -> usize {
/*A*/
/*B*/
1 + 2
}
42 changes: 42 additions & 0 deletions tests/target/issue-7019.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
fn two() -> usize {
/*A*/
/*B*/
1 + 2
}

fn three() -> usize {
/*A*/
/*B*/
/*C*/
1 + 2
}

fn nested() -> usize {
{
{
/*A*/
/*B*/
/*C*/
1 + 2
}
}
}

fn between_statements() -> usize {
let a = 1;
/*A*/
/*B*/
let b = 2;
a + b
}

fn trailing_inline() -> usize {
let a = 1; /*trailing*/
a
}

fn already_canonical() -> usize {
/*A*/
/*B*/
1 + 2
}