if/else that yields only the boolean literals true and false

ID

rust.maintainability.simplify_boolean_expression

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Redundancy

Language

Rust

Description

Reports an if/else whose branches produce nothing but true and false, either as the trailing expression or through return. The condition already is the value being computed.

Rationale

Five lines of branching stand in for the condition itself, so the reader has to check both branches to work out whether the result follows the condition or negates it. The branching also blocks the obvious next step - the expression cannot be combined with &&, || or ! without first being read and rewritten - and an edit that flips one branch silently inverts the result.

The following code illustrates the pattern detected by this rule:

pub fn positive(x: i32) -> bool {
    // FLAGGED: if/else that yields only the boolean literals true and false
    if x > 0 {
        true
    } else {
        false
    }
}

Remediation

Return the condition: if c { true } else { false } becomes c, and the inverted form becomes !c. When the condition is long, bind it to a well-named local first (let is_valid = …​; is_valid), which keeps the name the branching was there to supply.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved