diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 718c03d0c4..2b55147044 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -192,13 +192,11 @@ impl Format> for MaybeParenthesizeExpression<'_> { let needs_parentheses = expression.needs_parentheses(*parent, f.context()); let needs_parentheses = match parenthesize { - Parenthesize::IfRequired => { - if !needs_parentheses.is_always() && f.context().node_level().is_parenthesized() { - OptionalParentheses::Never - } else { - needs_parentheses - } - } + Parenthesize::IfRequired => match needs_parentheses { + OptionalParentheses::Always => OptionalParentheses::Always, + _ if f.context().node_level().is_parenthesized() => OptionalParentheses::Never, + needs_parentheses => needs_parentheses, + }, Parenthesize::Optional | Parenthesize::IfBreaks | Parenthesize::IfBreaksOrIfRequired => needs_parentheses, diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index 16979c4c80..1aaec853d1 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -23,12 +23,6 @@ pub(crate) enum OptionalParentheses { Never, } -impl OptionalParentheses { - pub(crate) const fn is_always(self) -> bool { - matches!(self, OptionalParentheses::Always) - } -} - pub(crate) trait NeedsParentheses { /// Determines if this object needs optional parentheses or if it is safe to omit the parentheses. fn needs_parentheses(