---
title: Nested if expressions can be collapsed into a single condition
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Nested if expressions can be collapsed into a single condition
---

# Nested if expressions can be collapsed into a single condition

{% 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/collapsible-if`

**Language:** Rust

**Severity:** Warning

**Category:** Code Style

## Description{% #description %}

A nested `if` inside another `if` with no `else` clause can be collapsed into a single `if` using `&&`. For example, `if a { if b { ... } }` is equivalent to `if a && b { ... }`. The collapsed form is shorter and reduces indentation without changing behavior.

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

```rust
fn branch_statement(x: i32) -> i32 {
    if x > 0 {
        if y > 0 {
            return 0;
        }
    }
    return 1;
}
```

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

```rust
fn main() {
    if x > 0 {
        if y > 0 {
            return 1;
        }
    } else {
        return 0;
    }
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 