---
title: Binary expression with identical operands on both sides
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Binary expression with identical operands on both sides
---

# Binary expression with identical operands on both sides

{% 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/identical-operand-comparison`

**Language:** Rust

**Severity:** Error

**Category:** Error Prone

## Description{% #description %}

Using the same operand on both sides of a binary operator is almost always a bug, typically a copy-paste error where the wrong variable was used.

- With `==`, `<=`, or `>=` the expression is always `true`.
- With `!=`, `<`, or `>` the expression is always `false`.
- With `&&`, `||`, `&`, `|`, or `^` the result is redundant.
- With `-` the result is always `0`; with `/` the result is always `1`.

## Example{% #example %}

```
// Before — always evaluates to true, likely a bug
if timeout == timeout {
    return Err("timed out");
}

// After — compare against the intended value
if elapsed == timeout {
    return Err("timed out");
}
```

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```rust
fn always_true(a: i32) -> bool {
    a == a
}

fn always_false(a: i32) -> bool {
    a != a
}

struct Point { x: i32, y: i32 }

fn field_always_true(p: Point) -> bool {
    p.x == p.x
}

fn redundant_and(a: bool) -> bool {
    a && a
}

fn always_zero(a: i32) -> i32 {
    a - a
}

fn always_false_lt(a: i32) -> bool {
    a < a
}

fn always_true_gte(a: i32) -> bool {
    a >= a
}
```

## Compliant Code Examples{% #compliant-code-examples %}

```rust
fn compare_different(a: i32, b: i32) -> bool {
    a == b
}

fn compare_fields(x: i32, y: i32) -> bool {
    x != y
}

struct Point { x: i32, y: i32 }

fn compare_field_different(p: Point, q: Point) -> bool {
    p.x == q.x
}

fn bitwise_different(a: u32, b: u32) -> u32 {
    a & b
}

fn func_call_not_flagged(mut v: Vec<i32>) -> bool {
    v.pop() == v.pop()
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 