]> git.lizzy.rs Git - rust.git/blob - tests/ui/suspicious_arithmetic_impl.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[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 }