-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
integer_arithmetic rule is too simplistic and is triggered even when arithmetic operations are applied to constants/literals that cannot cause overflow or panic #6209
Comments
According to the Reference
So this will overflow fn main() {
let _ = std::i32::MIN / -1;
} We could special case though if the RHS of a binary operation |
yeah, I don't think it's a good idea to ask people to use checked_div and checked_rem when we know that there's no chance of bug there : o |
This would also apply to other operations when both sides are literals/constants, right? For example, there are also no issues when writing Ideally, the lint would check if one or both operands are literals and, if yes, check if they cause overflow/panic. |
Indeed, and we could improve that too, but I think it will be a bit more involved than the cases with division and remainder. I'm going to add the |
Update the existing arithmetic lint re: #6209 Updates the lint to not the error message if RHS of binary operation `/` of `%` is a literal/constant that is not `0` or `-1`, as suggested [here](#6209 (comment)) changelog: Expand [`integer_arithmetic`] to work with RHS literals and constants
After looking into this some more, I noticed that For multiplication, addition, subtraction, both operands need to be known to rule out overflows; except if one is known to be 1 (multiplication) or 0 (addition, subtraction), which anyways trigger the identity_op lint. For the potential overflow of division and remainder in case the left-hand argument is the smallest integer of a signed integer type and the right-hand argument is -1, either one of the operands can potentially rule out the overflow. (Side note/question: Why is there an overflow when calculating the remainder with -1 anyway? The result is always 0, right?) |
@ebroto If you agree with my assessment and suggestions, I'm happy to raise a PR fixing this. |
If that helps, I also got this kind of false positive:
|
This issue can be closed now |
Hello, the following code triggers the integer-arithmetic rule:
I'm not sure how useful it is, as there is no danger here IIUC. The only edge-case is division by 0 right?
The text was updated successfully, but these errors were encountered: