Don't flatten a block containing a single macro call

We no longer flatten a block that looks like this:

```rust
match val {
    pat => { macro_call!() }
}
```

Currently, rust ignores trailing semicolons in macro expansion in
expression position (see https://github.com/rust-lang/rust/issues/33953)

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. `pat => macro_call!()`)
This commit is contained in:
Aaron Hill 2020-10-27 18:52:02 -04:00 committed by Caleb Cartwright
parent ea97ec5a4e
commit 46ab14437e
5 changed files with 31 additions and 7 deletions

View File

@ -284,6 +284,15 @@ fn rewrite_match_arm(
)
}
fn stmt_is_expr_mac(stmt: &ast::Stmt) -> bool {
if let ast::StmtKind::Expr(expr) = &stmt.kind {
if let ast::ExprKind::MacCall(_) = &expr.kind {
return true;
}
}
false
}
fn block_can_be_flattened<'a>(
context: &RewriteContext<'_>,
expr: &'a ast::Expr,
@ -292,7 +301,10 @@ fn block_can_be_flattened<'a>(
ast::ExprKind::Block(ref block, _)
if !is_unsafe_block(block)
&& !context.inside_macro()
&& is_simple_block(context, block, Some(&expr.attrs)) =>
&& is_simple_block(context, block, Some(&expr.attrs))
// Don't flatten a block containing a macro invocation,
// since it may expand to a statement
&& !stmt_is_expr_mac(&block.stmts[0]) =>
{
Some(&*block)
}

View File

@ -5,6 +5,8 @@ fn main() {
match lorem {
true =>
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
false => println!("{}", sit),
false => {
println!("{}", sit)
}
}
}

View File

@ -6,6 +6,8 @@ fn main() {
true => {
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
}
false => println!("{}", sit),
false => {
println!("{}", sit)
}
}
}

View File

@ -11,7 +11,9 @@ fn a_func() {
let err: &CStr = match err.kind {
ParseErrorKind::Custom(StyleParseErrorKind::MediaQueryExpectedFeatureName(
..,
)) => cstr!("PEMQExpectedFeatureName"),
)) => {
cstr!("PEMQExpectedFeatureName")
}
};
}
};

View File

@ -166,9 +166,15 @@ fn issue355() {
a => println!("a", b),
b => vec![1, 2],
c => vec![3; 4],
d => println!("a", b),
e => vec![1, 2],
f => vec![3; 4],
d => {
println!("a", b)
}
e => {
vec![1, 2]
}
f => {
vec![3; 4]
}
h => println!("a", b), // h comment
i => vec![1, 2], // i comment
j => vec![3; 4], // j comment