Skip to content

Commit 532973c

Browse files
rchaser53topecongiro
authored andcommitted
add trim to avoid internal error (#3905)
1 parent 7037481 commit 532973c

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

src/visitor.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
248248

249249
for (kind, offset, sub_slice) in CommentCodeSlices::new(self.snippet(span)) {
250250
let sub_slice = transform_missing_snippet(config, sub_slice);
251-
252251
debug!("close_block: {:?} {:?} {:?}", kind, offset, sub_slice);
253252

254253
match kind {
@@ -277,19 +276,24 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
277276
Some(offset) => {
278277
let first_line = &sub_slice[..offset];
279278
self.push_str(first_line);
280-
self.push_str(&self.block_indent.to_string_with_newline(config));
281279

282280
// put the other lines below it, shaping it as needed
283281
let other_lines = &sub_slice[offset + 1..];
284-
let comment_str =
285-
rewrite_comment(other_lines, false, comment_shape, config);
286-
match comment_str {
287-
Some(ref s) => self.push_str(s),
288-
None => self.push_str(other_lines),
282+
if !other_lines.trim().is_empty() {
283+
self.push_str(
284+
&self.block_indent.to_string_with_newline(config),
285+
);
286+
let comment_str =
287+
rewrite_comment(other_lines, false, comment_shape, config);
288+
match comment_str {
289+
Some(ref s) => self.push_str(s),
290+
None => self.push_str(other_lines),
291+
}
289292
}
290293
}
291294
}
292295
} else {
296+
let sub_slice = sub_slice.trim();
293297
if comment_on_same_line {
294298
// 1 = a space before `//`
295299
let offset_len = 1 + last_line_width(&self.buffer)

tests/source/issue-3904/one.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-version: One
2+
3+
fn main() {
4+
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */
5+
6+
7+
}

tests/source/issue-3904/two.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-version: Two
2+
3+
fn main() {
4+
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */
5+
6+
7+
}

tests/target/issue-3904/one.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-version: One
2+
3+
fn main() {
4+
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */
5+
}

tests/target/issue-3904/two.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-version: Two
2+
3+
fn main() {
4+
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */
5+
}

0 commit comments

Comments
 (0)