Swap attempt that overwrites the first value

ID

rust.correctness.almost_swapped

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Logic

Language

Rust

Description

These two assignments look like a swap, but the first one already overwrote the value the second one reads, so both places end up holding the same value. Use std::mem::swap, or keep the original in a temporary before assigning.

Rationale

These two assignments look like a swap, but the first one already overwrote the value the second one reads, so both places end up holding the same value. Use std::mem::swap, or keep the original in a temporary before assigning.

The following code illustrates the pattern detected by this rule:

pub struct Point {
    x: i32,
    y: i32,
}

impl Point {
    pub fn broken_swap(&mut self) {
        // FLAGGED: Swap attempt that overwrites the first value
        self.x = self.y;
        self.y = self.x;
    }
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved