]> git.lizzy.rs Git - rust.git/blob - tests/ui/suspicious_arithmetic_impl.rs
Merge branch 'master' into rustfmt_tests
[rust.git] / tests / ui / suspicious_arithmetic_impl.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::suspicious_arithmetic_impl)]
11 use std::ops::{Add, AddAssign, Div, Mul, Sub};
12
13 #[derive(Copy, Clone)]
14 struct Foo(u32);
15
16 impl Add for Foo {
17     type Output = Foo;
18
19     fn add(self, other: Self) -> Self {
20         Foo(self.0 - other.0)
21     }
22 }
23
24 impl AddAssign for Foo {
25     fn add_assign(&mut self, other: Foo) {
26         *self = *self - other;
27     }
28 }
29
30 impl Mul for Foo {
31     type Output = Foo;
32
33     fn mul(self, other: Foo) -> Foo {
34         Foo(self.0 * other.0 % 42) // OK: BinOpKind::Rem part of BiExpr as parent node
35     }
36 }
37
38 impl Sub for Foo {
39     type Output = Foo;
40
41     fn sub(self, other: Self) -> Self {
42         Foo(self.0 * other.0 - 42) // OK: BinOpKind::Mul part of BiExpr as child node
43     }
44 }
45
46 impl Div for Foo {
47     type Output = Foo;
48
49     fn div(self, other: Self) -> Self {
50         Foo(do_nothing(self.0 + other.0) / 42) // OK: BinOpKind::Add part of BiExpr as child node
51     }
52 }
53
54 struct Bar(i32);
55
56 impl Add for Bar {
57     type Output = Bar;
58
59     fn add(self, other: Self) -> Self {
60         Bar(self.0 & !other.0) // OK: UnNot part of BiExpr as child node
61     }
62 }
63
64 impl Sub for Bar {
65     type Output = Bar;
66
67     fn sub(self, other: Self) -> Self {
68         if self.0 <= other.0 {
69             Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node
70         } else {
71             Bar(0)
72         }
73     }
74 }
75
76 fn main() {}
77
78 fn do_nothing(x: u32) -> u32 {
79     x
80 }