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