-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug in order of operations (PLC2801) #9572
Comments
Oh thank you, that's a great catch! |
Does the fix also fix dunder calls in binarh expressions like |
This code: a = 1
2 * a.__add__(3) Does get linted to: a = 1
2 * a + 3 So parenthesis are indeed required. However, the parentheses are, of course, not always required. As |
I have no clue how Ruff works under the hood, but if you add the parentheses around everything, they will get removed by another Ruff rule when it's only that expression. Because Ruff lints |
Thanks @RubenVanEldik Yeah, we might even need to add parentheses around |
Fascinating... 🤔 |
@MichaReiser, I'm not entirely sure, but I don't think you can override the operator precedence. Something like Not adding unnecessary parentheses would be nice, but not a necessity, I think. Also, because sometimes it's good to add parentheses, even though they are not strictly necessary, just to add clarity to operator precedence. |
While i agree that this assumption that class A:
def __radd__(self, other):
return other + 1
print(int(1).__add__(A()))
print(int(1) + A()) prints
This is a limitation of the rule in general. The missing parentheses in the OP is of course still a bug and should be fixed. |
It's easy to autofix when the unary is not surrounded by parentheses, since we can just use the operand value as the TextRange start, but autofixing it for something like |
@konstin, good catch!! It seems that this automatic fix should be under the --unsafe-fixes flag then. Most of the time the Ruff fix should work, but as you showed it could create issues in specific scenarios. |
## Summary Closes astral-sh#9572 Don't go easy on this review! ## Test Plan `cargo test`
Hi all!
While updating from Ruff 0.1.11 to 0.1.13 I found a small bug in the newly created rule
PLC2801
.I had the code:
With the auto linter Ruff fixed this to:
The order of operations is changed, to fix this Ruff needs to add some parenthesis (
b = -(a-1)
).I have no experience with Rust, so I am not able to help, but I wanted to let you know of the issue. Thank you for making Rust! It's an absolutely amazing tool!!
The text was updated successfully, but these errors were encountered: