Assignment in Conditional Expression

ID

java.assign_in_condition

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:481, reliability, suspicious-comparison

Description

Reports assignment expressions (=) found inside if conditions.

Loop conditions are not covered here. while and do-while belong to java.loop_assignment_in_while_condition, which knows about the canonical sentinel read loop and allows it by default. The two rules are complementary: one condition produces at most one finding, from whichever rule owns it.

Rationale

Using = instead of == in a conditional is one of the most common programming mistakes. Even when the assignment is intentional, mixing a side effect into the condition expression makes the code harder to review and more likely to be misread as a comparison.

// Bad - likely meant == instead of =
if (b = true) {
    doSomething();
}

Remediation

Replace the assignment with a comparison, or move the assignment to a separate statement before the condition:

// Good - comparison
if (b == true) {
    doSomething();
}

// Good - assignment separated from condition
int x = readNext();
if (x > 0) {
    process(x);
}

© 2026 Xygeni. All rights reserved