]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/integer_arithmetic.rs
Rollup merge of #88375 - joshlf:patch-3, r=dtolnay
[rust.git] / src / tools / clippy / tests / ui / integer_arithmetic.rs
1 #![warn(clippy::integer_arithmetic, clippy::float_arithmetic)]
2 #![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::op_ref)]
3
4 #[rustfmt::skip]
5 fn main() {
6     let mut i = 1i32;
7     let mut var1 = 0i32;
8     let mut var2 = -1i32;
9     1 + i;
10     i * 2;
11     1 %
12     i / 2; // no error, this is part of the expression in the preceding line
13     i - 2 + 2 - i;
14     -i;
15     i >> 1;
16     i << 1;
17
18     // no error, overflows are checked by `overflowing_literals`
19     -1;
20     -(-1);
21
22     i & 1; // no wrapping
23     i | 1;
24     i ^ 1;
25
26     i += 1;
27     i -= 1;
28     i *= 2;
29     i /= 2;
30     i /= 0;
31     i /= -1;
32     i /= var1;
33     i /= var2;
34     i %= 2;
35     i %= 0;
36     i %= -1;
37     i %= var1;
38     i %= var2;
39     i <<= 3;
40     i >>= 2;
41
42     // no errors
43     i |= 1;
44     i &= 1;
45     i ^= i;
46
47     // No errors for the following items because they are constant expressions
48     enum Foo {
49         Bar = -2,
50     }
51     struct Baz([i32; 1 + 1]);
52     union Qux {
53         field: [i32; 1 + 1],
54     }
55     type Alias = [i32; 1 + 1];
56
57     const FOO: i32 = -2;
58     static BAR: i32 = -2;
59
60     let _: [i32; 1 + 1] = [0, 0];
61
62     let _: [i32; 1 + 1] = {
63         let a: [i32; 1 + 1] = [0, 0];
64         a
65     };
66
67     trait Trait {
68         const ASSOC: i32 = 1 + 1;
69     }
70
71     impl Trait for Foo {
72         const ASSOC: i32 = {
73             let _: [i32; 1 + 1];
74             fn foo() {}
75             1 + 1
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 }