Simplify IfRequired needs parentheses condition (#6678)

This commit is contained in:
Micha Reiser 2023-08-21 09:11:31 +02:00 committed by GitHub
parent 2a8d24dd4b
commit 8b347cdaa9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 13 deletions

View file

@ -192,13 +192,11 @@ impl Format<PyFormatContext<'_>> 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,

View file

@ -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(