Skip to content

Commit

Permalink
Compare code block line indentation with config whitespace (#4166)
Browse files Browse the repository at this point in the history
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 #4152
  • Loading branch information
ayazhafiz authored May 10, 2020
1 parent 93345f5 commit 2836f9b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rustfmt-core/rustfmt-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
.rfind('}')
.unwrap_or_else(|| formatted.snippet.len());
let mut is_indented = true;
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
if !is_first {
result.push('\n');
Expand All @@ -364,9 +365,8 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
// are too long, or we have failed to format code block. We will be
// conservative and just return `None` in this case.
return None;
} else if line.len() > config.tab_spaces() {
} else if line.len() > indent_str.len() {
// Make sure that the line has leading whitespaces.
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
if line.starts_with(indent_str.as_ref()) {
let offset = if config.hard_tabs() {
1
Expand Down
18 changes: 18 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-4152.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// rustfmt-hard_tabs: true

macro_rules! bit {
($bool:expr) => {
if $bool {
1;
1
} else {
0;
0
}
};
}
macro_rules! add_one {
($vec:expr) => {{
$vec.push(1);
}};
}

0 comments on commit 2836f9b

Please sign in to comment.