diff --git a/src/items.rs b/src/items.rs index daa3df9e5d0..2648c28b72b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2630,29 +2630,38 @@ fn rewrite_fn_base( } // Comment between return type and the end of the decl. - let snippet_lo = fd.output.span().hi(); if where_clause.predicates.is_empty() { + let snippet_lo = fd.output.span().hi(); let snippet_hi = span.hi(); - let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi)); - // Try to preserve the layout of the original snippet. - let original_starts_with_newline = snippet - .find(|c| c != ' ') - .map_or(false, |i| starts_with_newline(&snippet[i..])); - let original_ends_with_newline = snippet - .rfind(|c| c != ' ') - .map_or(false, |i| snippet[i..].ends_with('\n')); - let snippet = snippet.trim(); - if !snippet.is_empty() { - result.push(if original_starts_with_newline { - '\n' - } else { - ' ' - }); - result.push_str(snippet); - if original_ends_with_newline { - force_new_line_for_brace = true; + + let mut deal_snippet = |snippet: &str| { + // Try to preserve the layout of the original snippet. + let original_starts_with_newline = snippet + .find(|c| c != ' ') + .map_or(false, |i| starts_with_newline(&snippet[i..])); + let original_ends_with_newline = snippet + .rfind(|c| c != ' ') + .map_or(false, |i| snippet[i..].ends_with('\n')); + let snippet = snippet.trim(); + if !snippet.is_empty() { + result.push(if original_starts_with_newline { + '\n' + } else { + ' ' + }); + result.push_str(snippet); + if original_ends_with_newline { + force_new_line_for_brace = true; + } } - } + }; + + if context.config.version() == Version::Two && where_clause.has_where_token { + deal_snippet(context.snippet(mk_sp(snippet_lo, where_clause.span.lo()))); + deal_snippet(context.snippet(mk_sp(where_clause.span.hi(), snippet_hi))); + } else { + deal_snippet(context.snippet(mk_sp(snippet_lo, snippet_hi))); + }; } } diff --git a/tests/source/empty-where-clauses.rs b/tests/source/empty-where-clauses.rs new file mode 100644 index 00000000000..e362db7e0e8 --- /dev/null +++ b/tests/source/empty-where-clauses.rs @@ -0,0 +1,6 @@ +// rustfmt-version: Two + +fn foo() -> () where {} +fn bar() -> () /* comment */ where {} +fn baz() -> () where /* comment */ {} +fn qux() -> () /* comment */ where /* comment */ {} diff --git a/tests/target/empty-where-clauses.rs b/tests/target/empty-where-clauses.rs new file mode 100644 index 00000000000..40d80140ca0 --- /dev/null +++ b/tests/target/empty-where-clauses.rs @@ -0,0 +1,6 @@ +// rustfmt-version: Two + +fn foo() -> () {} +fn bar() -> () /* comment */ {} +fn baz() -> () /* comment */ {} +fn qux() -> () /* comment */ /* comment */ {}