From f815858420980bc4f9119731647a0fae9e7978c7 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 4 Feb 2018 17:21:10 +0900 Subject: [PATCH] Use correct offset when unindenting code block When using hard tabs, we should only remove '\t'. --- src/lib.rs | 7 ++++++- tests/target/issue-2401.rs | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/target/issue-2401.rs diff --git a/src/lib.rs b/src/lib.rs index 773567e5f88b..2b89bde72c16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -571,7 +571,12 @@ pub fn format_code_block(code_snippet: &str, config: &Config) -> Option let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config); if line.starts_with(indent_str.as_ref()) { - &line[config.tab_spaces()..] + let offset = if config.hard_tabs() { + 1 + } else { + config.tab_spaces() + }; + &line[offset..] } else { line } diff --git a/tests/target/issue-2401.rs b/tests/target/issue-2401.rs new file mode 100644 index 000000000000..ec8f27b732ff --- /dev/null +++ b/tests/target/issue-2401.rs @@ -0,0 +1,7 @@ +// rustfmt-hard_tabs = true +// rustfmt-normalize_comments = true + +/// ``` +/// println!("Hello, World!"); +/// ``` +fn main() {}