For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/rust-code-quality/self-assignment.md. A documentation index is available at /llms.txt.

Variable or field assigned to itself

This product is not supported for your selected Datadog site. ().

Metadata

ID: rust-code-quality/self-assignment

Language: Rust

Severity: Error

Category: Error Prone

Description

Assigning a variable or field to itself is always a no-op and almost certainly indicates a typo where the wrong variable was referenced on one side.

Example

// Before — a.x is never updated, likely a copy-paste bug
fn copy_position(a: &mut Event, b: &Event) {
    a.x = a.x;
}

// After — copy from b as intended
fn copy_position(a: &mut Event, b: &Event) {
    a.x = b.x;
}

Non-Compliant Code Examples

fn self_assign_var(mut a: i32) {
    a = a;
}

struct Point { x: i32, y: i32 }

fn self_assign_field(p: &mut Point) {
    p.x = p.x;
}

Compliant Code Examples

fn assign_different(a: i32, b: i32) {
    let mut x = a;
    x = b;
}

struct Point { x: i32, y: i32 }

fn copy_field(a: &mut Point, b: &Point) {
    a.x = b.x;
}

fn different_fields(p: &mut Point) {
    p.x = p.y;
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security