...
Code Block | ||
---|---|---|
| ||
public void f(boolean a, boolean b) { if (a = b) { /* ... */ } } |
Although the programmer's intent of the code could be have been to assign b
to a
and test the value of the result, it is frequently a case of this usage frequently occurs when the programmer mistakenly using used the assignment operator =
instead of rather than the equals equality operator ==
.
Compliant Solution
...
When the assignment is intended, the following compliant solution may be used because clarifies the programmer's intent is clearer:
Code Block | ||
---|---|---|
| ||
public void f(boolean a, boolean b) { if ((a = b) == true) { /* ... */ } } |
But Nevertheless, it might may be preferable to express this same logic as an assignment followed by a conditional:
...
In this noncompliant code example, an assignment expression is appears in the controlling expression , as an operand of the &&
operator. Since Because &&
is not a comparison operator, assignment is an illegal operand.
...
Code Block | ||
---|---|---|
| ||
public void f(boolean a, boolean b) { while ( (a == b) && flag ) { /* ... */ } } |
When Use one of the above solutions when the assignment is intended, one of the same solutions should be used as shown above.
"Likely Incorrect Expression [KOA]" | |
CWE ID 480, "Use of Incorrect Operator" |
...