Why is this an issue?

The sequence foo = bar; bar = foo looks like a failed attempt to swap the values of two variables. This code mistakenly reassigns both variables, leading to incorrect and unintended behavior. Using std::mem::swap ensures a correct and intended swap.

Code examples

Noncompliant code example

let mut foo = 1;
let mut bar = 2;
foo = bar; // Noncompliant
bar = foo; // Noncompliant

Compliant solution

let mut foo = 1;
let mut bar = 2;
std::mem::swap(&mut foo, &mut bar); // Compliant

Resources

Documentation