Skip to content

Preserve blank line between match inner attrs and first arm #6119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I'm seeing the !arms.is_empty() condition I think we should also add test cases showing how we reformat a match with inner attributes but no match arms.

{
let blank_line_snip = if let Some(hi) = context
.snippet(mk_sp(open_brace_pos, arms[0].span.lo()))
.find('/')
Comment on lines +128 to +130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably the .find('/') call is searching for comments? I think it's more appropriate to use the Span from the last inner attribute instead of the open_brace_pos. I think using the open_brace_pos could lead to subtle formatting errors for these cases:

pub fn main() {
    match a {
        // comment before inner attributes
        #![attr1]
        #![attr2]
        _ => None,
    }

    match a {
        #![attr1]
        // comment between inner attributes
        #![attr2]
        _ => None,
    }
}

{
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 {
""
};

Comment on lines +124 to +147
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nicer to extract the blank_line_after_inner_attrs calculation into a standalone function so that rewrite_match isn't responsible for this logic.

if arms.is_empty() {
let snippet = context.snippet(mk_sp(open_brace_pos, span.hi() - BytePos(1)));
if snippet.trim().is_empty() {
Expand All @@ -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),
Expand Down
21 changes: 21 additions & 0 deletions tests/source/issue-6005/multiple_blank_lines.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
15 changes: 15 additions & 0 deletions tests/source/issue-6005/no_blank_line.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
17 changes: 17 additions & 0 deletions tests/source/issue-6005/single_blank_line.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
17 changes: 17 additions & 0 deletions tests/target/issue-6005/multiple_blank_lines.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
15 changes: 15 additions & 0 deletions tests/target/issue-6005/no_blank_line.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
17 changes: 17 additions & 0 deletions tests/target/issue-6005/single_blank_line.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}