Skip to content

Commit e086436

Browse files
ayazhafizbradleypmartin
authored and
bradleypmartin
committed
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 dc5fb24 commit e086436

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

rustfmt-core/rustfmt-lib/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
350350
.rfind('}')
351351
.unwrap_or_else(|| formatted.snippet.len());
352352
let mut is_indented = true;
353+
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
353354
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
354355
if !is_first {
355356
result.push('\n');
@@ -364,9 +365,8 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
364365
// are too long, or we have failed to format code block. We will be
365366
// conservative and just return `None` in this case.
366367
return None;
367-
} else if line.len() > config.tab_spaces() {
368+
} else if line.len() > indent_str.len() {
368369
// Make sure that the line has leading whitespaces.
369-
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
370370
if line.starts_with(indent_str.as_ref()) {
371371
let offset = if config.hard_tabs() {
372372
1
Lines changed: 18 additions & 0 deletions
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)