Skip to content

Commit ec24856

Browse files
committed
Add tests and fixes for the two remaining issues.
This fixes the issue of unwanted changes in the following situations: * Space after a doc comment * Space before a comment in the last line of the file (without trailing newline)
1 parent 834d33c commit ec24856

6 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/missed_spans.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,20 @@ impl<'a> FmtVisitor<'a> {
364364
}
365365
}
366366

367-
let remaining = snippet[status.line_start..subslice.len() + offset].trim();
368-
if !remaining.is_empty() {
369-
self.push_str(&self.block_indent.to_string(self.config));
370-
self.push_str(remaining);
371-
status.line_start = subslice.len() + offset;
367+
let mut remaining = &snippet[status.line_start..subslice.len() + offset];
368+
status.line_start = subslice.len() + offset;
369+
370+
let skip_this_line = !self
371+
.config
372+
.file_lines()
373+
.contains_line(file_name, status.cur_line);
374+
if !skip_this_line {
375+
remaining = remaining.trim();
376+
if !remaining.is_empty() {
377+
self.push_str(&self.block_indent.to_string(self.config));
378+
}
372379
}
380+
381+
self.push_str(remaining);
373382
}
374383
}

src/visitor.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
895895
return false;
896896
}
897897

898-
let rewrite = attrs.rewrite(&self.get_context(), self.shape());
899898
let span = mk_sp(attrs[0].span.lo(), attrs[attrs.len() - 1].span.hi());
899+
if out_of_file_lines_range!(self, span) {
900+
return false;
901+
}
902+
903+
let rewrite = attrs.rewrite(&self.get_context(), self.shape());
900904
self.push_rewrite(span, rewrite);
901905

902906
false

tests/source/issue-5136-4.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-file_lines: []
2+
// Test that a missing space at the end of a doc comment is preserved when the
3+
// line is not in the --file-lines range.
4+
//!

tests/source/issue-5136-5.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-file_lines: []
2+
// Test that the space before the comment is not removed if the line is not
3+
// contained in `--file-lines`.
4+
// Note: It's important for the bug to repro that there is no newline at the
5+
// end of the comment
6+
fn f(){} // what

tests/target/issue-5136-4.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-file_lines: []
2+
// Test that a missing space at the end of a doc comment is preserved when the
3+
// line is not in the --file-lines range.
4+
//!

tests/target/issue-5136-5.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-file_lines: []
2+
// Test that the space before the comment is not removed if the line is not
3+
// contained in `--file-lines`.
4+
// Note: It's important for the bug to repro that there is no newline at the
5+
// end of the comment
6+
fn f(){} // what

0 commit comments

Comments
 (0)