Skip to content

False positive with suspicious use of operator when using const generics #6600

@rossmacarthur

Description

@rossmacarthur

Lint name:

I tried this code, and related other ops implementations:

Playground

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions