Nested if whose outer branch holds nothing else

ID

rust.maintainability.collapsible_if

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Complexity

Language

Rust

Description

Reports an if whose block contains only another if, with neither of the two carrying an else branch. The two conditions can be merged into a single test.

Rationale

The outer if adds a decision point and a level of indentation without expressing a separate decision, because both conditions have to hold before anything runs. Flattening removes a branch from the function’s cyclomatic complexity, shortens the path a reader has to follow, and makes it plain that the two tests belong together.

The following code illustrates the pattern detected by this rule:

pub fn both_set(a: bool, b: bool) {
    // FLAGGED: Nested if whose outer branch holds nothing else
    if a {
        if b {
            println!("both");
        }
    }
}

Remediation

Merge the conditions with &&: if a { if b { …​ } } becomes if a && b { …​ }. Keep the nesting only when an else is about to be added to one of the two levels, or when the outer block is about to gain further statements.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved