rust-lang / rust-lang/rust-clippy
False positive with suspicious use of operator when using const generics
Open
Nobody has claimed this yet.
C-bug
I-false-positive
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Lint name:
I tried this code, and related other ops implementations:
use std::ops::Add;
#[derive(Debug, Copy, Clone)]
struct Matrix<T, const M: usize, const N: usize> {
data: [[T; M]; N],
}
impl<T, const M: usize, const N: usize> Add<Matrix<T, M, N>> for Matrix<T, M, N>
where
T: Copy + Add<Output = T>,
{
type Output = Matrix<T, M, N>;
fn add(mut self, other: Matrix<T, M, N>) -> Self::Output {
for idx in 0..(M * N) {
self[idx] = self[idx].add(other[idx]);
}
self
}
}
I expected to see this happen: compile without errors
Instead, this happened:
Checking playground v0.0.1 (/playground)
error: suspicious use of binary operator in `Add` impl
--> src/main.rs:15:26
|
15 | for idx in 0..(M * N) {
| ^
|
= note: `#[deny(clippy::suspicious_arithmetic_impl)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl
error: aborting due to previous error
error: could not compile `playground`
To learn more, run the command again with --verbose.
Meta
clippy 0.0.212 (44e3daf5 2020-12-31)-
rustc 1.51.0-nightly (44e3daf5e 2020-12-31) binary: rustc commit-hash: 44e3daf5eee8263dfc3a2509e78ddd1f6f783a0e commit-date: 2020-12-31 host: x86_64-apple-darwin release: 1.51.0-nightly
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 with the Rust Playground reproducer in the issue and run Clippy with suspicious_op_assign_impl and suspicious_arithmetic_impl enabled. Check why the M * N expression in the const-generic Matrix Add implementation is reported, then verify the example compiles without a false-positive diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100