An equality operator should be used instead of an assignment operator in a conditional statement

  • BAD_ASSIGN_IN_CONDITION
  • Error
  • Medium
  • cwe

This rule applies when an assignment operator is unintentionally used in a conditional statement instead of an equality operator.

Note: There are cases where an assignment operator is intentionally used in a conditional statement to simplify an assignment and a truthy value check. In order not to detect these intentional cases as alarms, this rule only applies when the RHS of the assignment is evaluated to a constant value.

Noncompliant Code Example

View with compliant examples side by side
x = str.indexOf(y);
if (x = -1) return "no"; // BAD_ASSIGN_IN_CONDITION alarm. The true branch is always taken and "no" is returned.
else return "yes";

Compliant Code Example

View with noncompliant examples side by side
x = str.indexOf(y);
if (x == -1) return "no";
else return "yes";

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

Was this documentation helpful?