From 27655d5716db9bf265229a06b346226c384407a0 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 16 Apr 2026 10:57:31 -0700 Subject: [PATCH 1/3] Don't format statements outide of file lines --- src/visitor.rs | 8 ++++++++ tests/source/issue-6863/fn-stmts.rs | 7 +++++++ tests/target/issue-6863/fn-stmts.rs | 7 +++++++ 3 files changed, 22 insertions(+) create mode 100644 tests/source/issue-6863/fn-stmts.rs create mode 100644 tests/target/issue-6863/fn-stmts.rs diff --git a/src/visitor.rs b/src/visitor.rs index 6048084c8da..a95632d4402 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -148,6 +148,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { return; } + // Preserve original source snippet if the statement isn't in the selected file lines. + if out_of_file_lines_range!(self, stmt.span()) { + let stmt_span = source!(self, stmt.span()); + self.push_str(self.snippet(mk_sp(self.last_pos, stmt_span.hi()))); + self.last_pos = stmt_span.hi(); + return; + } + match stmt.as_ast_node().kind { ast::StmtKind::Item(ref item) => { self.visit_item(item); diff --git a/tests/source/issue-6863/fn-stmts.rs b/tests/source/issue-6863/fn-stmts.rs new file mode 100644 index 00000000000..e677f842bd5 --- /dev/null +++ b/tests/source/issue-6863/fn-stmts.rs @@ -0,0 +1,7 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6863/fn-stmts.rs","range":[5,5]}] + +fn main() { +println!("a"); +println!("b"); +println!("c"); +} diff --git a/tests/target/issue-6863/fn-stmts.rs b/tests/target/issue-6863/fn-stmts.rs new file mode 100644 index 00000000000..82ed6dc37c8 --- /dev/null +++ b/tests/target/issue-6863/fn-stmts.rs @@ -0,0 +1,7 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6863/fn-stmts.rs","range":[5,5]}] + +fn main() { +println!("a"); + println!("b"); +println!("c"); +} From b6e660c39a013e0609db36fc4e28e49026d598ea Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 16 Apr 2026 11:49:57 -0700 Subject: [PATCH 2/3] Check for file lines before handling empty statements --- src/visitor.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/visitor.rs b/src/visitor.rs index a95632d4402..52a1287468e 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -118,6 +118,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { fn visit_stmt(&mut self, stmt: &Stmt<'_>, include_empty_semi: bool) { debug!("visit_stmt: {}", self.psess.span_to_debug_info(stmt.span())); + // Preserve original source snippet if the statement isn't in the selected file lines. + if out_of_file_lines_range!(self, stmt.span()) { + let stmt_span = source!(self, stmt.span()); + self.push_str(self.snippet(mk_sp(self.last_pos, stmt_span.hi()))); + self.last_pos = stmt_span.hi(); + return; + } + if stmt.is_empty() { // If the statement is empty, just skip over it. Before that, make sure any comment // snippet preceding the semicolon is picked up. @@ -148,14 +156,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { return; } - // Preserve original source snippet if the statement isn't in the selected file lines. - if out_of_file_lines_range!(self, stmt.span()) { - let stmt_span = source!(self, stmt.span()); - self.push_str(self.snippet(mk_sp(self.last_pos, stmt_span.hi()))); - self.last_pos = stmt_span.hi(); - return; - } - match stmt.as_ast_node().kind { ast::StmtKind::Item(ref item) => { self.visit_item(item); From d48d03017d5dcf807c703e0eeb0454594210d5dc Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 16 Apr 2026 11:58:13 -0700 Subject: [PATCH 3/3] Add test for empty statements --- tests/source/issue-6863/empty-stmt.rs | 7 +++++++ tests/target/issue-6863/empty-stmt.rs | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 tests/source/issue-6863/empty-stmt.rs create mode 100644 tests/target/issue-6863/empty-stmt.rs diff --git a/tests/source/issue-6863/empty-stmt.rs b/tests/source/issue-6863/empty-stmt.rs new file mode 100644 index 00000000000..050fbb9c525 --- /dev/null +++ b/tests/source/issue-6863/empty-stmt.rs @@ -0,0 +1,7 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6863/empty-stmt.rs","range":[5,5]}] + +fn main() { +; +println!("b"); +; +} diff --git a/tests/target/issue-6863/empty-stmt.rs b/tests/target/issue-6863/empty-stmt.rs new file mode 100644 index 00000000000..b67340f5bc0 --- /dev/null +++ b/tests/target/issue-6863/empty-stmt.rs @@ -0,0 +1,7 @@ +// rustfmt-file_lines: [{"file":"tests/source/issue-6863/empty-stmt.rs","range":[5,5]}] + +fn main() { +; + println!("b"); +; +}