From 00a20bceff40e98034a7346e5cc6319cbd2d21b7 Mon Sep 17 00:00:00 2001 From: Ivan Komarov Date: Sat, 27 Oct 2018 04:01:37 +0300 Subject: [PATCH] Fix formatting failures on Windows When newline_style is set to Windows, an empty line inside of a macro results in `\r` being passed to the `fold()` in `MacroBranch::rewrite()`. `\r` is technically not an empty string, so we try to indent it, leaving trailing whitespaces behind, even though that was not intended (as far as I can see). This commit replaces the `!l.is_empty()` check with calling `is_empty_line()`, since trying to indent any whitespace-only string will probably result in problematic trailing whitespaces. Fixes: #2810 --- src/macros.rs | 2 +- tests/target/issue-2810.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/target/issue-2810.rs diff --git a/src/macros.rs b/src/macros.rs index d378bb8ffb14..43f3071b70a6 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1347,7 +1347,7 @@ impl MacroBranch { .fold( (String::new(), true), |(mut s, need_indent), (i, (kind, ref l))| { - if !l.is_empty() + if !is_empty_line(l) && need_indent && !new_body_snippet.is_line_non_formatted(i + 1) { diff --git a/tests/target/issue-2810.rs b/tests/target/issue-2810.rs new file mode 100644 index 000000000000..34140c7a1fc6 --- /dev/null +++ b/tests/target/issue-2810.rs @@ -0,0 +1,14 @@ +// rustfmt-newline_style: Windows + +#[macro_export] +macro_rules! hmmm___ffi_error { + ($result:ident) => { + pub struct $result { + success: bool, + } + + impl $result { + pub fn foo(self) {} + } + }; +}