]> git.lizzy.rs Git - rust.git/blob - tests/ui/suspicious_arithmetic_impl.rs
Merge pull request #2539 from Baelyk/master
[rust.git] / tests / ui / suspicious_arithmetic_impl.rs
1
2
3
4 #![warn(suspicious_arithmetic_impl)]
5 use std::ops::{Add, AddAssign, Mul, Sub, Div};
6
7 #[derive(Copy, Clone)]
8 struct Foo(u32);
9
10 impl Add for Foo {
11     type Output = Foo;
12
13     fn add(self, other: Self) -> Self {
14         Foo(self.0 - other.0)
15     }
16 }
17
18 impl AddAssign for Foo {
19     fn add_assign(&mut self, other: Foo) {
20         *self = *self - other;
21     }
22 }
23
24 impl Mul for Foo {
25     type Output = Foo;
26
27     fn mul(self, other: Foo) -> Foo {
28         Foo(self.0 * other.0 % 42) // OK: BiRem part of BiExpr as parent node
29     }
30 }
31
32 impl Sub for Foo {
33     type Output = Foo;
34
35     fn sub(self, other: Self) -> Self {
36         Foo(self.0 * other.0 - 42) // OK: BiMul part of BiExpr as child node
37     }
38 }
39
40 impl Div for Foo {
41     type Output = Foo;
42
43     fn div(self, other: Self) -> Self {
44         Foo(do_nothing(self.0 + other.0) / 42) // OK: BiAdd part of BiExpr as child node
45     }
46 }
47
48 struct Bar(i32);
49
50 impl Add for Bar {
51     type Output = Bar;
52
53     fn add(self, other: Self) -> Self {
54         Bar(self.0 & !other.0) // OK: UnNot part of BiExpr as child node
55     }
56 }
57
58 impl Sub for Bar {
59     type Output = Bar;
60
61     fn sub(self, other: Self) -> Self {
62         if self.0 <= other.0 {
63             Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node
64         } else {
65             Bar(0)
66         }
67     }
68 }
69
70 fn main() {}
71
72 fn do_nothing(x: u32) -> u32 {
73     x
74 }