Skip to content

Commit 09e44b2

Browse files
authored
Merge pull request #2351 from topecongiro/issue-2256
Use correct BytePos when recovering comments on removed import
2 parents be00003 + 949708f commit 09e44b2

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/imports.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,14 @@ impl<'a> FmtVisitor<'a> {
314314
Some(ref s) if s.is_empty() => {
315315
// Format up to last newline
316316
let prev_span = mk_sp(self.last_pos, source!(self, span).lo());
317-
let span_end = match self.snippet(prev_span).rfind('\n') {
318-
Some(offset) => self.last_pos + BytePos(offset as u32),
319-
None => source!(self, span).lo(),
320-
};
317+
let trimmed_snippet = self.snippet(prev_span).trim_right();
318+
let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32);
321319
self.format_missing(span_end);
320+
// We have an excessive newline from the removed import.
321+
if self.buffer.ends_with('\n') {
322+
self.buffer.pop();
323+
self.line_number -= 1;
324+
}
322325
self.last_pos = source!(self, span).hi();
323326
}
324327
Some(ref s) => {

tests/source/issue-2256.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// こんにちは
2+
use std::{};
3+
use std::borrow::Cow;
4+
5+
/* comment */ use std::{};
6+
/* comment */ use std::{};
7+
8+
9+
10+
11+
12+
/* comment */ use std::{};

tests/target/issue-2256.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// こんにちは
2+
use std::borrow::Cow;
3+
4+
/* comment */
5+
/* comment */
6+
7+
/* comment */

0 commit comments

Comments
 (0)