]> git.lizzy.rs Git - rust.git/blob - tests/ui/suspicious_arithmetic_impl.rs
Merge commit '09bd400243ed6f7059fedc0c1623aae3792521d6' into clippyup
[rust.git] / tests / ui / suspicious_arithmetic_impl.rs
1 #![warn(clippy::suspicious_arithmetic_impl)]
2 use std::ops::{Add, AddAssign, BitOrAssign, Div, DivAssign, Mul, MulAssign, Sub};
3
4 #[derive(Copy, Clone)]
5 struct Foo(u32);
6
7 impl Add for Foo {
8     type Output = Foo;
9
10     fn add(self, other: Self) -> Self {
11         Foo(self.0 - other.0)
12     }
13 }
14
15 impl AddAssign for Foo {
16     fn add_assign(&mut self, other: Foo) {
17         *self = *self - other;
18     }
19 }
20
21 impl BitOrAssign for Foo {
22     fn bitor_assign(&mut self, other: Foo) {
23         let idx = other.0;
24         self.0 |= 1 << idx; // OK: BinOpKind::Shl part of AssignOp as child node
25     }
26 }
27
28 impl MulAssign for Foo {
29     fn mul_assign(&mut self, other: Foo) {
30         self.0 /= other.0;
31     }
32 }
33
34 impl DivAssign for Foo {
35     fn div_assign(&mut self, other: Foo) {
36         self.0 /= other.0; // OK: BinOpKind::Div == DivAssign
37     }
38 }
39
40 impl Mul for Foo {
41     type Output = Foo;
42
43     fn mul(self, other: Foo) -> Foo {
44         Foo(self.0 * other.0 % 42) // OK: BinOpKind::Rem part of BiExpr as parent node
45     }
46 }
47
48 impl Sub for Foo {
49     type Output = Foo;
50
51     fn sub(self, other: Self) -> Self {
52         Foo(self.0 * other.0 - 42) // OK: BinOpKind::Mul part of BiExpr as child node
53     }
54 }
55
56 impl Div for Foo {
57     type Output = Foo;
58
59     fn div(self, other: Self) -> Self {
60         Foo(do_nothing(self.0 + other.0) / 42) // OK: BinOpKind::Add part of BiExpr as child node
61     }
62 }
63
64 struct Bar(i32);
65
66 impl Add for Bar {
67     type Output = Bar;
68
69     fn add(self, other: Self) -> Self {
70         Bar(self.0 & !other.0) // OK: UnNot part of BiExpr as child node
71     }
72 }
73
74 impl Sub for Bar {
75     type Output = Bar;
76
77     fn sub(self, other: Self) -> Self {
78         if self.0 <= other.0 {
79             Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node
80         } else {
81             Bar(0)
82         }
83     }
84 }
85
86 fn main() {}
87
88 fn do_nothing(x: u32) -> u32 {
89     x
90 }
91
92 struct MultipleBinops(u32);
93
94 impl Add for MultipleBinops {
95     type Output = MultipleBinops;
96
97     // OK: multiple Binops in `add` impl
98     fn add(self, other: Self) -> Self::Output {
99         let mut result = self.0 + other.0;
100         if result >= u32::max_value() {
101             result -= u32::max_value();
102         }
103         MultipleBinops(result)
104     }
105 }
106
107 impl Mul for MultipleBinops {
108     type Output = MultipleBinops;
109
110     // OK: multiple Binops in `mul` impl
111     fn mul(self, other: Self) -> Self::Output {
112         let mut result: u32 = 0;
113         let size = std::cmp::max(self.0, other.0) as usize;
114         let mut v = vec![0; size + 1];
115         for i in 0..size + 1 {
116             result *= i as u32;
117         }
118         MultipleBinops(result)
119     }
120 }