]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/swap.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / swap.rs
1 //@aux-build: macro_rules.rs
2
3 #![warn(clippy::all)]
4 #![allow(
5 clippy::disallowed_names,
6 clippy::no_effect,
7 clippy::redundant_clone,
8 redundant_semicolons,
9 dead_code,
10 unused_assignments,
11 unused_variables,
12 clippy::let_and_return,
13 clippy::useless_vec,
14 clippy::redundant_locals
15 )]
16
17 struct Foo(u32);
18
19 #[derive(Clone)]
20 struct Bar {
21 a: u32,
22 b: u32,
23 }
24
25 fn field() {
26 let mut bar = Bar { a: 1, b: 2 };
27
28 let temp = bar.a;
29 bar.a = bar.b;
30 bar.b = temp;
31
32 let mut baz = vec![bar.clone(), bar.clone()];
33 let temp = baz[0].a;
34 baz[0].a = baz[1].a;
35 baz[1].a = temp;
36 }
37
38 fn array() {
39 let mut foo = [1, 2];
40 let temp = foo[0];
41 foo[0] = foo[1];
42 foo[1] = temp;
43
44 foo.swap(0, 1);
45 }
46
47 fn slice() {
48 let foo = &mut [1, 2];
49 let temp = foo[0];
50 foo[0] = foo[1];
51 foo[1] = temp;
52
53 foo.swap(0, 1);
54 }
55
56 fn unswappable_slice() {
57 let foo = &mut [vec![1, 2], vec![3, 4]];
58 let temp = foo[0][1];
59 foo[0][1] = foo[1][0];
60 foo[1][0] = temp;
61
62 // swap(foo[0][1], foo[1][0]) would fail
63 // this could use split_at_mut and mem::swap, but that is not much simpler.
64 }
65
66 fn vec() {
67 let mut foo = vec![1, 2];
68 let temp = foo[0];
69 foo[0] = foo[1];
70 foo[1] = temp;
71
72 foo.swap(0, 1);
73 }
74
75 fn xor_swap_locals() {
76 // This is an xor-based swap of local variables.
77 let mut a = 0;
78 let mut b = 1;
79 a ^= b;
80 b ^= a;
81 a ^= b;
82 }
83
84 fn xor_field_swap() {
85 // This is an xor-based swap of fields in a struct.
86 let mut bar = Bar { a: 0, b: 1 };
87 bar.a ^= bar.b;
88 bar.b ^= bar.a;
89 bar.a ^= bar.b;
90 }
91
92 fn xor_slice_swap() {
93 // This is an xor-based swap of a slice
94 let foo = &mut [1, 2];
95 foo[0] ^= foo[1];
96 foo[1] ^= foo[0];
97 foo[0] ^= foo[1];
98 }
99
100 fn xor_no_swap() {
101 // This is a sequence of xor-assignment statements that doesn't result in a swap.
102 let mut a = 0;
103 let mut b = 1;
104 let mut c = 2;
105 a ^= b;
106 b ^= c;
107 a ^= c;
108 c ^= a;
109 }
110
111 fn xor_unswappable_slice() {
112 let foo = &mut [vec![1, 2], vec![3, 4]];
113 foo[0][1] ^= foo[1][0];
114 foo[1][0] ^= foo[0][0];
115 foo[0][1] ^= foo[1][0];
116
117 // swap(foo[0][1], foo[1][0]) would fail
118 // this could use split_at_mut and mem::swap, but that is not much simpler.
119 }
120
121 fn distinct_slice() {
122 let foo = &mut [vec![1, 2], vec![3, 4]];
123 let bar = &mut [vec![1, 2], vec![3, 4]];
124 let temp = foo[0][1];
125 foo[0][1] = bar[1][0];
126 bar[1][0] = temp;
127 }
128
129 #[rustfmt::skip]
130 fn main() {
131
132 let mut a = 42;
133 let mut b = 1337;
134
135 a = b;
136 b = a;
137
138 ; let t = a;
139 a = b;
140 b = t;
141
142 let mut c = Foo(42);
143
144 c.0 = a;
145 a = c.0;
146
147 ; let t = c.0;
148 c.0 = a;
149 a = t;
150
151 let a = b;
152 let b = a;
153
154 let mut c = 1;
155 let mut d = 2;
156 d = c;
157 c = d;
158
159 let mut b = 1;
160 let a = b;
161 b = a;
162
163 let b = 1;
164 let a = 2;
165
166 let t = b;
167 let b = a;
168 let a = t;
169
170 let mut b = 1;
171 let mut a = 2;
172
173 let t = b;
174 b = a;
175 a = t;
176 }
177
178 fn issue_8154() {
179 struct S1 {
180 x: i32,
181 y: i32,
182 }
183 struct S2(S1);
184 struct S3<'a, 'b>(&'a mut &'b mut S1);
185
186 impl std::ops::Deref for S2 {
187 type Target = S1;
188 fn deref(&self) -> &Self::Target {
189 &self.0
190 }
191 }
192 impl std::ops::DerefMut for S2 {
193 fn deref_mut(&mut self) -> &mut Self::Target {
194 &mut self.0
195 }
196 }
197
198 // Don't lint. `s.0` is mutably borrowed by `s.x` and `s.y` via the deref impl.
199 let mut s = S2(S1 { x: 0, y: 0 });
200 let t = s.x;
201 s.x = s.y;
202 s.y = t;
203
204 // Accessing through a mutable reference is fine
205 let mut s = S1 { x: 0, y: 0 };
206 let mut s = &mut s;
207 let s = S3(&mut s);
208 let t = s.0.x;
209 s.0.x = s.0.y;
210 s.0.y = t;
211 }
212
213 const fn issue_9864(mut u: u32) -> u32 {
214 let mut v = 10;
215
216 let temp = u;
217 u = v;
218 v = temp;
219 u + v
220 }
221
222 #[macro_use]
223 extern crate macro_rules;
224
225 const fn issue_10421(x: u32) -> u32 {
226 issue_10421!();
227 let a = x;
228 let a = a;
229 let a = a;
230 a
231 }