Skip to content

Commit 79f4dec

Browse files
committed
Do not add or remove block from closure body inside macro
1 parent 8f738a2 commit 79f4dec

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

src/expr.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,10 @@ fn rewrite_last_closure(
22992299
if prefix.contains('\n') {
23002300
return None;
23012301
}
2302+
// If we are inside macro, we do not want to add or remove block from closure body.
2303+
if context.inside_macro {
2304+
return expr.rewrite(context, shape);
2305+
}
23022306

23032307
let body_shape = try_opt!(shape.offset_left(extra_offset));
23042308

@@ -2365,15 +2369,7 @@ where
23652369
ast::ExprKind::Closure(..) => {
23662370
// If the argument consists of multiple closures, we do not overflow
23672371
// the last closure.
2368-
if args.len() > 1 &&
2369-
args.iter()
2370-
.rev()
2371-
.skip(1)
2372-
.filter_map(|arg| arg.to_expr())
2373-
.any(|expr| match expr.node {
2374-
ast::ExprKind::Closure(..) => true,
2375-
_ => false,
2376-
}) {
2372+
if args_has_many_closure(args) {
23772373
None
23782374
} else {
23792375
rewrite_last_closure(context, expr, shape)
@@ -2394,6 +2390,22 @@ where
23942390
}
23952391
}
23962392

2393+
fn args_has_many_closure<T>(args: &[&T]) -> bool
2394+
where
2395+
T: ToExpr,
2396+
{
2397+
args.iter()
2398+
.filter(|arg| {
2399+
arg.to_expr()
2400+
.map(|e| match e.node {
2401+
ast::ExprKind::Closure(..) => true,
2402+
_ => false,
2403+
})
2404+
.unwrap_or(false)
2405+
})
2406+
.count() > 1
2407+
}
2408+
23972409
fn can_be_overflowed<'a, T>(context: &RewriteContext, args: &[&T]) -> bool
23982410
where
23992411
T: Rewrite + Spanned + ToExpr + 'a,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-fn_call_style: Block
2+
3+
// #1547
4+
fuzz_target!(|data: &[u8]| if let Some(first) = data.first() {
5+
let index = *first as usize;
6+
if index >= ENCODINGS.len() {
7+
return;
8+
}
9+
let encoding = ENCODINGS[index];
10+
dispatch_test(encoding, &data[1..]);
11+
});

tests/target/closure-block-inside-macro.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// rustfmt-fn_call_style: Block
22

33
// #1547
4-
fuzz_target!(|data: &[u8]| {
5-
if let Some(first) = data.first() {
6-
let index = *first as usize;
7-
if index >= ENCODINGS.len() {
8-
return;
9-
}
4+
fuzz_target!(|data: &[u8]| if let Some(first) = data.first() {
5+
let index = *first as usize;
6+
if index >= ENCODINGS.len() {
7+
return;
108
}
119
let encoding = ENCODINGS[index];
1210
dispatch_test(encoding, &data[1..]);

0 commit comments

Comments
 (0)