Skip to content

Commit c77c6a4

Browse files
ayazhafizcalebcartwright
authored andcommitted
Compare code block line indentation with config whitespace (rust-lang#4166)
Previously the indetation of a line was compared with the configured number of spaces per tab, which could cause lines that were formatted with hard tabs not to be recognized as indented ("\t".len() < " ".len()). Closes rust-lang#4152
1 parent e7ecdc1 commit c77c6a4

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ fn format_code_block(
371371
.rfind('}')
372372
.unwrap_or_else(|| formatted.snippet.len());
373373
let mut is_indented = true;
374+
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
374375
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
375376
if !is_first {
376377
result.push('\n');
@@ -385,9 +386,8 @@ fn format_code_block(
385386
// are too long, or we have failed to format code block. We will be
386387
// conservative and just return `None` in this case.
387388
return None;
388-
} else if line.len() > config.tab_spaces() {
389+
} else if line.len() > indent_str.len() {
389390
// Make sure that the line has leading whitespaces.
390-
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
391391
if line.starts_with(indent_str.as_ref()) {
392392
let offset = if config.hard_tabs() {
393393
1

tests/target/issue-4152.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// rustfmt-hard_tabs: true
2+
3+
macro_rules! bit {
4+
($bool:expr) => {
5+
if $bool {
6+
1;
7+
1
8+
} else {
9+
0;
10+
0
11+
}
12+
};
13+
}
14+
macro_rules! add_one {
15+
($vec:expr) => {{
16+
$vec.push(1);
17+
}};
18+
}

0 commit comments

Comments
 (0)