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