mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-20 02:20:42 +00:00
can_omit_optional_parentheses
: Exit early for unparenthesized expressions (#9125)
This commit is contained in:
parent
7256b882b9
commit
c99eae2c08
1 changed files with 9 additions and 9 deletions
|
@ -533,15 +533,15 @@ fn can_omit_optional_parentheses(expr: &Expr, context: &PyFormatContext) -> bool
|
||||||
let mut visitor = CanOmitOptionalParenthesesVisitor::new(context);
|
let mut visitor = CanOmitOptionalParenthesesVisitor::new(context);
|
||||||
visitor.visit_subexpression(expr);
|
visitor.visit_subexpression(expr);
|
||||||
|
|
||||||
if visitor.max_precedence == OperatorPrecedence::None {
|
if !visitor.any_parenthesized_expressions {
|
||||||
|
// Only use the more complex IR when there is any expression that we can possibly split by
|
||||||
|
false
|
||||||
|
} else if visitor.max_precedence == OperatorPrecedence::None {
|
||||||
true
|
true
|
||||||
} else if visitor.max_precedence_count > 1 {
|
} else if visitor.max_precedence_count > 1 {
|
||||||
false
|
false
|
||||||
} else if visitor.max_precedence == OperatorPrecedence::Attribute {
|
} else if visitor.max_precedence == OperatorPrecedence::Attribute {
|
||||||
true
|
true
|
||||||
} else if !visitor.any_parenthesized_expressions {
|
|
||||||
// Only use the more complex IR when there is any expression that we can possibly split by
|
|
||||||
false
|
|
||||||
} else {
|
} else {
|
||||||
fn is_parenthesized(expr: &Expr, context: &PyFormatContext) -> bool {
|
fn is_parenthesized(expr: &Expr, context: &PyFormatContext) -> bool {
|
||||||
// Don't break subscripts except in parenthesized context. It looks weird.
|
// Don't break subscripts except in parenthesized context. It looks weird.
|
||||||
|
@ -716,6 +716,9 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Non terminal nodes that don't have a termination token.
|
||||||
|
Expr::NamedExpr(_) | Expr::GeneratorExp(_) | Expr::Tuple(_) => {}
|
||||||
|
|
||||||
// Expressions with sub expressions but a preceding token
|
// Expressions with sub expressions but a preceding token
|
||||||
// Mark this expression as first expression and not the sub expression.
|
// Mark this expression as first expression and not the sub expression.
|
||||||
// Visit the sub-expressions because the sub expressions may be the end of the entire expression.
|
// Visit the sub-expressions because the sub expressions may be the end of the entire expression.
|
||||||
|
@ -738,11 +741,8 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
|
||||||
self.first.set_if_none(First::Token);
|
self.first.set_if_none(First::Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Terminal nodes or nodes that wrap a sub-expression (where the sub expression can never be the end).
|
// Terminal nodes or nodes that wrap a sub-expression (where the sub expression can never be at the end).
|
||||||
Expr::Tuple(_)
|
Expr::FString(_)
|
||||||
| Expr::NamedExpr(_)
|
|
||||||
| Expr::GeneratorExp(_)
|
|
||||||
| Expr::FString(_)
|
|
||||||
| Expr::StringLiteral(_)
|
| Expr::StringLiteral(_)
|
||||||
| Expr::BytesLiteral(_)
|
| Expr::BytesLiteral(_)
|
||||||
| Expr::NumberLiteral(_)
|
| Expr::NumberLiteral(_)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue