Do not use the assignment operator in conditional expressions because it typically frequently indicates programmer error and can result in unexpected behavior. This means that the assignment operator should not be used in the following contexts:
...
In this noncompliant code example, an assignment expression is in the controlling expression is used with , as an operand of the &&
operator. Since &&
is not a comparison operator, assignment is an illegal operand.
Code Block | ||
---|---|---|
| ||
public void f(boolean a, boolean b, boolean flag) { while ( (a = b) && flag ) { /* ... */ } } |
...
When the assignment is intended, one of the same solutions should be used as shown above.
Applicability
Errors of omission can result in unintended program flow.
"Likely Incorrect Expression [KOA]" | |
CWE ID 480, "Use of Incorrect Operator" |
...