From 0a41abed52898a03687b848a7cb7698269276f9f Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Tue, 2 Dec 2025 10:30:45 -0500 Subject: [PATCH] avoid lambda special-casing in maybe_parenthesize_expression --- .../src/expression/mod.rs | 25 +---------- .../src/statement/stmt_assign.rs | 45 +++++++++++-------- 2 files changed, 28 insertions(+), 42 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index c9ae062b75..a320a1edf5 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -14,7 +14,6 @@ use ruff_text_size::Ranged; use crate::builders::parenthesize_if_expands; use crate::comments::{LeadingDanglingTrailingComments, leading_comments, trailing_comments}; use crate::context::{NodeLevel, WithNodeLevel}; -use crate::expression::expr_lambda::ExprLambdaLayout; use crate::expression::parentheses::{ NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, is_expression_parenthesized, optional_parentheses, parenthesized, @@ -343,7 +342,6 @@ where expression, parent: parent.into(), parenthesize, - lambda_layout: ExprLambdaLayout::default(), } } @@ -351,14 +349,6 @@ pub(crate) struct MaybeParenthesizeExpression<'a> { expression: &'a Expr, parent: AnyNodeRef<'a>, parenthesize: Parenthesize, - lambda_layout: ExprLambdaLayout, -} - -impl MaybeParenthesizeExpression<'_> { - pub(crate) fn with_lambda_layout(mut self, layout: ExprLambdaLayout) -> Self { - self.lambda_layout = layout; - self - } } impl Format> for MaybeParenthesizeExpression<'_> { @@ -367,7 +357,6 @@ impl Format> for MaybeParenthesizeExpression<'_> { expression, parent, parenthesize, - lambda_layout, } = self; let preserve_parentheses = parenthesize.is_optional() @@ -421,19 +410,9 @@ impl Format> for MaybeParenthesizeExpression<'_> { | Parenthesize::IfBreaksParenthesized | Parenthesize::IfBreaksParenthesizedNested => { if can_omit_optional_parentheses(expression, f.context()) { - if let Expr::Lambda(lambda) = expression { - optional_parentheses(&lambda.format().with_options(*lambda_layout)) - .fmt(f) - } else { - optional_parentheses(&unparenthesized).fmt(f) - } + optional_parentheses(&unparenthesized).fmt(f) } else { - if let Expr::Lambda(lambda) = expression { - parenthesize_if_expands(&lambda.format().with_options(*lambda_layout)) - .fmt(f) - } else { - parenthesize_if_expands(&unparenthesized).fmt(f) - } + parenthesize_if_expands(&unparenthesized).fmt(f) } } }, diff --git a/crates/ruff_python_formatter/src/statement/stmt_assign.rs b/crates/ruff_python_formatter/src/statement/stmt_assign.rs index ff40885440..0fb481b82f 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_assign.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_assign.rs @@ -304,13 +304,17 @@ impl Format> for FormatStatementsLastExpression<'_> { && format_implicit_flat.is_none() && format_interpolated_string.is_none() { - return maybe_parenthesize_expression( - value, - *statement, - Parenthesize::IfBreaks, - ) - .with_lambda_layout(ExprLambdaLayout::Assignment) - .fmt(f); + return if let Expr::Lambda(lambda) = value { + let lambda = lambda.format().with_options(ExprLambdaLayout::Assignment); + if can_omit_optional_parentheses(value, f.context()) { + optional_parentheses(&lambda).fmt(f) + } else { + parenthesize_if_expands(&lambda).fmt(f) + } + } else { + maybe_parenthesize_expression(value, *statement, Parenthesize::IfBreaks) + .fmt(f) + }; } let comments = f.context().comments().clone(); @@ -581,20 +585,23 @@ impl Format> for FormatStatementsLastExpression<'_> { && format_implicit_flat.is_none() && format_interpolated_string.is_none() { + let formatted_value = format_with(|f| { + if let Expr::Lambda(lambda) = value { + let lambda = lambda.format().with_options(ExprLambdaLayout::Assignment); + if can_omit_optional_parentheses(value, f.context()) { + optional_parentheses(&lambda).fmt(f) + } else { + parenthesize_if_expands(&lambda).fmt(f) + } + } else { + maybe_parenthesize_expression(value, *statement, Parenthesize::IfBreaks) + .fmt(f) + } + }); + return write!( f, - [ - before_operator, - space(), - operator, - space(), - maybe_parenthesize_expression( - value, - *statement, - Parenthesize::IfBreaks - ) - .with_lambda_layout(ExprLambdaLayout::Assignment) - ] + [before_operator, space(), operator, space(), formatted_value] ); }