summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/arithmetic_side_effects.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/clippy/tests/ui/arithmetic_side_effects.rs')
-rw-r--r--src/tools/clippy/tests/ui/arithmetic_side_effects.rs225
1 files changed, 185 insertions, 40 deletions
diff --git a/src/tools/clippy/tests/ui/arithmetic_side_effects.rs b/src/tools/clippy/tests/ui/arithmetic_side_effects.rs
index b5ed8988a..918cf81c6 100644
--- a/src/tools/clippy/tests/ui/arithmetic_side_effects.rs
+++ b/src/tools/clippy/tests/ui/arithmetic_side_effects.rs
@@ -2,6 +2,7 @@
clippy::assign_op_pattern,
clippy::erasing_op,
clippy::identity_op,
+ clippy::no_effect,
clippy::op_ref,
clippy::unnecessary_owned_empty_strings,
arithmetic_overflow,
@@ -12,31 +13,95 @@
use core::num::{Saturating, Wrapping};
+#[derive(Clone, Copy)]
pub struct Custom;
macro_rules! impl_arith {
- ( $( $_trait:ident, $ty:ty, $method:ident; )* ) => {
+ ( $( $_trait:ident, $lhs:ty, $rhs:ty, $method:ident; )* ) => {
$(
- impl core::ops::$_trait<$ty> for Custom {
- type Output = Self;
- fn $method(self, _: $ty) -> Self::Output { Self }
+ impl core::ops::$_trait<$lhs> for $rhs {
+ type Output = Custom;
+ fn $method(self, _: $lhs) -> Self::Output { todo!() }
+ }
+ )*
+ }
+}
+
+macro_rules! impl_assign_arith {
+ ( $( $_trait:ident, $lhs:ty, $rhs:ty, $method:ident; )* ) => {
+ $(
+ impl core::ops::$_trait<$lhs> for $rhs {
+ fn $method(&mut self, _: $lhs) {}
}
)*
}
}
impl_arith!(
- Add, i32, add;
- Div, i32, div;
- Mul, i32, mul;
- Sub, i32, sub;
-
- Add, f64, add;
- Div, f64, div;
- Mul, f64, mul;
- Sub, f64, sub;
+ Add, Custom, Custom, add;
+ Div, Custom, Custom, div;
+ Mul, Custom, Custom, mul;
+ Rem, Custom, Custom, rem;
+ Sub, Custom, Custom, sub;
+
+ Add, Custom, &Custom, add;
+ Div, Custom, &Custom, div;
+ Mul, Custom, &Custom, mul;
+ Rem, Custom, &Custom, rem;
+ Sub, Custom, &Custom, sub;
+
+ Add, &Custom, Custom, add;
+ Div, &Custom, Custom, div;
+ Mul, &Custom, Custom, mul;
+ Rem, &Custom, Custom, rem;
+ Sub, &Custom, Custom, sub;
+
+ Add, &Custom, &Custom, add;
+ Div, &Custom, &Custom, div;
+ Mul, &Custom, &Custom, mul;
+ Rem, &Custom, &Custom, rem;
+ Sub, &Custom, &Custom, sub;
+);
+
+impl_assign_arith!(
+ AddAssign, Custom, Custom, add_assign;
+ DivAssign, Custom, Custom, div_assign;
+ MulAssign, Custom, Custom, mul_assign;
+ RemAssign, Custom, Custom, rem_assign;
+ SubAssign, Custom, Custom, sub_assign;
+
+ AddAssign, Custom, &Custom, add_assign;
+ DivAssign, Custom, &Custom, div_assign;
+ MulAssign, Custom, &Custom, mul_assign;
+ RemAssign, Custom, &Custom, rem_assign;
+ SubAssign, Custom, &Custom, sub_assign;
+
+ AddAssign, &Custom, Custom, add_assign;
+ DivAssign, &Custom, Custom, div_assign;
+ MulAssign, &Custom, Custom, mul_assign;
+ RemAssign, &Custom, Custom, rem_assign;
+ SubAssign, &Custom, Custom, sub_assign;
+
+ AddAssign, &Custom, &Custom, add_assign;
+ DivAssign, &Custom, &Custom, div_assign;
+ MulAssign, &Custom, &Custom, mul_assign;
+ RemAssign, &Custom, &Custom, rem_assign;
+ SubAssign, &Custom, &Custom, sub_assign;
);
+impl core::ops::Neg for Custom {
+ type Output = Custom;
+ fn neg(self) -> Self::Output {
+ todo!()
+ }
+}
+impl core::ops::Neg for &Custom {
+ type Output = Custom;
+ fn neg(self) -> Self::Output {
+ todo!()
+ }
+}
+
pub fn association_with_structures_should_not_trigger_the_lint() {
enum Foo {
Bar = -2,
@@ -125,6 +190,18 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
_n *= &0;
_n *= 1;
_n *= &1;
+ _n += -0;
+ _n += &-0;
+ _n -= -0;
+ _n -= &-0;
+ _n /= -99;
+ _n /= &-99;
+ _n %= -99;
+ _n %= &-99;
+ _n *= -0;
+ _n *= &-0;
+ _n *= -1;
+ _n *= &-1;
// Binary
_n = _n + 0;
@@ -158,8 +235,9 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
_n = -&i32::MIN;
}
-pub fn runtime_ops() {
+pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
let mut _n = i32::MAX;
+ let mut _custom = Custom;
// Assign
_n += 1;
@@ -172,6 +250,36 @@ pub fn runtime_ops() {
_n %= &0;
_n *= 2;
_n *= &2;
+ _n += -1;
+ _n += &-1;
+ _n -= -1;
+ _n -= &-1;
+ _n /= -0;
+ _n /= &-0;
+ _n %= -0;
+ _n %= &-0;
+ _n *= -2;
+ _n *= &-2;
+ _custom += Custom;
+ _custom += &Custom;
+ _custom -= Custom;
+ _custom -= &Custom;
+ _custom /= Custom;
+ _custom /= &Custom;
+ _custom %= Custom;
+ _custom %= &Custom;
+ _custom *= Custom;
+ _custom *= &Custom;
+ _custom += -Custom;
+ _custom += &-Custom;
+ _custom -= -Custom;
+ _custom -= &-Custom;
+ _custom /= -Custom;
+ _custom /= &-Custom;
+ _custom %= -Custom;
+ _custom %= &-Custom;
+ _custom *= -Custom;
+ _custom *= &-Custom;
// Binary
_n = _n + 1;
@@ -193,36 +301,73 @@ pub fn runtime_ops() {
_n = 23 + &85;
_n = &23 + 85;
_n = &23 + &85;
-
- // Custom
- let _ = Custom + 0;
- let _ = Custom + 1;
- let _ = Custom + 2;
- let _ = Custom + 0.0;
- let _ = Custom + 1.0;
- let _ = Custom + 2.0;
- let _ = Custom - 0;
- let _ = Custom - 1;
- let _ = Custom - 2;
- let _ = Custom - 0.0;
- let _ = Custom - 1.0;
- let _ = Custom - 2.0;
- let _ = Custom / 0;
- let _ = Custom / 1;
- let _ = Custom / 2;
- let _ = Custom / 0.0;
- let _ = Custom / 1.0;
- let _ = Custom / 2.0;
- let _ = Custom * 0;
- let _ = Custom * 1;
- let _ = Custom * 2;
- let _ = Custom * 0.0;
- let _ = Custom * 1.0;
- let _ = Custom * 2.0;
+ _custom = _custom + _custom;
+ _custom = _custom + &_custom;
+ _custom = Custom + _custom;
+ _custom = &Custom + _custom;
+ _custom = _custom - Custom;
+ _custom = _custom - &Custom;
+ _custom = Custom - _custom;
+ _custom = &Custom - _custom;
+ _custom = _custom / Custom;
+ _custom = _custom / &Custom;
+ _custom = _custom % Custom;
+ _custom = _custom % &Custom;
+ _custom = _custom * Custom;
+ _custom = _custom * &Custom;
+ _custom = Custom * _custom;
+ _custom = &Custom * _custom;
+ _custom = Custom + &Custom;
+ _custom = &Custom + Custom;
+ _custom = &Custom + &Custom;
// Unary
_n = -_n;
_n = -&_n;
+ _custom = -_custom;
+ _custom = -&_custom;
+}
+
+// Copied and pasted from the `integer_arithmetic` lint for comparison.
+pub fn integer_arithmetic() {
+ let mut i = 1i32;
+ let mut var1 = 0i32;
+ let mut var2 = -1i32;
+
+ 1 + i;
+ i * 2;
+ 1 % i / 2;
+ i - 2 + 2 - i;
+ -i;
+ i >> 1;
+ i << 1;
+
+ -1;
+ -(-1);
+
+ i & 1;
+ i | 1;
+ i ^ 1;
+
+ i += 1;
+ i -= 1;
+ i *= 2;
+ i /= 2;
+ i /= 0;
+ i /= -1;
+ i /= var1;
+ i /= var2;
+ i %= 2;
+ i %= 0;
+ i %= -1;
+ i %= var1;
+ i %= var2;
+ i <<= 3;
+ i >>= 2;
+
+ i |= 1;
+ i &= 1;
+ i ^= i;
}
fn main() {}