]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/arithmetic_side_effects.rs
merge rustc history
[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::unnecessary_owned_empty_strings,
6     arithmetic_overflow,
7     unconditional_panic
8 )]
9 #![feature(inline_const, saturating_int_impl)]
10 #![warn(clippy::arithmetic_side_effects)]
11
12 use core::num::{Saturating, Wrapping};
13
14 pub fn association_with_structures_should_not_trigger_the_lint() {
15     enum Foo {
16         Bar = -2,
17     }
18
19     impl Trait for Foo {
20         const ASSOC: i32 = {
21             let _: [i32; 1 + 1];
22             fn foo() {}
23             1 + 1
24         };
25     }
26
27     struct Baz([i32; 1 + 1]);
28
29     trait Trait {
30         const ASSOC: i32 = 1 + 1;
31     }
32
33     type Alias = [i32; 1 + 1];
34
35     union Qux {
36         field: [i32; 1 + 1],
37     }
38
39     let _: [i32; 1 + 1] = [0, 0];
40
41     let _: [i32; 1 + 1] = {
42         let a: [i32; 1 + 1] = [0, 0];
43         a
44     };
45 }
46
47 pub fn hard_coded_allowed() {
48     let _ = 1f32 + 1f32;
49     let _ = 1f64 + 1f64;
50
51     let _ = Saturating(0u32) + Saturating(0u32);
52     let _ = String::new() + "";
53     let _ = Wrapping(0u32) + Wrapping(0u32);
54
55     let saturating: Saturating<u32> = Saturating(0u32);
56     let string: String = String::new();
57     let wrapping: Wrapping<u32> = Wrapping(0u32);
58
59     let inferred_saturating = saturating + saturating;
60     let inferred_string = string + "";
61     let inferred_wrapping = wrapping + wrapping;
62
63     let _ = inferred_saturating + inferred_saturating;
64     let _ = inferred_string + "";
65     let _ = inferred_wrapping + inferred_wrapping;
66 }
67
68 #[rustfmt::skip]
69 pub fn const_ops_should_not_trigger_the_lint() {
70     const _: i32 = { let mut n = 1; n += 1; n };
71     let _ = const { let mut n = 1; n += 1; n };
72
73     const _: i32 = { let mut n = 1; n = n + 1; n };
74     let _ = const { let mut n = 1; n = n + 1; n };
75
76     const _: i32 = { let mut n = 1; n = 1 + n; n };
77     let _ = const { let mut n = 1; n = 1 + n; n };
78
79     const _: i32 = 1 + 1;
80     let _ = const { 1 + 1 };
81
82     const _: i32 = { let mut n = -1; n = -(-1); n = -n; n };
83     let _ = const { let mut n = -1; n = -(-1); n = -n; n };
84 }
85
86 pub fn non_overflowing_runtime_ops_or_ops_already_handled_by_the_compiler() {
87     let mut _n = i32::MAX;
88
89     // Assign
90     _n += 0;
91     _n -= 0;
92     _n /= 99;
93     _n %= 99;
94     _n *= 0;
95     _n *= 1;
96
97     // Binary
98     _n = _n + 0;
99     _n = 0 + _n;
100     _n = _n - 0;
101     _n = 0 - _n;
102     _n = _n / 99;
103     _n = _n % 99;
104     _n = _n * 0;
105     _n = 0 * _n;
106     _n = _n * 1;
107     _n = 1 * _n;
108     _n = 23 + 85;
109
110     // Unary
111     _n = -1;
112     _n = -(-1);
113 }
114
115 pub fn overflowing_runtime_ops() {
116     let mut _n = i32::MAX;
117
118     // Assign
119     _n += 1;
120     _n -= 1;
121     _n /= 0;
122     _n %= 0;
123     _n *= 2;
124
125     // Binary
126     _n = _n + 1;
127     _n = 1 + _n;
128     _n = _n - 1;
129     _n = 1 - _n;
130     _n = _n / 0;
131     _n = _n % 0;
132     _n = _n * 2;
133     _n = 2 * _n;
134
135     // Unary
136     _n = -_n;
137 }
138
139 fn main() {}