]> git.lizzy.rs Git - rust.git/blob - tests/ui/suspicious_arithmetic_impl.rs
map_unit_fn: rename tests to fixable
[rust.git] / tests / ui / suspicious_arithmetic_impl.rs
1 #![warn(clippy::suspicious_arithmetic_impl)]
2 use std::ops::{Add, AddAssign, Div, Mul, 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 Mul for Foo {
22     type Output = Foo;
23
24     fn mul(self, other: Foo) -> Foo {
25         Foo(self.0 * other.0 % 42) // OK: BinOpKind::Rem part of BiExpr as parent node
26     }
27 }
28
29 impl Sub for Foo {
30     type Output = Foo;
31
32     fn sub(self, other: Self) -> Self {
33         Foo(self.0 * other.0 - 42) // OK: BinOpKind::Mul part of BiExpr as child node
34     }
35 }
36
37 impl Div for Foo {
38     type Output = Foo;
39
40     fn div(self, other: Self) -> Self {
41         Foo(do_nothing(self.0 + other.0) / 42) // OK: BinOpKind::Add part of BiExpr as child node
42     }
43 }
44
45 struct Bar(i32);
46
47 impl Add for Bar {
48     type Output = Bar;
49
50     fn add(self, other: Self) -> Self {
51         Bar(self.0 & !other.0) // OK: UnNot part of BiExpr as child node
52     }
53 }
54
55 impl Sub for Bar {
56     type Output = Bar;
57
58     fn sub(self, other: Self) -> Self {
59         if self.0 <= other.0 {
60             Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node
61         } else {
62             Bar(0)
63         }
64     }
65 }
66
67 fn main() {}
68
69 fn do_nothing(x: u32) -> u32 {
70     x
71 }