---
title: Variable or field assigned to itself
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Variable or field assigned to itself
---

# Variable or field assigned to itself

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `rust-code-quality/self-assignment`

**Language:** Rust

**Severity:** Error

**Category:** Error Prone

## Description{% #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{% #example %}

```rust
// 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{% #non-compliant-code-examples %}

```rust
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{% #compliant-code-examples %}

```rust
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;
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 