Loop body always returns on the first iteration

ID

c.correctness.loop_at_most_one_iteration

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

low

Resource

Control Flow

Language

C / C++

Description

This loop always leaves on its first iteration: its body ends with an unconditional return and contains no conditional branch that could keep it going. The loop header is misleading — it reads as if the body repeats. Either make the exit conditional, or drop the loop and write the single pass directly.

Rationale

This loop always leaves on its first iteration: its body ends with an unconditional return and contains no conditional branch that could keep it going. The loop header is misleading — it reads as if the body repeats. Either make the exit conditional, or drop the loop and write the single pass directly.

The following code illustrates the pattern detected by this rule:

extern int check(int i);
extern void do_work(void);
int unconditional_return(void)
{
    // FLAGGED: Loop body always returns on the first iteration
    while (check(0)) {
        do_work();
        return 1;
    }
    return 0;
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved