Invalid use of == operator
Equality operation in assignment statement
Description
This defect occurs when you use an equality operator instead of an assignment operator in a simple statement.
Risk
The use of ==
operator instead of an =
operator can silently produce incorrect results. If you intended to assign a value
to a variable, the assignment does not occur. The variable retains its previous
value or if not initialized previously, stays uninitialized.
Fix
Use the =
(assignment) operator instead of the
==
(equality) operator.
The check appears on chained assignment and equality operators such as:
compFlag = val1 == val2;
compFlag = (val1 == val2);
If the use of
==
operator is intended, add comments to
your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax: BAD_EQUAL_EQUAL_USE |
Impact: High |
Version History
Introduced in R2013b
See Also
Find defects (-checkers)
| Invalid use of = (assignment) operator
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)