if condition constant-folds to always-true or always-false

ID

c.correctness.constant_condition

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

low

Resource

Dead Code

Language

C / C++

Description

Reports an if condition that provably evaluates to the same truth value on every run, because it constant-folds to a fixed non-zero or zero value.

Rationale

When a condition always evaluates the same way, one branch of the if is unreachable dead code and the other is unconditional — the if no longer expresses a real decision. This is usually a leftover from a refactor (a variable that used to vary is now assigned a fixed value) or a bug where the intended runtime value was never wired in, and either way the reader is misled into thinking both branches are live.

The following code illustrates the pattern detected by this rule:

extern void do_something();
void literal_false()
{
    // FLAGGED: if condition constant-folds to always-true or always-false
    if (0) {
        do_something();
    }
}

Remediation

If the condition is genuinely meant to be constant, remove the if and keep only the branch that always runs (or delete the dead one), possibly with a comment explaining why. If it is not meant to be constant, fix the assignment so the variable actually carries a runtime value.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved