Skip to main content

IAR Embedded Workbench for RL78 5.20

EXP-cond-assign

In this section:
Synopsis

An assignment might be mistakenly used as the condition for an if, for, while, or do statement.

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

An assignment might be mistakenly used as the condition for an if, for, while, or do statement. This condition will either always or never hold, depending on the value of the second operand. This was most likely intended to be a comparison, not an assignment. This might cause incorrect program flow, and possibly an infinite loop. This check is identical to MISRAC2012-Rule-13.4_a.

Coding standards
CERT EXP18-C

Do not perform assignments in selection statements

CERT EXP19-CPP

Do not perform assignments in conditional expressions

CWE 481

Assigning instead of Comparing

MISRA C:2012 Rule-13.4

(Advisory) The result of an assignment operator should not be used

Code examples

The following code example fails the check and will give a warning:

int example(void) {
  int x = 2;
  if (x = 3)
    return 1;
  return 0;
}

The following code example passes the check and will not give a warning about this issue:

int example(void) {
  int x = 2;
  if (x == 3)
    return 1;
  return 0;
}