Skip to content

Commit e28eb9e

Browse files
authored
Don't format statements that are outside of the selected file-lines range (#6867)
* Don't format statements outide of file lines * Check for file lines before handling empty statements * Add test for empty statements
1 parent 5f206e9 commit e28eb9e

5 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/visitor.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
118118
fn visit_stmt(&mut self, stmt: &Stmt<'_>, include_empty_semi: bool) {
119119
debug!("visit_stmt: {}", self.psess.span_to_debug_info(stmt.span()));
120120

121+
// Preserve original source snippet if the statement isn't in the selected file lines.
122+
if out_of_file_lines_range!(self, stmt.span()) {
123+
let stmt_span = source!(self, stmt.span());
124+
self.push_str(self.snippet(mk_sp(self.last_pos, stmt_span.hi())));
125+
self.last_pos = stmt_span.hi();
126+
return;
127+
}
128+
121129
if stmt.is_empty() {
122130
// If the statement is empty, just skip over it. Before that, make sure any comment
123131
// snippet preceding the semicolon is picked up.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/issue-6863/empty-stmt.rs","range":[5,5]}]
2+
3+
fn main() {
4+
;
5+
println!("b");
6+
;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/issue-6863/fn-stmts.rs","range":[5,5]}]
2+
3+
fn main() {
4+
println!("a");
5+
println!("b");
6+
println!("c");
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/issue-6863/empty-stmt.rs","range":[5,5]}]
2+
3+
fn main() {
4+
;
5+
println!("b");
6+
;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/issue-6863/fn-stmts.rs","range":[5,5]}]
2+
3+
fn main() {
4+
println!("a");
5+
println!("b");
6+
println!("c");
7+
}

0 commit comments

Comments
 (0)