Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: grammar fixes

...

Code Block
bgColor#FFcccc
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
bgColor#ccccff
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
bgColor#ccccff
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.

 

ISO/IEC TR 24772:2010

"Likely Incorrect Expression [KOA]"

MITRE CWE

CWE ID 480, "Use of Incorrect Operator"

...