mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-11 22:28:27 +00:00
Avoid extra parentheses in unary expressions (#7428)
## Summary This PR applies a similar fix to unary expressions as in https://github.com/astral-sh/ruff/pull/7424. Specifically, we only need to parenthesize the entire operator if the operand itself doesn't have parentheses, and requires parentheses. Closes https://github.com/astral-sh/ruff/issues/7423. ## Test Plan `cargo test` No change in similarity. Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1632 | | django | 0.99982 | 2760 | 37 | | transformers | 0.99957 | 2587 | 399 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99983 | 3496 | 18 | | warehouse | 0.99923 | 648 | 18 | | zulip | 0.99962 | 1437 | 22 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1632 | | django | 0.99982 | 2760 | 37 | | transformers | 0.99957 | 2587 | 399 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99983 | 3496 | 18 | | warehouse | 0.99923 | 648 | 18 | | zulip | 0.99962 | 1437 | 22 |
This commit is contained in:
parent
0d1fb823d6
commit
1880cceac1
3 changed files with 34 additions and 4 deletions
|
@ -82,8 +82,10 @@ impl NeedsParentheses for ExprUnaryOp {
|
|||
context.source(),
|
||||
) {
|
||||
OptionalParentheses::Never
|
||||
} else if context.comments().has(self.operand.as_ref()) {
|
||||
OptionalParentheses::Always
|
||||
} else {
|
||||
OptionalParentheses::Multiline
|
||||
self.operand.needs_parentheses(self.into(), context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue