mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +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
|
@ -151,3 +151,13 @@ if (
|
||||||
not # comment
|
not # comment
|
||||||
a):
|
a):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
# Regression test for: https://github.com/astral-sh/ruff/issues/7423
|
||||||
|
if True:
|
||||||
|
if True:
|
||||||
|
if True:
|
||||||
|
if not yn_question(
|
||||||
|
Fore.RED
|
||||||
|
+ "WARNING: Removing listed files. Do you really want to continue. yes/n)? "
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
|
|
@ -82,8 +82,10 @@ impl NeedsParentheses for ExprUnaryOp {
|
||||||
context.source(),
|
context.source(),
|
||||||
) {
|
) {
|
||||||
OptionalParentheses::Never
|
OptionalParentheses::Never
|
||||||
|
} else if context.comments().has(self.operand.as_ref()) {
|
||||||
|
OptionalParentheses::Always
|
||||||
} else {
|
} else {
|
||||||
OptionalParentheses::Multiline
|
self.operand.needs_parentheses(self.into(), context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,6 +157,16 @@ if (
|
||||||
not # comment
|
not # comment
|
||||||
a):
|
a):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
# Regression test for: https://github.com/astral-sh/ruff/issues/7423
|
||||||
|
if True:
|
||||||
|
if True:
|
||||||
|
if True:
|
||||||
|
if not yn_question(
|
||||||
|
Fore.RED
|
||||||
|
+ "WARNING: Removing listed files. Do you really want to continue. yes/n)? "
|
||||||
|
):
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
|
@ -323,10 +333,18 @@ if (
|
||||||
):
|
):
|
||||||
...
|
...
|
||||||
|
|
||||||
if (
|
if not a: # comment
|
||||||
not a # comment
|
|
||||||
):
|
|
||||||
...
|
...
|
||||||
|
|
||||||
|
# Regression test for: https://github.com/astral-sh/ruff/issues/7423
|
||||||
|
if True:
|
||||||
|
if True:
|
||||||
|
if True:
|
||||||
|
if not yn_question(
|
||||||
|
Fore.RED
|
||||||
|
+ "WARNING: Removing listed files. Do you really want to continue. yes/n)? "
|
||||||
|
):
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue