---
title: Simplify if/else that returns opposite boolean literals
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Simplify if/else that returns opposite boolean literals
---

# Simplify if/else that returns opposite boolean literals

{% 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/needless-bool`

**Language:** Rust

**Severity:** Warning

**Category:** Code Style

## Description{% #description %}

An `if`/`else` expression that returns `true` in one branch and `false` in the other is equivalent to the condition itself (or its negation). The boolean literal branches are redundant and add noise without adding clarity.

## How to remediate?{% #how-to-remediate %}

Replace the `if`/`else` with the condition directly:

```rust
// Before
if x > 0 { true } else { false }

// After
x > 0

// Before
if x > 0 { false } else { true }

// After
!(x > 0)
```

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

```rust
fn check_positive(x: i32) -> bool {
    if x > 0 { true } else { false }
}

fn check_negative(x: i32) -> bool {
    if x > 0 { false } else { true }
}

fn is_admin(role: &str) -> bool {
      if role == "admin" { true } else { false }
}
```

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

```rust
fn same_branch(x: i32) -> bool {
    if x > 0 { true } else { true }
}

fn non_literal(x: i32) -> bool {
    if x > 0 { x > 1 } else { false }
}

fn no_else(x: i32) -> bool {
    if x > 0 { return true; }
    false
}

fn has_permission(role: &str, action: &str) -> bool {
      if role == "admin" { log_access(action); true } else { false }
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 