diff --git a/src/matches.rs b/src/matches.rs index 06e7078b11f..ea15691ec59 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -3,7 +3,7 @@ use std::iter::repeat; use rustc_ast::{ast, ptr}; -use rustc_span::{BytePos, Span}; +use rustc_span::{BytePos, Pos, Span}; use crate::comment::{combine_strs_with_missing_comments, rewrite_comment, FindUncommented}; use crate::config::lists::*; @@ -121,6 +121,30 @@ pub(crate) fn rewrite_match( inner_attrs[inner_attrs.len() - 1].span.hi() }; + let blank_line_after_inner_attrs = if context.config.version() == Version::Two + && !inner_attrs.is_empty() + && !arms.is_empty() + { + let blank_line_snip = if let Some(hi) = context + .snippet(mk_sp(open_brace_pos, arms[0].span.lo())) + .find('/') + { + context.snippet(mk_sp( + open_brace_pos, + open_brace_pos + BytePos::from_usize(hi), + )) + } else { + context.snippet(mk_sp(open_brace_pos, arms[0].span.lo())) + }; + if blank_line_snip.find("\n") != blank_line_snip.rfind("\n") { + "\n" + } else { + "" + } + } else { + "" + }; + if arms.is_empty() { let snippet = context.snippet(mk_sp(open_brace_pos, span.hi() - BytePos(1))); if snippet.trim().is_empty() { @@ -132,10 +156,11 @@ pub(crate) fn rewrite_match( } else { let span_after_cond = mk_sp(cond.span.hi(), span.hi()); Some(format!( - "match {}{}{{\n{}{}{}\n{}}}", + "match {}{}{{\n{}{}{}{}\n{}}}", cond_str, block_sep, inner_attrs_str, + blank_line_after_inner_attrs, nested_indent_str, rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?, shape.indent.to_string(context.config), diff --git a/tests/source/issue-6005/multiple_blank_lines.rs b/tests/source/issue-6005/multiple_blank_lines.rs new file mode 100644 index 00000000000..0c571f6569d --- /dev/null +++ b/tests/source/issue-6005/multiple_blank_lines.rs @@ -0,0 +1,21 @@ +// rustfmt-version: Two +pub fn main() { + match a { + #![deny(non_exhaustive_omitted_patterns)] + + + + // test + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } + + match b { + #![deny(non_exhaustive_omitted_patterns)] + + + + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } +} diff --git a/tests/source/issue-6005/no_blank_line.rs b/tests/source/issue-6005/no_blank_line.rs new file mode 100644 index 00000000000..e93bb2dc739 --- /dev/null +++ b/tests/source/issue-6005/no_blank_line.rs @@ -0,0 +1,15 @@ +// rustfmt-version: Two +pub fn main() { + match a { + #![deny(non_exhaustive_omitted_patterns)] + // test + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } + + match b { + #![deny(non_exhaustive_omitted_patterns)] + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } +} diff --git a/tests/source/issue-6005/single_blank_line.rs b/tests/source/issue-6005/single_blank_line.rs new file mode 100644 index 00000000000..20b825d3b11 --- /dev/null +++ b/tests/source/issue-6005/single_blank_line.rs @@ -0,0 +1,17 @@ +// rustfmt-version: Two +pub fn main() { + match a { + #![deny(non_exhaustive_omitted_patterns)] + + // test + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } + + match b { + #![deny(non_exhaustive_omitted_patterns)] + + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } +} diff --git a/tests/target/issue-6005/multiple_blank_lines.rs b/tests/target/issue-6005/multiple_blank_lines.rs new file mode 100644 index 00000000000..c83d82af34c --- /dev/null +++ b/tests/target/issue-6005/multiple_blank_lines.rs @@ -0,0 +1,17 @@ +// rustfmt-version: Two +pub fn main() { + match a { + #![deny(non_exhaustive_omitted_patterns)] + + // test + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } + + match b { + #![deny(non_exhaustive_omitted_patterns)] + + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } +} diff --git a/tests/target/issue-6005/no_blank_line.rs b/tests/target/issue-6005/no_blank_line.rs new file mode 100644 index 00000000000..9ab8e3eeb79 --- /dev/null +++ b/tests/target/issue-6005/no_blank_line.rs @@ -0,0 +1,15 @@ +// rustfmt-version: Two +pub fn main() { + match a { + #![deny(non_exhaustive_omitted_patterns)] + // test + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } + + match b { + #![deny(non_exhaustive_omitted_patterns)] + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } +} diff --git a/tests/target/issue-6005/single_blank_line.rs b/tests/target/issue-6005/single_blank_line.rs new file mode 100644 index 00000000000..c83d82af34c --- /dev/null +++ b/tests/target/issue-6005/single_blank_line.rs @@ -0,0 +1,17 @@ +// rustfmt-version: Two +pub fn main() { + match a { + #![deny(non_exhaustive_omitted_patterns)] + + // test + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } + + match b { + #![deny(non_exhaustive_omitted_patterns)] + + Expr::Array(ExprArray { attrs, .. }) => None, + _ => None, + } +}