]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/arithmetic_side_effects.rs
Rollup merge of #102470 - est31:stabilize_const_char_convert, r=joshtriplett
[rust.git] / src / tools / clippy / tests / ui / arithmetic_side_effects.rs
1 #![allow(
2     clippy::assign_op_pattern,
3     clippy::erasing_op,
4     clippy::identity_op,
5     clippy::op_ref,
6     clippy::unnecessary_owned_empty_strings,
7     arithmetic_overflow,
8     unconditional_panic
9 )]
10 #![feature(const_mut_refs, inline_const, saturating_int_impl)]
11 #![warn(clippy::arithmetic_side_effects)]
12
13 use core::num::{Saturating, Wrapping};
14
15 pub struct Custom;
16
17 macro_rules! impl_arith {
18     ( $( $_trait:ident, $ty:ty, $method:ident; )* ) => {
19         $(
20             impl core::ops::$_trait<$ty> for Custom {
21                 type Output = Self;
22                 fn $method(self, _: $ty) -> Self::Output { Self }
23             }
24         )*
25     }
26 }
27
28 impl_arith!(
29     Add, i32, add;
30     Div, i32, div;
31     Mul, i32, mul;
32     Sub, i32, sub;
33
34     Add, f64, add;
35     Div, f64, div;
36     Mul, f64, mul;
37     Sub, f64, sub;
38 );
39
40 pub fn association_with_structures_should_not_trigger_the_lint() {
41     enum Foo {
42         Bar = -2,
43     }
44
45     impl Trait for Foo {
46         const ASSOC: i32 = {
47             let _: [i32; 1 + 1];
48             fn foo() {}
49             1 + 1
50         };
51     }
52
53     struct Baz([i32; 1 + 1]);
54
55     trait Trait {
56         const ASSOC: i32 = 1 + 1;
57     }
58
59     type Alias = [i32; 1 + 1];
60
61     union Qux {
62         field: [i32; 1 + 1],
63     }
64
65     let _: [i32; 1 + 1] = [0, 0];
66
67     let _: [i32; 1 + 1] = {
68         let a: [i32; 1 + 1] = [0, 0];
69         a
70     };
71 }
72
73 pub fn hard_coded_allowed() {
74     let _ = 1f32 + 1f32;
75     let _ = 1f64 + 1f64;
76
77     let _ = Saturating(0u32) + Saturating(0u32);
78     let _ = String::new() + "";
79     let _ = Wrapping(0u32) + Wrapping(0u32);
80
81     let saturating: Saturating<u32> = Saturating(0u32);
82     let string: String = String::new();
83     let wrapping: Wrapping<u32> = Wrapping(0u32);
84
85     let inferred_saturating = saturating + saturating;
86     let inferred_string = string + "";
87     let inferred_wrapping = wrapping + wrapping;
88
89     let _ = inferred_saturating + inferred_saturating;
90     let _ = inferred_string + "";
91     let _ = inferred_wrapping + inferred_wrapping;
92 }
93
94 #[rustfmt::skip]
95 pub fn const_ops_should_not_trigger_the_lint() {
96     const _: i32 = { let mut n = 1; n += 1; n };
97     let _ = const { let mut n = 1; n += 1; n };
98
99     const _: i32 = { let mut n = 1; n = n + 1; n };
100     let _ = const { let mut n = 1; n = n + 1; n };
101
102     const _: i32 = { let mut n = 1; n = 1 + n; n };
103     let _ = const { let mut n = 1; n = 1 + n; n };
104
105     const _: i32 = 1 + 1;
106     let _ = const { 1 + 1 };
107
108     const _: i32 = { let mut n = 1; n = -1; n = -(-1); n = -n; n };
109     let _ = const { let mut n = 1; n = -1; n = -(-1); n = -n; n };
110 }
111
112 pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_trigger_the_lint() {
113     let mut _n = i32::MAX;
114
115     // Assign
116     _n += 0;
117     _n += &0;
118     _n -= 0;
119     _n -= &0;
120     _n /= 99;
121     _n /= &99;
122     _n %= 99;
123     _n %= &99;
124     _n *= 0;
125     _n *= &0;
126     _n *= 1;
127     _n *= &1;
128
129     // Binary
130     _n = _n + 0;
131     _n = _n + &0;
132     _n = 0 + _n;
133     _n = &0 + _n;
134     _n = _n - 0;
135     _n = _n - &0;
136     _n = 0 - _n;
137     _n = &0 - _n;
138     _n = _n / 99;
139     _n = _n / &99;
140     _n = _n % 99;
141     _n = _n % &99;
142     _n = _n * 0;
143     _n = _n * &0;
144     _n = 0 * _n;
145     _n = &0 * _n;
146     _n = _n * 1;
147     _n = _n * &1;
148     _n = 1 * _n;
149     _n = &1 * _n;
150     _n = 23 + 85;
151
152     // Unary
153     _n = -1;
154     _n = -(-1);
155 }
156
157 pub fn runtime_ops() {
158     let mut _n = i32::MAX;
159
160     // Assign
161     _n += 1;
162     _n += &1;
163     _n -= 1;
164     _n -= &1;
165     _n /= 0;
166     _n /= &0;
167     _n %= 0;
168     _n %= &0;
169     _n *= 2;
170     _n *= &2;
171
172     // Binary
173     _n = _n + 1;
174     _n = _n + &1;
175     _n = 1 + _n;
176     _n = &1 + _n;
177     _n = _n - 1;
178     _n = _n - &1;
179     _n = 1 - _n;
180     _n = &1 - _n;
181     _n = _n / 0;
182     _n = _n / &0;
183     _n = _n % 0;
184     _n = _n % &0;
185     _n = _n * 2;
186     _n = _n * &2;
187     _n = 2 * _n;
188     _n = &2 * _n;
189     _n = 23 + &85;
190     _n = &23 + 85;
191     _n = &23 + &85;
192
193     // Custom
194     let _ = Custom + 0;
195     let _ = Custom + 1;
196     let _ = Custom + 2;
197     let _ = Custom + 0.0;
198     let _ = Custom + 1.0;
199     let _ = Custom + 2.0;
200     let _ = Custom - 0;
201     let _ = Custom - 1;
202     let _ = Custom - 2;
203     let _ = Custom - 0.0;
204     let _ = Custom - 1.0;
205     let _ = Custom - 2.0;
206     let _ = Custom / 0;
207     let _ = Custom / 1;
208     let _ = Custom / 2;
209     let _ = Custom / 0.0;
210     let _ = Custom / 1.0;
211     let _ = Custom / 2.0;
212     let _ = Custom * 0;
213     let _ = Custom * 1;
214     let _ = Custom * 2;
215     let _ = Custom * 0.0;
216     let _ = Custom * 1.0;
217     let _ = Custom * 2.0;
218
219     // Unary
220     _n = -_n;
221     _n = -&_n;
222 }
223
224 fn main() {}