if statement with an empty body

ID

apex.correctness.empty_if_body

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Dead Code

Language

Apex

Description

Reports an if whose then-branch contains no statements.

Rationale

An empty branch evaluates a condition and does nothing with the answer, which is never what the author meant. It is normally one of two mistakes: the body was moved out during a refactor and the if was left behind, or the logic was inverted and the real work now sits in an else that is missing. Either way the condition is still evaluated on every record, so the cost is paid and nothing is gained.

The following code illustrates the pattern detected by this rule:

public class EmptyIfs {

    public void run(Account a, Boolean flag) {
        // FLAGGED: if statement with an empty body
        if (a.Name == null) {
        }

        // FLAGGED: if statement with an empty body
        if (flag) { }

        if (a.Name == null) {
            a.addError(Label.Account_Name_Required);
        }

        if (!flag) {
            System.debug('off');
        }
    }
}

Remediation

Delete the if if the condition no longer matters, fill the branch if the work was lost, or invert the condition and drop the empty side if the intent was to act on the other case.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved