diff --git a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs index 3fe831c3a0b9a..6b52a7031d2d6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs @@ -3,7 +3,9 @@ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::ExprNamedExpr; use crate::comments::{dangling_comments, SourceComment}; -use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; +use crate::expression::parentheses::{ + in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses, +}; use crate::prelude::*; #[derive(Default)] @@ -24,7 +26,10 @@ impl FormatNodeRule for FormatExprNamedExpr { write!( f, [ - group(&format_args!(target.format(), soft_line_break_or_space())), + group(&format_args!( + target.format(), + in_parentheses_only_soft_line_break_or_space() + )), token(":=") ] )?; diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index 55a8dfc6a636c..785ab73f502b1 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -4,10 +4,7 @@ use ruff_python_ast::MatchCase; use crate::builders::parenthesize_if_expands; use crate::comments::SourceComment; -use crate::expression::maybe_parenthesize_expression; -use crate::expression::parentheses::{ - NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, -}; +use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses}; use crate::prelude::*; use crate::statement::clause::{clause_body, clause_header, ClauseHeader}; @@ -61,19 +58,7 @@ impl FormatNodeRule for FormatMatchCase { } if let Some(guard) = guard { - write!( - f, - [ - space(), - token("if"), - space(), - maybe_parenthesize_expression( - guard, - item, - Parenthesize::Optional - ) - ] - )?; + write!(f, [space(), token("if"), space(), guard.format()])?; } Ok(()) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap index 76b9438b8d4aa..cd64f26de63e0 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap @@ -682,9 +682,7 @@ match newlines: match long_lines: - case "this is a long line for if condition" if ( - aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2 - ): # comment + case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment pass case "this is a long line for if condition with parentheses" if ( @@ -703,9 +701,9 @@ match long_lines: ): # another comment pass - case {"long_long_long_key": str(long_long_long_key)} if ( - value := "long long long long long long long long long long long value" - ): + case { + "long_long_long_key": str(long_long_long_key) + } if value := "long long long long long long long long long long long value": pass