diff --git a/src/visitor.rs b/src/visitor.rs index 6048084c8da..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. 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/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/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"); +; +} 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"); +}