Skip to content

Commit be67244

Browse files
committed
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 rust-lang/rust#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!()`)
1 parent 8954c21 commit be67244

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

src/formatting/matches.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@ fn rewrite_match_arm(
295295
)
296296
}
297297

298+
fn stmt_is_expr_mac(stmt: &ast::Stmt) -> bool {
299+
if let ast::StmtKind::Expr(expr) = &stmt.kind {
300+
if let ast::ExprKind::MacCall(_) = &expr.kind {
301+
return true;
302+
}
303+
}
304+
false
305+
}
306+
298307
fn block_can_be_flattened<'a>(
299308
context: &RewriteContext<'_>,
300309
expr: &'a ast::Expr,
@@ -303,7 +312,10 @@ fn block_can_be_flattened<'a>(
303312
ast::ExprKind::Block(ref block, _)
304313
if !is_unsafe_block(block)
305314
&& !context.inside_macro()
306-
&& is_simple_block(context, block, Some(&expr.attrs)) =>
315+
&& is_simple_block(context, block, Some(&expr.attrs))
316+
// Don't flatten a block containing a macro invocation,
317+
// since it may expand to a statement
318+
&& !stmt_is_expr_mac(&block.stmts[0]) =>
307319
{
308320
Some(&*block)
309321
}

tests/target/configs/match_arm_blocks/false.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ fn main() {
55
match lorem {
66
true =>
77
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
8-
false => println!("{}", sit),
8+
false => {
9+
println!("{}", sit)
10+
}
911
}
1012
}

tests/target/configs/match_arm_blocks/true.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ fn main() {
66
true => {
77
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
88
}
9-
false => println!("{}", sit),
9+
false => {
10+
println!("{}", sit)
11+
}
1012
}
1113
}

tests/target/issue-2936.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ impl Something for AStruct {
1111
let err: &CStr = match err.kind {
1212
ParseErrorKind::Custom(StyleParseErrorKind::MediaQueryExpectedFeatureName(
1313
..,
14-
)) => cstr!("PEMQExpectedFeatureName"),
14+
)) => {
15+
cstr!("PEMQExpectedFeatureName")
16+
}
1517
};
1618
}
1719
};

tests/target/match.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,15 @@ fn issue355() {
166166
a => println!("a", b),
167167
b => vec![1, 2],
168168
c => vec![3; 4],
169-
d => println!("a", b),
170-
e => vec![1, 2],
171-
f => vec![3; 4],
169+
d => {
170+
println!("a", b)
171+
}
172+
e => {
173+
vec![1, 2]
174+
}
175+
f => {
176+
vec![3; 4]
177+
}
172178
h => println!("a", b), // h comment
173179
i => vec![1, 2], // i comment
174180
j => vec![3; 4], // j comment

0 commit comments

Comments
 (0)