For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/rust-code-quality/collapsible-if.md. A documentation index is available at /llms.txt.

Nested if expressions can be collapsed into a single condition

This product is not supported for your selected Datadog site. ().

Metadata

ID: rust-code-quality/collapsible-if

Language: Rust

Severity: Warning

Category: Code Style

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

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

Compliant Code Examples

fn main() {
    if x > 0 {
        if y > 0 {
            return 1;
        }
    } else {
        return 0;
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security