Make `suspicious_arithmetic_impl` understand multiplicative inverses
raphlinus opened this issue · comments
Raph Levien commented
Summary
I pretty often write Div
impls that multiply by a reciprocal, as that's more efficient, and for graphics operations, giving up precise rounding behavior is usually not a problem. When working on the color crate, it was one of the few exceptions I had to ask for.
I suggest we recognize the * rhs.recip()
idiom and accept it as a valid alternative to division.
Lint Name
suspicious_arithmetic_impl
Reproducer
I tried this code:
impl<T: Colorspace> core::ops::Div<f32> for PremulColor<T> {
type Output = Self;
fn div(self, rhs: f32) -> Self {
self * rhs.recip()
}
}
I saw this happen:
warning: suspicious use of `*` in `Div` impl
--> color/src/color.rs:451:14
|
451 | self * rhs.recip()
| ^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl
= note: `#[warn(clippy::suspicious_arithmetic_impl)]` on by default```
I expected to see this happen:
No lint.
### Version
rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: aarch64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1
### Additional Labels
_No response_