rspec/rules/S7437/rust/rule.adoc
github-actions[bot] 00d540f5ed
Create rule S7437 Variables should be swapped using std::mem::swap (#4780)
* Create rule S7437

* Update rule.adoc

* Update metadata.json

* Update metadata.json

* Update metadata.json

* Update metadata.json

* Update metadata.json

---------

Co-authored-by: sallaigy <sallaigy@users.noreply.github.com>
Co-authored-by: Gyula Sallai <gyula.sallai@sonarsource.com>
2025-03-19 14:10:07 +00:00

30 lines
765 B
Plaintext

== 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
[source,rust,diff-id=1,diff-type=noncompliant]
----
let mut foo = 1;
let mut bar = 2;
foo = bar; // Noncompliant
bar = foo; // Noncompliant
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
let mut foo = 1;
let mut bar = 2;
std::mem::swap(&mut foo, &mut bar); // Compliant
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped