From 2220563bece0c10c42d5d6edddccb0fe0419bd3e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 4 Jan 2024 19:38:12 -0500 Subject: [PATCH] Parenthesize breaking named expressions in match guards --- .../test/fixtures/ruff/statement/match.py | 5 +++++ .../src/other/match_case.rs | 19 +++++++++++++++++-- .../snapshots/format@statement__match.py.snap | 14 +++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py index f24f9416cc0ef..af1e25c96f785 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py @@ -109,6 +109,11 @@ def foo(): ): # 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": + pass + match pattern_comments: case ( diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index 785ab73f502b1..55a8dfc6a636c 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -4,7 +4,10 @@ use ruff_python_ast::MatchCase; use crate::builders::parenthesize_if_expands; use crate::comments::SourceComment; -use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses}; +use crate::expression::maybe_parenthesize_expression; +use crate::expression::parentheses::{ + NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, +}; use crate::prelude::*; use crate::statement::clause::{clause_body, clause_header, ClauseHeader}; @@ -58,7 +61,19 @@ impl FormatNodeRule for FormatMatchCase { } if let Some(guard) = guard { - write!(f, [space(), token("if"), space(), guard.format()])?; + write!( + f, + [ + space(), + token("if"), + space(), + maybe_parenthesize_expression( + guard, + item, + Parenthesize::Optional + ) + ] + )?; } 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 dad7a3c526f8b..76b9438b8d4aa 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 @@ -115,6 +115,11 @@ 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": + pass + match pattern_comments: case ( @@ -677,7 +682,9 @@ 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 ( @@ -696,6 +703,11 @@ 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" + ): + pass + match pattern_comments: case (