rust-lang / rust-lang/rust-clippy
`suspicious_arithmetic_impl` flags on size addition in multiplication
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
I maintain a library that supports symbolic multiplication on bitvectors. The API exposes how many bits to keep in the output, i.e. multiply(lhs, rhs, n). For the std::ops::Mul impl I just use the maximum size needed: multiply(lhs, rhs, lhs.len() + rhs.len()). Because the lint sees the + as the sole binary operator in Mul, it flags it as suspicious.
As has been noted in other issues (#2268, #3215), this lint is hard to get right, but would be great if it didn't flag here.
Lint Name
suspicious_arithmetic_impl
Reproducer
I tried this code:
pub struct BitVec {
bytes: Vec<u8>,
}
impl BitVec {
pub fn multiply(self, _rhs: Self, _n: usize) -> Self {
unimplemented!()
}
}
impl std::ops::Mul for BitVec {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
// False positive due to +
let n = self.bytes.len() + rhs.bytes.len();
self.multiply(rhs, n)
}
}
I saw this happen:
warning: suspicious use of `+` in `Mul` impl
--> test.rs:16:34
|
16 | let n = self.bytes.len() + rhs.bytes.len();
| ^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#suspicious_arithmetic_impl
= note: `#[warn(clippy::suspicious_arithmetic_impl)]` on by default
I expected to see this happen: Lint not to fire.
Version
rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: x86_64-unknown-linux-gnu
release: 1.91.1
LLVM version: 21.1.2
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the suspicious_arithmetic_impl warning with the BitVec example, then locate the lint's implementation and existing tests using the lint name. Done means the shown size addition in a Mul implementation no longer triggers the warning without weakening detection of genuinely suspicious arithmetic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100