From 1e7ff38c2dcbe3df62a2ec91182b5f72b348aed2 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 10 Jan 2018 21:30:13 +0900 Subject: [PATCH 1/2] Add a test for #2256 --- tests/source/issue-2256.rs | 12 ++++++++++++ tests/target/issue-2256.rs | 7 +++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/source/issue-2256.rs create mode 100644 tests/target/issue-2256.rs diff --git a/tests/source/issue-2256.rs b/tests/source/issue-2256.rs new file mode 100644 index 00000000000..b505b96e2b3 --- /dev/null +++ b/tests/source/issue-2256.rs @@ -0,0 +1,12 @@ +// こんにちは +use std::{}; +use std::borrow::Cow; + +/* comment */ use std::{}; +/* comment */ use std::{}; + + + + + +/* comment */ use std::{}; diff --git a/tests/target/issue-2256.rs b/tests/target/issue-2256.rs new file mode 100644 index 00000000000..4b546223f94 --- /dev/null +++ b/tests/target/issue-2256.rs @@ -0,0 +1,7 @@ +// こんにちは +use std::borrow::Cow; + +/* comment */ +/* comment */ + +/* comment */ From 949708f617ae7e815da8ebd69b7128c5ae184299 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 10 Jan 2018 21:30:41 +0900 Subject: [PATCH 2/2] Use correct BytePos when recovering comments on removed import --- src/imports.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index b940dcc9de7..c49a95b6ff7 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -314,11 +314,14 @@ impl<'a> FmtVisitor<'a> { Some(ref s) if s.is_empty() => { // Format up to last newline let prev_span = mk_sp(self.last_pos, source!(self, span).lo()); - let span_end = match self.snippet(prev_span).rfind('\n') { - Some(offset) => self.last_pos + BytePos(offset as u32), - None => source!(self, span).lo(), - }; + let trimmed_snippet = self.snippet(prev_span).trim_right(); + let span_end = self.last_pos + BytePos(trimmed_snippet.len() as u32); self.format_missing(span_end); + // We have an excessive newline from the removed import. + if self.buffer.ends_with('\n') { + self.buffer.pop(); + self.line_number -= 1; + } self.last_pos = source!(self, span).hi(); } Some(ref s) => {