about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhafiz <20735482+ayazhafiz@users.noreply.github.com>2020-05-10 06:54:46 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2020-11-29 13:26:58 -0600
commitc77c6a405d9584a8b3b2d7b18bd9153d19ddb6ff (patch)
treed882c32e03b22ab92dedbb82b084b641a3969912
parente7ecdc16642a364725f13d472dd99b707a47954b (diff)
downloadrust-c77c6a405d9584a8b3b2d7b18bd9153d19ddb6ff.tar.gz
rust-c77c6a405d9584a8b3b2d7b18bd9153d19ddb6ff.zip
Compare code block line indentation with config whitespace (#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 #4152
-rw-r--r--src/lib.rs4
-rw-r--r--tests/target/issue-4152.rs18
2 files changed, 20 insertions, 2 deletions
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 format_code_block(
         .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 format_code_block(
             // 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);
+	}};
+}