Skip to content

Commit 1e754ac

Browse files
committed
Fix panic in annotated_snippet dependency (GitHub issue #4968).
* Internally, rustfmt preserves tabs and counts them as multiple characters (based on configuration). * The annoted_snippet dependency counts tabs as 1 character. * If rustfmt produces an error on a line containing tabs, annotated_snippet may think that the error is out of range and panic. * Have rustfmt internally replace tabs with the corresponding number of spaces, so that columns can be counted unambiguously. * This change is based on PR #5039 by karyon, but with the code review suggestions by camsteffen.
1 parent ee2bed9 commit 1e754ac

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/formatting.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,14 @@ impl<'a> FormatLines<'a> {
574574
fn char(&mut self, c: char, kind: FullCodeCharKind) {
575575
self.newline_count = 0;
576576
self.line_len += if c == '\t' {
577+
self.line_buffer
578+
.push_str(&" ".repeat(self.config.tab_spaces()));
577579
self.config.tab_spaces()
578580
} else {
579-
1
581+
self.line_buffer.push(c);
582+
c.len_utf8()
580583
};
581584
self.last_was_space = c.is_whitespace();
582-
self.line_buffer.push(c);
583585
if kind.is_string() {
584586
self.current_line_contains_string_literal = true;
585587
}

tests/target/issue-4968.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-hard_tabs: true
2+
// rustfmt-max_width: 40
3+
// rustfmt-error_on_unformatted: true
4+
5+
fn foo(x: u32) {
6+
if x > 10 {
7+
if x > 20 {
8+
println!("0123456789abcdefghijklmnopqrstuvwxyz");
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)