Skip to content

add trim to avoid internal error #3905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {

for (kind, offset, sub_slice) in CommentCodeSlices::new(self.snippet(span)) {
let sub_slice = transform_missing_snippet(config, sub_slice);

debug!("close_block: {:?} {:?} {:?}", kind, offset, sub_slice);

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

// put the other lines below it, shaping it as needed
let other_lines = &sub_slice[offset + 1..];
let comment_str =
rewrite_comment(other_lines, false, comment_shape, config);
match comment_str {
Some(ref s) => self.push_str(s),
None => self.push_str(other_lines),
if !other_lines.trim().is_empty() {
self.push_str(
&self.block_indent.to_string_with_newline(config),
);
let comment_str =
rewrite_comment(other_lines, false, comment_shape, config);
match comment_str {
Some(ref s) => self.push_str(s),
None => self.push_str(other_lines),
}
}
}
}
} else {
let sub_slice = sub_slice.trim();
if comment_on_same_line {
// 1 = a space before `//`
let offset_len = 1 + last_line_width(&self.buffer)
Expand Down
7 changes: 7 additions & 0 deletions tests/source/issue-3904/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-version: One

fn main() {
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */


}
7 changes: 7 additions & 0 deletions tests/source/issue-3904/two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-version: Two

fn main() {
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */


}
5 changes: 5 additions & 0 deletions tests/target/issue-3904/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-version: One

fn main() {
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */
}
5 changes: 5 additions & 0 deletions tests/target/issue-3904/two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-version: Two

fn main() {
let checkkwd = (0x2i32 | 0x1i32) as i64; /* unrecognized "\z": print both chars unless ' or " */
}