Skip to content

Commit bb1697e

Browse files
committed
Do not add or remove block from closure body inside macro
1 parent a7a7feb commit bb1697e

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
@@ -2291,6 +2291,10 @@ fn rewrite_last_closure(
22912291
if prefix.contains('\n') {
22922292
return None;
22932293
}
2294+
// If we are inside macro, we do not want to add or remove block from closure body.
2295+
if context.inside_macro {
2296+
return expr.rewrite(context, shape);
2297+
}
22942298

22952299
let body_shape = try_opt!(shape.offset_left(extra_offset));
22962300

@@ -2357,15 +2361,7 @@ where
23572361
ast::ExprKind::Closure(..) => {
23582362
// If the argument consists of multiple closures, we do not overflow
23592363
// the last closure.
2360-
if args.len() > 1 &&
2361-
args.iter()
2362-
.rev()
2363-
.skip(1)
2364-
.filter_map(|arg| arg.to_expr())
2365-
.any(|expr| match expr.node {
2366-
ast::ExprKind::Closure(..) => true,
2367-
_ => false,
2368-
}) {
2364+
if args_has_many_closure(args) {
23692365
None
23702366
} else {
23712367
rewrite_last_closure(context, expr, shape)
@@ -2386,6 +2382,22 @@ where
23862382
}
23872383
}
23882384

2385+
fn args_has_many_closure<T>(args: &[&T]) -> bool
2386+
where
2387+
T: ToExpr,
2388+
{
2389+
args.iter()
2390+
.filter(|arg| {
2391+
arg.to_expr()
2392+
.map(|e| match e.node {
2393+
ast::ExprKind::Closure(..) => true,
2394+
_ => false,
2395+
})
2396+
.unwrap_or(false)
2397+
})
2398+
.count() > 1
2399+
}
2400+
23892401
fn can_be_overflowed<'a, T>(context: &RewriteContext, args: &[&T]) -> bool
23902402
where
23912403
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)