-
Notifications
You must be signed in to change notification settings - Fork 927
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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('/') | ||
Comment on lines
+128
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Presumably the 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nicer to extract the |
||
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), | ||
|
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, | ||
} | ||
} |
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, | ||
} | ||
} |
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, | ||
} | ||
} |
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, | ||
} | ||
} |
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, | ||
} | ||
} |
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, | ||
} | ||
} |
There was a problem hiding this comment.
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.