]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/swap.rs
Rollup merge of #100030 - WaffleLapkin:nice_pointer_sis, r=scottmcm
[rust.git] / src / tools / clippy / tests / ui / swap.rs
1 // run-rustfix
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 )]
12
13 struct Foo(u32);
14
15 #[derive(Clone)]
16 struct Bar {
17     a: u32,
18     b: u32,
19 }
20
21 fn field() {
22     let mut bar = Bar { a: 1, b: 2 };
23
24     let temp = bar.a;
25     bar.a = bar.b;
26     bar.b = temp;
27
28     let mut baz = vec![bar.clone(), bar.clone()];
29     let temp = baz[0].a;
30     baz[0].a = baz[1].a;
31     baz[1].a = temp;
32 }
33
34 fn array() {
35     let mut foo = [1, 2];
36     let temp = foo[0];
37     foo[0] = foo[1];
38     foo[1] = temp;
39
40     foo.swap(0, 1);
41 }
42
43 fn slice() {
44     let foo = &mut [1, 2];
45     let temp = foo[0];
46     foo[0] = foo[1];
47     foo[1] = temp;
48
49     foo.swap(0, 1);
50 }
51
52 fn unswappable_slice() {
53     let foo = &mut [vec![1, 2], vec![3, 4]];
54     let temp = foo[0][1];
55     foo[0][1] = foo[1][0];
56     foo[1][0] = temp;
57
58     // swap(foo[0][1], foo[1][0]) would fail
59     // this could use split_at_mut and mem::swap, but that is not much simpler.
60 }
61
62 fn vec() {
63     let mut foo = vec![1, 2];
64     let temp = foo[0];
65     foo[0] = foo[1];
66     foo[1] = temp;
67
68     foo.swap(0, 1);
69 }
70
71 fn xor_swap_locals() {
72     // This is an xor-based swap of local variables.
73     let mut a = 0;
74     let mut b = 1;
75     a ^= b;
76     b ^= a;
77     a ^= b;
78 }
79
80 fn xor_field_swap() {
81     // This is an xor-based swap of fields in a struct.
82     let mut bar = Bar { a: 0, b: 1 };
83     bar.a ^= bar.b;
84     bar.b ^= bar.a;
85     bar.a ^= bar.b;
86 }
87
88 fn xor_slice_swap() {
89     // This is an xor-based swap of a slice
90     let foo = &mut [1, 2];
91     foo[0] ^= foo[1];
92     foo[1] ^= foo[0];
93     foo[0] ^= foo[1];
94 }
95
96 fn xor_no_swap() {
97     // This is a sequence of xor-assignment statements that doesn't result in a swap.
98     let mut a = 0;
99     let mut b = 1;
100     let mut c = 2;
101     a ^= b;
102     b ^= c;
103     a ^= c;
104     c ^= a;
105 }
106
107 fn xor_unswappable_slice() {
108     let foo = &mut [vec![1, 2], vec![3, 4]];
109     foo[0][1] ^= foo[1][0];
110     foo[1][0] ^= foo[0][0];
111     foo[0][1] ^= foo[1][0];
112
113     // swap(foo[0][1], foo[1][0]) would fail
114     // this could use split_at_mut and mem::swap, but that is not much simpler.
115 }
116
117 fn distinct_slice() {
118     let foo = &mut [vec![1, 2], vec![3, 4]];
119     let bar = &mut [vec![1, 2], vec![3, 4]];
120     let temp = foo[0][1];
121     foo[0][1] = bar[1][0];
122     bar[1][0] = temp;
123 }
124
125 #[rustfmt::skip]
126 fn main() {
127
128     let mut a = 42;
129     let mut b = 1337;
130
131     a = b;
132     b = a;
133
134     ; let t = a;
135     a = b;
136     b = t;
137
138     let mut c = Foo(42);
139
140     c.0 = a;
141     a = c.0;
142
143     ; let t = c.0;
144     c.0 = a;
145     a = t;
146 }
147
148 fn issue_8154() {
149     struct S1 {
150         x: i32,
151         y: i32,
152     }
153     struct S2(S1);
154     struct S3<'a, 'b>(&'a mut &'b mut S1);
155
156     impl std::ops::Deref for S2 {
157         type Target = S1;
158         fn deref(&self) -> &Self::Target {
159             &self.0
160         }
161     }
162     impl std::ops::DerefMut for S2 {
163         fn deref_mut(&mut self) -> &mut Self::Target {
164             &mut self.0
165         }
166     }
167
168     // Don't lint. `s.0` is mutably borrowed by `s.x` and `s.y` via the deref impl.
169     let mut s = S2(S1 { x: 0, y: 0 });
170     let t = s.x;
171     s.x = s.y;
172     s.y = t;
173
174     // Accessing through a mutable reference is fine
175     let mut s = S1 { x: 0, y: 0 };
176     let mut s = &mut s;
177     let s = S3(&mut s);
178     let t = s.0.x;
179     s.0.x = s.0.y;
180     s.0.y = t;
181 }