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