Skip to content

Commit 41b2a3d

Browse files
committed
Auto merge of #10170 - c410-f3r:arith, r=dswij
[arithmetic_side_effects] Add more tests related to custom types Add tests to ensure that custom types are triggered with any type of arithmetic operation as well as combinations with or without references. --- changelog: none <!-- changelog_checked -->
2 parents 46981dc + fb34fbf commit 41b2a3d

File tree

2 files changed

+365
-183
lines changed

2 files changed

+365
-183
lines changed

tests/ui/arithmetic_side_effects.rs

+119-39
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,95 @@
1313

1414
use core::num::{Saturating, Wrapping};
1515

16+
#[derive(Clone, Copy)]
1617
pub struct Custom;
1718

1819
macro_rules! impl_arith {
19-
( $( $_trait:ident, $ty:ty, $method:ident; )* ) => {
20+
( $( $_trait:ident, $lhs:ty, $rhs:ty, $method:ident; )* ) => {
2021
$(
21-
impl core::ops::$_trait<$ty> for Custom {
22-
type Output = Self;
23-
fn $method(self, _: $ty) -> Self::Output { Self }
22+
impl core::ops::$_trait<$lhs> for $rhs {
23+
type Output = Custom;
24+
fn $method(self, _: $lhs) -> Self::Output { todo!() }
25+
}
26+
)*
27+
}
28+
}
29+
30+
macro_rules! impl_assign_arith {
31+
( $( $_trait:ident, $lhs:ty, $rhs:ty, $method:ident; )* ) => {
32+
$(
33+
impl core::ops::$_trait<$lhs> for $rhs {
34+
fn $method(&mut self, _: $lhs) {}
2435
}
2536
)*
2637
}
2738
}
2839

2940
impl_arith!(
30-
Add, i32, add;
31-
Div, i32, div;
32-
Mul, i32, mul;
33-
Sub, i32, sub;
34-
35-
Add, f64, add;
36-
Div, f64, div;
37-
Mul, f64, mul;
38-
Sub, f64, sub;
41+
Add, Custom, Custom, add;
42+
Div, Custom, Custom, div;
43+
Mul, Custom, Custom, mul;
44+
Rem, Custom, Custom, rem;
45+
Sub, Custom, Custom, sub;
46+
47+
Add, Custom, &Custom, add;
48+
Div, Custom, &Custom, div;
49+
Mul, Custom, &Custom, mul;
50+
Rem, Custom, &Custom, rem;
51+
Sub, Custom, &Custom, sub;
52+
53+
Add, &Custom, Custom, add;
54+
Div, &Custom, Custom, div;
55+
Mul, &Custom, Custom, mul;
56+
Rem, &Custom, Custom, rem;
57+
Sub, &Custom, Custom, sub;
58+
59+
Add, &Custom, &Custom, add;
60+
Div, &Custom, &Custom, div;
61+
Mul, &Custom, &Custom, mul;
62+
Rem, &Custom, &Custom, rem;
63+
Sub, &Custom, &Custom, sub;
64+
);
65+
66+
impl_assign_arith!(
67+
AddAssign, Custom, Custom, add_assign;
68+
DivAssign, Custom, Custom, div_assign;
69+
MulAssign, Custom, Custom, mul_assign;
70+
RemAssign, Custom, Custom, rem_assign;
71+
SubAssign, Custom, Custom, sub_assign;
72+
73+
AddAssign, Custom, &Custom, add_assign;
74+
DivAssign, Custom, &Custom, div_assign;
75+
MulAssign, Custom, &Custom, mul_assign;
76+
RemAssign, Custom, &Custom, rem_assign;
77+
SubAssign, Custom, &Custom, sub_assign;
78+
79+
AddAssign, &Custom, Custom, add_assign;
80+
DivAssign, &Custom, Custom, div_assign;
81+
MulAssign, &Custom, Custom, mul_assign;
82+
RemAssign, &Custom, Custom, rem_assign;
83+
SubAssign, &Custom, Custom, sub_assign;
84+
85+
AddAssign, &Custom, &Custom, add_assign;
86+
DivAssign, &Custom, &Custom, div_assign;
87+
MulAssign, &Custom, &Custom, mul_assign;
88+
RemAssign, &Custom, &Custom, rem_assign;
89+
SubAssign, &Custom, &Custom, sub_assign;
3990
);
4091

92+
impl core::ops::Neg for Custom {
93+
type Output = Custom;
94+
fn neg(self) -> Self::Output {
95+
todo!()
96+
}
97+
}
98+
impl core::ops::Neg for &Custom {
99+
type Output = Custom;
100+
fn neg(self) -> Self::Output {
101+
todo!()
102+
}
103+
}
104+
41105
pub fn association_with_structures_should_not_trigger_the_lint() {
42106
enum Foo {
43107
Bar = -2,
@@ -173,6 +237,7 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
173237

174238
pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
175239
let mut _n = i32::MAX;
240+
let mut _custom = Custom;
176241

177242
// Assign
178243
_n += 1;
@@ -195,6 +260,26 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
195260
_n %= &-0;
196261
_n *= -2;
197262
_n *= &-2;
263+
_custom += Custom;
264+
_custom += &Custom;
265+
_custom -= Custom;
266+
_custom -= &Custom;
267+
_custom /= Custom;
268+
_custom /= &Custom;
269+
_custom %= Custom;
270+
_custom %= &Custom;
271+
_custom *= Custom;
272+
_custom *= &Custom;
273+
_custom += -Custom;
274+
_custom += &-Custom;
275+
_custom -= -Custom;
276+
_custom -= &-Custom;
277+
_custom /= -Custom;
278+
_custom /= &-Custom;
279+
_custom %= -Custom;
280+
_custom %= &-Custom;
281+
_custom *= -Custom;
282+
_custom *= &-Custom;
198283

199284
// Binary
200285
_n = _n + 1;
@@ -216,36 +301,31 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
216301
_n = 23 + &85;
217302
_n = &23 + 85;
218303
_n = &23 + &85;
219-
220-
// Custom
221-
let _ = Custom + 0;
222-
let _ = Custom + 1;
223-
let _ = Custom + 2;
224-
let _ = Custom + 0.0;
225-
let _ = Custom + 1.0;
226-
let _ = Custom + 2.0;
227-
let _ = Custom - 0;
228-
let _ = Custom - 1;
229-
let _ = Custom - 2;
230-
let _ = Custom - 0.0;
231-
let _ = Custom - 1.0;
232-
let _ = Custom - 2.0;
233-
let _ = Custom / 0;
234-
let _ = Custom / 1;
235-
let _ = Custom / 2;
236-
let _ = Custom / 0.0;
237-
let _ = Custom / 1.0;
238-
let _ = Custom / 2.0;
239-
let _ = Custom * 0;
240-
let _ = Custom * 1;
241-
let _ = Custom * 2;
242-
let _ = Custom * 0.0;
243-
let _ = Custom * 1.0;
244-
let _ = Custom * 2.0;
304+
_custom = _custom + _custom;
305+
_custom = _custom + &_custom;
306+
_custom = Custom + _custom;
307+
_custom = &Custom + _custom;
308+
_custom = _custom - Custom;
309+
_custom = _custom - &Custom;
310+
_custom = Custom - _custom;
311+
_custom = &Custom - _custom;
312+
_custom = _custom / Custom;
313+
_custom = _custom / &Custom;
314+
_custom = _custom % Custom;
315+
_custom = _custom % &Custom;
316+
_custom = _custom * Custom;
317+
_custom = _custom * &Custom;
318+
_custom = Custom * _custom;
319+
_custom = &Custom * _custom;
320+
_custom = Custom + &Custom;
321+
_custom = &Custom + Custom;
322+
_custom = &Custom + &Custom;
245323

246324
// Unary
247325
_n = -_n;
248326
_n = -&_n;
327+
_custom = -_custom;
328+
_custom = -&_custom;
249329
}
250330

251331
// Copied and pasted from the `integer_arithmetic` lint for comparison.

0 commit comments

Comments
 (0)