13
13
14
14
use core:: num:: { Saturating , Wrapping } ;
15
15
16
+ #[ derive( Clone , Copy ) ]
16
17
pub struct Custom ;
17
18
18
19
macro_rules! impl_arith {
19
- ( $( $_trait: ident, $ty : ty, $method: ident; ) * ) => {
20
+ ( $( $_trait: ident, $lhs : ty , $rhs : ty, $method: ident; ) * ) => {
20
21
$(
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) { }
24
35
}
25
36
) *
26
37
}
27
38
}
28
39
29
40
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;
39
90
) ;
40
91
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
+
41
105
pub fn association_with_structures_should_not_trigger_the_lint ( ) {
42
106
enum Foo {
43
107
Bar = -2 ,
@@ -173,6 +237,7 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
173
237
174
238
pub fn unknown_ops_or_runtime_ops_that_can_overflow ( ) {
175
239
let mut _n = i32:: MAX ;
240
+ let mut _custom = Custom ;
176
241
177
242
// Assign
178
243
_n += 1 ;
@@ -195,6 +260,26 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
195
260
_n %= & -0 ;
196
261
_n *= -2 ;
197
262
_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 ;
198
283
199
284
// Binary
200
285
_n = _n + 1 ;
@@ -216,36 +301,31 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
216
301
_n = 23 + & 85 ;
217
302
_n = & 23 + 85 ;
218
303
_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 ;
245
323
246
324
// Unary
247
325
_n = -_n;
248
326
_n = -& _n;
327
+ _custom = -_custom;
328
+ _custom = -& _custom;
249
329
}
250
330
251
331
// Copied and pasted from the `integer_arithmetic` lint for comparison.
0 commit comments