summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/suspicious_arithmetic_impl.rs
blob: ae253a0487cb40083457f728c6255163168bc333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#![warn(clippy::suspicious_arithmetic_impl)]
use std::ops::{
    Add, AddAssign, BitAnd, BitOr, BitOrAssign, BitXor, Div, DivAssign, Mul, MulAssign, Rem, Shl, Shr, Sub,
};

#[derive(Copy, Clone)]
struct Foo(u32);

impl Add for Foo {
    type Output = Foo;

    fn add(self, other: Self) -> Self {
        Foo(self.0 - other.0)
    }
}

impl AddAssign for Foo {
    fn add_assign(&mut self, other: Foo) {
        *self = *self - other;
    }
}

impl BitOrAssign for Foo {
    fn bitor_assign(&mut self, other: Foo) {
        let idx = other.0;
        self.0 |= 1 << idx; // OK: BinOpKind::Shl part of AssignOp as child node
    }
}

impl MulAssign for Foo {
    fn mul_assign(&mut self, other: Foo) {
        self.0 /= other.0;
    }
}

impl DivAssign for Foo {
    fn div_assign(&mut self, other: Foo) {
        self.0 /= other.0; // OK: BinOpKind::Div == DivAssign
    }
}

impl Mul for Foo {
    type Output = Foo;

    fn mul(self, other: Foo) -> Foo {
        Foo(self.0 * other.0 % 42) // OK: BinOpKind::Rem part of BiExpr as parent node
    }
}

impl Sub for Foo {
    type Output = Foo;

    fn sub(self, other: Self) -> Self {
        Foo(self.0 * other.0 - 42) // OK: BinOpKind::Mul part of BiExpr as child node
    }
}

impl Div for Foo {
    type Output = Foo;

    fn div(self, other: Self) -> Self {
        Foo(do_nothing(self.0 + other.0) / 42) // OK: BinOpKind::Add part of BiExpr as child node
    }
}

impl Rem for Foo {
    type Output = Foo;

    fn rem(self, other: Self) -> Self {
        Foo(self.0 / other.0)
    }
}

impl BitAnd for Foo {
    type Output = Foo;

    fn bitand(self, other: Self) -> Self {
        Foo(self.0 | other.0)
    }
}

impl BitOr for Foo {
    type Output = Foo;

    fn bitor(self, other: Self) -> Self {
        Foo(self.0 ^ other.0)
    }
}

impl BitXor for Foo {
    type Output = Foo;

    fn bitxor(self, other: Self) -> Self {
        Foo(self.0 & other.0)
    }
}

impl Shl for Foo {
    type Output = Foo;

    fn shl(self, other: Self) -> Self {
        Foo(self.0 >> other.0)
    }
}

impl Shr for Foo {
    type Output = Foo;

    fn shr(self, other: Self) -> Self {
        Foo(self.0 << other.0)
    }
}

struct Bar(i32);

impl Add for Bar {
    type Output = Bar;

    fn add(self, other: Self) -> Self {
        Bar(self.0 & !other.0) // OK: Not part of BiExpr as child node
    }
}

impl Sub for Bar {
    type Output = Bar;

    fn sub(self, other: Self) -> Self {
        if self.0 <= other.0 {
            Bar(-(self.0 & other.0)) // OK: Neg part of BiExpr as parent node
        } else {
            Bar(0)
        }
    }
}

fn main() {}

fn do_nothing(x: u32) -> u32 {
    x
}

struct MultipleBinops(u32);

impl Add for MultipleBinops {
    type Output = MultipleBinops;

    // OK: multiple Binops in `add` impl
    fn add(self, other: Self) -> Self::Output {
        let mut result = self.0 + other.0;
        if result >= u32::max_value() {
            result -= u32::max_value();
        }
        MultipleBinops(result)
    }
}

impl Mul for MultipleBinops {
    type Output = MultipleBinops;

    // OK: multiple Binops in `mul` impl
    fn mul(self, other: Self) -> Self::Output {
        let mut result: u32 = 0;
        let size = std::cmp::max(self.0, other.0) as usize;
        let mut v = vec![0; size + 1];
        for i in 0..size + 1 {
            result *= i as u32;
        }
        MultipleBinops(result)
    }
}