From cafcd7b09c298e1ef2cfc5a2c440e16596819239 Mon Sep 17 00:00:00 2001 From: Jonathan Amponsah <82057176+mgalore@users.noreply.github.com> Date: Wed, 5 Aug 2026 14:06:31 +0000 Subject: [PATCH] fix: preserve closure receivers for postfix chains --- src/chains.rs | 20 +++++++++++--------- tests/source/issue-7011.rs | 7 +++++++ tests/target/issue-7011.rs | 7 +++++++ 3 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 tests/source/issue-7011.rs create mode 100644 tests/target/issue-7011.rs diff --git a/src/chains.rs b/src/chains.rs index 90adb67ad43..4bb2f66d484 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -167,7 +167,7 @@ enum CommentPosition { /// Information about an expression in a chain. struct SubExpr { expr: ast::Expr, - is_method_call_receiver: bool, + is_postfix_receiver: bool, } /// An expression plus trailing `?`s to be formatted together. @@ -226,7 +226,7 @@ impl ChainItemKind { fn from_ast( context: &RewriteContext<'_>, expr: &ast::Expr, - is_method_call_receiver: bool, + is_postfix_receiver: bool, ) -> (ChainItemKind, Span) { let (kind, span) = match expr.kind { ast::ExprKind::MethodCall(ref call) => { @@ -276,7 +276,7 @@ impl ChainItemKind { return ( ChainItemKind::Parent { expr: expr.clone(), - parens: is_method_call_receiver && should_add_parens(expr, context), + parens: is_postfix_receiver && should_add_parens(expr, context), }, expr.span, ); @@ -331,8 +331,7 @@ impl Rewrite for ChainItem { impl ChainItem { fn new(context: &RewriteContext<'_>, expr: &SubExpr, tries: usize) -> ChainItem { - let (kind, span) = - ChainItemKind::from_ast(context, &expr.expr, expr.is_method_call_receiver); + let (kind, span) = ChainItemKind::from_ast(context, &expr.expr, expr.is_postfix_receiver); ChainItem { kind, tries, span } } @@ -503,7 +502,7 @@ impl Chain { fn make_subexpr_list(expr: &ast::Expr, context: &RewriteContext<'_>) -> Vec { let mut subexpr_list = vec![SubExpr { expr: expr.clone(), - is_method_call_receiver: false, + is_postfix_receiver: false, }]; while let Some(subexpr) = Self::pop_expr_chain(subexpr_list.last().unwrap(), context) { @@ -519,15 +518,18 @@ impl Chain { match expr.expr.kind { ast::ExprKind::MethodCall(ref call) => Some(SubExpr { expr: Self::convert_try(&call.receiver, context), - is_method_call_receiver: true, + is_postfix_receiver: true, }), ast::ExprKind::Field(ref subexpr, _) - | ast::ExprKind::Try(ref subexpr) | ast::ExprKind::Await(ref subexpr, _) | ast::ExprKind::Use(ref subexpr, _) | ast::ExprKind::Yield(ast::YieldKind::Postfix(ref subexpr)) => Some(SubExpr { expr: Self::convert_try(subexpr, context), - is_method_call_receiver: false, + is_postfix_receiver: true, + }), + ast::ExprKind::Try(ref subexpr) => Some(SubExpr { + expr: Self::convert_try(subexpr, context), + is_postfix_receiver: false, }), _ => None, } diff --git a/tests/source/issue-7011.rs b/tests/source/issue-7011.rs new file mode 100644 index 00000000000..e83631d9230 --- /dev/null +++ b/tests/source/issue-7011.rs @@ -0,0 +1,7 @@ +fn main() { + || 1.. .field; + || 1.. ?.field; + || 1.. .await; + || 1.. .use; + || 1.. .yield; +} diff --git a/tests/target/issue-7011.rs b/tests/target/issue-7011.rs new file mode 100644 index 00000000000..b6f147bd94c --- /dev/null +++ b/tests/target/issue-7011.rs @@ -0,0 +1,7 @@ +fn main() { + (|| 1..).field; + || 1..?.field; + (|| 1..).await; + (|| 1..).use; + (|| 1..).yield; +}