From ab42c02f2cae63e9e310d87d9fe835ff699df50f Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 9 May 2020 19:57:41 -0700 Subject: [PATCH] Compare code block line indentation with config whitespace 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 --- rustfmt-core/rustfmt-lib/src/lib.rs | 4 ++-- .../rustfmt-lib/tests/target/issue-4152.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 rustfmt-core/rustfmt-lib/tests/target/issue-4152.rs diff --git a/rustfmt-core/rustfmt-lib/src/lib.rs b/rustfmt-core/rustfmt-lib/src/lib.rs index 4dc0981b6c6..29e2a7c360e 100644 --- a/rustfmt-core/rustfmt-lib/src/lib.rs +++ b/rustfmt-core/rustfmt-lib/src/lib.rs @@ -350,6 +350,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option Option 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 diff --git a/rustfmt-core/rustfmt-lib/tests/target/issue-4152.rs b/rustfmt-core/rustfmt-lib/tests/target/issue-4152.rs new file mode 100644 index 00000000000..80f9ff5e304 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/issue-4152.rs @@ -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); + }}; +}