-
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
spurious suspicious_arithmetic_impl #3215
Comments
Playground example for people who want to test this by just clicking one button 😉 If the arithmetic impl is that complicated, I think this is a case where you should just put an Nonetheless this is a false positive. If someone has an idea how to detect cases like this, feel free to improve this lint! |
Fair enough, I'm happy to just disable the lint. I won't be offended if somebody closes this issue (but on the other hand, if there's a way to detect and avoid the false positive, that'd be even better :)). |
Here's a simpler example showing the false positive. I was implementing a binary counter with a limited number of bits and default-wrapping addition. fn add(self, other: Self) -> Self {
Addr((self.0.wrapping_add(other.0)) & P::addr_mask(), PhantomData)
} Clippy is upset by the presence of |
I also got this, by using
|
That is a great idea! |
This whole lint seems like it's likely to cause more problems than it solves to me. If you're implementing arithmetic operations yourself, chances are that you'll have to do more than simply use the operators that you're trying to implement. It seems like it's trying to fix a very specific bug in a very specific case, and doesn't seem like a good fit for being deny by default. |
Even that will fail (result in a false positive) for simple cases such as a complex number in polar representation where multiplication requires multiplying the magnitudes and adding the polar components (and optionally applying %). Having run into it yesterday, I agree with @Serentty that it seems quite narrow for a general deny (only seems to make sense for "forwarding" implementations). @llogiq suggested looking at the types the addition is being applied to yesterday, i.e. are they related (potentially via a later Another option might be to check if it is the only binary operator (other function calls, |
This just started breaking the nightly builds on my ring buffer implementation because I have a logical index type which implements |
Example https://gist.github.com/apoelstra/d44c7b0244c1a4f87560a4b0e5c17f40 gives
It is possible to implement division using only division (at the very least, alongside #2545 you can use
<=
and/
to extract bits and do boolean logic on them, so you can do anything ;)), but it isn't really reasonable. Even a more efficient implementation of division would need to use subtraction at least.The text was updated successfully, but these errors were encountered: