From 4c9ef93df7e7984bbe1077aa3c24ae538fa0a42c Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Tue, 8 May 2018 20:31:33 +0200 Subject: [PATCH] fix: Don't insert an extra brace in macros with native newlines Due to `format_snippet` formatting the input with \r\n the subtraction would wouldn't give a length that removed the } Fixes #2641 --- src/lib.rs | 2 +- tests/config/issue-2641.toml | 1 + tests/source/issue-2641.rs | 3 +++ tests/target/issue-2641.rs | 3 +++ 4 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/config/issue-2641.toml create mode 100644 tests/source/issue-2641.rs create mode 100644 tests/target/issue-2641.rs diff --git a/src/lib.rs b/src/lib.rs index e8bd331f9680..80b0764e513e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -645,7 +645,7 @@ pub fn format_code_block(code_snippet: &str, config: &Config) -> Option // then unindent the whole code block. let formatted = format_snippet(&snippet, config)?; // 2 = "}\n" - let block_len = formatted.len().checked_sub(2).unwrap_or(0); + let block_len = formatted.rfind('}').unwrap_or(formatted.len()); let mut is_indented = true; for (kind, ref line) in LineClasses::new(&formatted[FN_MAIN_PREFIX.len()..block_len]) { if !is_first { diff --git a/tests/config/issue-2641.toml b/tests/config/issue-2641.toml new file mode 100644 index 000000000000..11c9dca8a06c --- /dev/null +++ b/tests/config/issue-2641.toml @@ -0,0 +1 @@ +newline_style = "Windows" diff --git a/tests/source/issue-2641.rs b/tests/source/issue-2641.rs new file mode 100644 index 000000000000..c7ad60674fd5 --- /dev/null +++ b/tests/source/issue-2641.rs @@ -0,0 +1,3 @@ +macro_rules! a { + () => {{}} +} diff --git a/tests/target/issue-2641.rs b/tests/target/issue-2641.rs new file mode 100644 index 000000000000..fbf5326c330d --- /dev/null +++ b/tests/target/issue-2641.rs @@ -0,0 +1,3 @@ +macro_rules! a { + () => {{}}; +}