5 clippy::blacklisted_name,
7 clippy::redundant_clone,
22 let mut bar = Bar { a: 1, b: 2 };
24 std::mem::swap(&mut bar.a, &mut bar.b);
26 let mut baz = vec![bar.clone(), bar.clone()];
40 let foo = &mut [1, 2];
46 fn unswappable_slice() {
47 let foo = &mut [vec![1, 2], vec![3, 4]];
49 foo[0][1] = foo[1][0];
52 // swap(foo[0][1], foo[1][0]) would fail
53 // this could use split_at_mut and mem::swap, but that is not much simpler.
57 let mut foo = vec![1, 2];
63 fn xor_swap_locals() {
64 // This is an xor-based swap of local variables.
67 std::mem::swap(&mut a, &mut b)
71 // This is an xor-based swap of fields in a struct.
72 let mut bar = Bar { a: 0, b: 1 };
73 std::mem::swap(&mut bar.a, &mut bar.b)
77 // This is an xor-based swap of a slice
78 let foo = &mut [1, 2];
83 // This is a sequence of xor-assignment statements that doesn't result in a swap.
93 fn xor_unswappable_slice() {
94 let foo = &mut [vec![1, 2], vec![3, 4]];
95 foo[0][1] ^= foo[1][0];
96 foo[1][0] ^= foo[0][0];
97 foo[0][1] ^= foo[1][0];
99 // swap(foo[0][1], foo[1][0]) would fail
100 // this could use split_at_mut and mem::swap, but that is not much simpler.
103 fn distinct_slice() {
104 let foo = &mut [vec![1, 2], vec![3, 4]];
105 let bar = &mut [vec![1, 2], vec![3, 4]];
106 std::mem::swap(&mut foo[0][1], &mut bar[1][0]);
115 std::mem::swap(&mut a, &mut b);
117 ; std::mem::swap(&mut a, &mut b);
121 std::mem::swap(&mut c.0, &mut a);
123 ; std::mem::swap(&mut c.0, &mut a);
132 struct S3<'a, 'b>(&'a mut &'b mut S1);
134 impl std::ops::Deref for S2 {
136 fn deref(&self) -> &Self::Target {
140 impl std::ops::DerefMut for S2 {
141 fn deref_mut(&mut self) -> &mut Self::Target {
146 // Don't lint. `s.0` is mutably borrowed by `s.x` and `s.y` via the deref impl.
147 let mut s = S2(S1 { x: 0, y: 0 });
152 // Accessing through a mutable reference is fine
153 let mut s = S1 { x: 0, y: 0 };
156 std::mem::swap(&mut s.0.x, &mut s.0.y);