Skip to content

Commit 5b0ce0e

Browse files
rchaser53topecongiro
authored andcommitted
avoid not to truncate necessary chars (#3640)
1 parent 1d19a08 commit 5b0ce0e

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/formatting.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
177177
visitor.format_separate_mod(module, &*source_file);
178178
};
179179

180-
debug_assert_eq!(visitor.line_number, count_newlines(&visitor.buffer));
180+
debug_assert_eq!(
181+
visitor.line_number,
182+
count_newlines(&visitor.buffer),
183+
"failed in format_file visitor.buffer:\n {:?}",
184+
&visitor.buffer
185+
);
181186

182187
// For some reason, the source_map does not include terminating
183188
// newlines so we must add one on for each file. This is sad.

src/visitor.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,20 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
257257
} else {
258258
self.config.tab_spaces()
259259
};
260-
self.buffer.truncate(total_len - chars_too_many);
260+
261+
// FIXME this is a temporaly fix
262+
// should be remove truncate logic in close_block
263+
// avoid not to truncate necessary chars
264+
let truncate_start = total_len - chars_too_many;
265+
let target_str = &self.buffer[truncate_start..total_len];
266+
let truncate_length = target_str.len() - target_str.trim().len();
267+
268+
if let Some(last_char) = target_str.chars().last() {
269+
self.buffer.truncate(total_len - truncate_length);
270+
if last_char == '\n' {
271+
self.buffer.push_str("\n");
272+
}
273+
}
261274
}
262275
self.push_str("}");
263276
self.block_indent = self.block_indent.block_unindent(self.config);

tests/source/issue-3636.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/issue-3636.rs","range":[4,7]}]
2+
3+
fn foo() {
4+
let x =
5+
42;
6+
let y =
7+
42;
8+
let z = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
9+
let z = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
10+
}

tests/target/issue-3636.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/issue-3636.rs","range":[4,7]}]
2+
3+
fn foo() {
4+
let x = 42;
5+
let y = 42;
6+
let z = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
7+
let z = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
8+
}

0 commit comments

Comments
 (0)