diff --git a/src/lib.rs b/src/lib.rs index 753840e065c..da83b285a21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,6 +371,7 @@ fn enclose_in_main_block(s: &str, config: &Config) -> String { .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'); @@ -385,9 +386,8 @@ fn enclose_in_main_block(s: &str, config: &Config) -> String { // 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 diff --git a/tests/target/issue-4152.rs b/tests/target/issue-4152.rs new file mode 100644 index 00000000000..80f9ff5e304 --- /dev/null +++ b/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); + }}; +}