ruff/crates/ruff_python_formatter/src/pattern
Brent Westbrook 63b1c1ea8b
Avoid extra parentheses for long `match` patterns with `as` captures (#21176)
Summary
--

This PR fixes #17796 by taking the approach mentioned in
https://github.com/astral-sh/ruff/issues/17796#issuecomment-2847943862
of simply recursing into the `MatchAs` patterns when checking if we need
parentheses. This allows us to reuse the parentheses in the inner
pattern before also breaking the `MatchAs` pattern itself:

```diff
 match class_pattern:
     case Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture:
         pass
-    case (
-        Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture
-    ):
+    case Class(
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ) as capture:
         pass
-    case (
-        Class(
-            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-        ) as capture
-    ):
+    case Class(
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ) as capture:
         pass
     case (
         Class(
@@ -685,13 +683,11 @@
 match sequence_pattern_brackets:
     case [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture:
         pass
-    case (
-        [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture
-    ):
+    case [
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ] as capture:
         pass
-    case (
-        [
-            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-        ] as capture
-    ):
+    case [
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ] as capture:
         pass
```

I haven't really resolved the question of whether or not it's okay
always to recurse, but I'm hoping the ecosystem check on this PR might
shed some light on that.

Test Plan
--

New tests based on the issue and then reviewing the ecosystem check here
2025-11-03 17:06:52 -05:00
..
mod.rs Avoid extra parentheses for long `match` patterns with `as` captures (#21176) 2025-11-03 17:06:52 -05:00
pattern_arguments.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
pattern_keyword.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
pattern_match_as.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
pattern_match_class.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
pattern_match_mapping.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
pattern_match_or.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
pattern_match_sequence.rs Update pre-commit dependencies (#19162) 2025-07-07 04:07:44 +00:00
pattern_match_singleton.rs Ruff 2025 style guide (#13906) 2025-01-09 10:20:06 +01:00
pattern_match_star.rs Upgrade to Rust 1.82 (#13816) 2024-10-19 16:05:50 +02:00
pattern_match_value.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00