]> git.lizzy.rs Git - rust.git/blob - tests/ui/integer_arithmetic.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[rust.git] / tests / ui / integer_arithmetic.rs
1 #![warn(clippy::integer_arithmetic, clippy::float_arithmetic)]
2 #![allow(
3     unused,
4     clippy::shadow_reuse,
5     clippy::shadow_unrelated,
6     clippy::no_effect,
7     clippy::unnecessary_operation,
8     clippy::op_ref
9 )]
10
11 #[rustfmt::skip]
12 fn main() {
13     let mut i = 1i32;
14     1 + i;
15     i * 2;
16     1 %
17     i / 2; // no error, this is part of the expression in the preceding line
18     i - 2 + 2 - i;
19     -i;
20     i >> 1;
21     i << 1;
22
23     // no error, overflows are checked by `overflowing_literals`
24     -1;
25     -(-1);
26
27     i & 1; // no wrapping
28     i | 1;
29     i ^ 1;
30
31     i += 1;
32     i -= 1;
33     i *= 2;
34     i /= 2;
35     i %= 2;
36     i <<= 3;
37     i >>= 2;
38
39     // no errors
40     i |= 1;
41     i &= 1;
42     i ^= i;
43
44     // No errors for the following items because they are constant expressions
45     enum Foo {
46         Bar = -2,
47     }
48     struct Baz([i32; 1 + 1]);
49     union Qux {
50         field: [i32; 1 + 1],
51     }
52     type Alias = [i32; 1 + 1];
53
54     const FOO: i32 = -2;
55     static BAR: i32 = -2;
56
57     let _: [i32; 1 + 1] = [0, 0];
58
59     let _: [i32; 1 + 1] = {
60         let a: [i32; 1 + 1] = [0, 0];
61         a
62     };
63
64     trait Trait {
65         const ASSOC: i32 = 1 + 1;
66     }
67
68     impl Trait for Foo {
69         const ASSOC: i32 = {
70             let _: [i32; 1 + 1];
71             fn foo() {}
72             1 + 1
73         };
74     }
75 }
76
77 // warn on references as well! (#5328)
78 pub fn int_arith_ref() {
79     3 + &1;
80     &3 + 1;
81     &3 + &1;
82 }
83
84 pub fn foo(x: &i32) -> i32 {
85     let a = 5;
86     a + x
87 }
88
89 pub fn bar(x: &i32, y: &i32) -> i32 {
90     x + y
91 }
92
93 pub fn baz(x: i32, y: &i32) -> i32 {
94     x + y
95 }
96
97 pub fn qux(x: i32, y: i32) -> i32 {
98     (&x + &y)
99 }