mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Avoid unnecessary comments check in maybe_parenthesize_expression
(#7686)
## Summary No-op refactor, but we can evaluate early if the first part of `preserve_parentheses || has_comments` is `true`, and thus avoid looking up the node comments. ## Test Plan `cargo test`
This commit is contained in:
parent
f53c410ff8
commit
1c02fcd7ce
1 changed files with 12 additions and 9 deletions
|
@ -187,7 +187,6 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
||||||
parenthesize,
|
parenthesize,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
let comments = f.context().comments();
|
|
||||||
let preserve_parentheses = parenthesize.is_optional()
|
let preserve_parentheses = parenthesize.is_optional()
|
||||||
&& is_expression_parenthesized(
|
&& is_expression_parenthesized(
|
||||||
(*expression).into(),
|
(*expression).into(),
|
||||||
|
@ -195,14 +194,20 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
||||||
f.context().source(),
|
f.context().source(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let node_comments = comments.leading_dangling_trailing(*expression);
|
// If we want to preserve parentheses, short-circuit.
|
||||||
|
if preserve_parentheses {
|
||||||
|
return expression.format().with_options(Parentheses::Always).fmt(f);
|
||||||
|
}
|
||||||
|
|
||||||
let has_comments = node_comments.has_leading() || node_comments.has_trailing_own_line();
|
let node_comments = f
|
||||||
|
.context()
|
||||||
|
.comments()
|
||||||
|
.leading_dangling_trailing(*expression);
|
||||||
|
|
||||||
// If the expression has comments, we always want to preserve the parentheses. This also
|
// If the expression has comments, we always want to preserve the parentheses. This also
|
||||||
// ensures that we correctly handle parenthesized comments, and don't need to worry about
|
// ensures that we correctly handle parenthesized comments, and don't need to worry about
|
||||||
// them in the implementation below.
|
// them in the implementation below.
|
||||||
if preserve_parentheses || has_comments {
|
if node_comments.has_leading() || node_comments.has_trailing_own_line() {
|
||||||
return expression.format().with_options(Parentheses::Always).fmt(f);
|
return expression.format().with_options(Parentheses::Always).fmt(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,9 +257,7 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
||||||
// The group id is necessary because the nested expressions may reference it.
|
// The group id is necessary because the nested expressions may reference it.
|
||||||
let group_id = f.group_id("optional_parentheses");
|
let group_id = f.group_id("optional_parentheses");
|
||||||
let f = &mut WithNodeLevel::new(NodeLevel::Expression(Some(group_id)), f);
|
let f = &mut WithNodeLevel::new(NodeLevel::Expression(Some(group_id)), f);
|
||||||
ruff_formatter::prelude::best_fit_parenthesize(
|
best_fit_parenthesize(&expression.format().with_options(Parentheses::Never))
|
||||||
&expression.format().with_options(Parentheses::Never),
|
|
||||||
)
|
|
||||||
.with_group_id(Some(group_id))
|
.with_group_id(Some(group_id))
|
||||||
.fmt(f)
|
.fmt(f)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue