2023-06-20 21:34:24 +00:00
|
|
|
#![allow(clippy::legacy_numeric_constants)]
|
2018-07-28 15:34:52 +00:00
|
|
|
#![warn(clippy::suspicious_arithmetic_impl)]
|
2020-08-28 14:10:16 +00:00
|
|
|
use std::ops::{
|
|
|
|
Add, AddAssign, BitAnd, BitOr, BitOrAssign, BitXor, Div, DivAssign, Mul, MulAssign, Rem, Shl, Shr, Sub,
|
|
|
|
};
|
2018-02-13 14:40:17 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Add for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn add(self, other: Self) -> Self {
|
|
|
|
Foo(self.0 - other.0)
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `-` in `Add` impl
|
|
|
|
//~| NOTE: `-D clippy::suspicious-arithmetic-impl` implied by `-D warnings`
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign for Foo {
|
|
|
|
fn add_assign(&mut self, other: Foo) {
|
|
|
|
*self = *self - other;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `-` in `AddAssign` impl
|
|
|
|
//~| NOTE: `-D clippy::suspicious-op-assign-impl` implied by `-D warnings`
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:40:51 +00:00
|
|
|
impl BitOrAssign for Foo {
|
|
|
|
fn bitor_assign(&mut self, other: Foo) {
|
|
|
|
let idx = other.0;
|
|
|
|
self.0 |= 1 << idx; // OK: BinOpKind::Shl part of AssignOp as child node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MulAssign for Foo {
|
|
|
|
fn mul_assign(&mut self, other: Foo) {
|
|
|
|
self.0 /= other.0;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `/` in `MulAssign` impl
|
2020-04-05 18:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DivAssign for Foo {
|
|
|
|
fn div_assign(&mut self, other: Foo) {
|
|
|
|
self.0 /= other.0; // OK: BinOpKind::Div == DivAssign
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:40:17 +00:00
|
|
|
impl Mul for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn mul(self, other: Foo) -> Foo {
|
2018-07-12 07:50:09 +00:00
|
|
|
Foo(self.0 * other.0 % 42) // OK: BinOpKind::Rem part of BiExpr as parent node
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn sub(self, other: Self) -> Self {
|
2018-07-12 07:50:09 +00:00
|
|
|
Foo(self.0 * other.0 - 42) // OK: BinOpKind::Mul part of BiExpr as child node
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Div for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn div(self, other: Self) -> Self {
|
2018-07-12 07:50:09 +00:00
|
|
|
Foo(do_nothing(self.0 + other.0) / 42) // OK: BinOpKind::Add part of BiExpr as child node
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 14:10:16 +00:00
|
|
|
impl Rem for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn rem(self, other: Self) -> Self {
|
|
|
|
Foo(self.0 / other.0)
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `/` in `Rem` impl
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BitAnd for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn bitand(self, other: Self) -> Self {
|
|
|
|
Foo(self.0 | other.0)
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `|` in `BitAnd` impl
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BitOr for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn bitor(self, other: Self) -> Self {
|
|
|
|
Foo(self.0 ^ other.0)
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `^` in `BitOr` impl
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BitXor for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn bitxor(self, other: Self) -> Self {
|
|
|
|
Foo(self.0 & other.0)
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `&` in `BitXor` impl
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Shl for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn shl(self, other: Self) -> Self {
|
|
|
|
Foo(self.0 >> other.0)
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `>>` in `Shl` impl
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Shr for Foo {
|
|
|
|
type Output = Foo;
|
|
|
|
|
|
|
|
fn shr(self, other: Self) -> Self {
|
|
|
|
Foo(self.0 << other.0)
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: suspicious use of `<<` in `Shr` impl
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 12:58:03 +00:00
|
|
|
struct Bar(i32);
|
|
|
|
|
|
|
|
impl Add for Bar {
|
|
|
|
type Output = Bar;
|
|
|
|
|
|
|
|
fn add(self, other: Self) -> Self {
|
2021-02-09 08:15:53 +00:00
|
|
|
Bar(self.0 & !other.0) // OK: Not part of BiExpr as child node
|
2018-03-06 12:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub for Bar {
|
|
|
|
type Output = Bar;
|
|
|
|
|
|
|
|
fn sub(self, other: Self) -> Self {
|
2018-03-17 20:34:13 +00:00
|
|
|
if self.0 <= other.0 {
|
2021-02-09 08:15:53 +00:00
|
|
|
Bar(-(self.0 & other.0)) // OK: Neg part of BiExpr as parent node
|
2018-03-17 20:34:13 +00:00
|
|
|
} else {
|
|
|
|
Bar(0)
|
|
|
|
}
|
2018-03-06 12:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:40:17 +00:00
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
fn do_nothing(x: u32) -> u32 {
|
|
|
|
x
|
|
|
|
}
|
2020-08-11 13:43:21 +00:00
|
|
|
|
|
|
|
struct MultipleBinops(u32);
|
|
|
|
|
|
|
|
impl Add for MultipleBinops {
|
|
|
|
type Output = MultipleBinops;
|
|
|
|
|
|
|
|
// OK: multiple Binops in `add` impl
|
|
|
|
fn add(self, other: Self) -> Self::Output {
|
|
|
|
let mut result = self.0 + other.0;
|
|
|
|
if result >= u32::max_value() {
|
|
|
|
result -= u32::max_value();
|
|
|
|
}
|
|
|
|
MultipleBinops(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul for MultipleBinops {
|
|
|
|
type Output = MultipleBinops;
|
|
|
|
|
|
|
|
// OK: multiple Binops in `mul` impl
|
|
|
|
fn mul(self, other: Self) -> Self::Output {
|
|
|
|
let mut result: u32 = 0;
|
|
|
|
let size = std::cmp::max(self.0, other.0) as usize;
|
|
|
|
let mut v = vec![0; size + 1];
|
|
|
|
for i in 0..size + 1 {
|
|
|
|
result *= i as u32;
|
|
|
|
}
|
|
|
|
MultipleBinops(result)
|
|
|
|
}
|
|
|
|
}
|