Invalid use of = operator
Assignment in conditional statement
Description
This defect occurs when an
assignment is made inside the predicate of a conditional, such as if
or while
.
In C and C++, a single equal sign is an assignment not a comparison. Using a single equal sign in a conditional statement can indicate a typo or a mistake.
Polyspace® does not report an Invalid use of =
(assignment) operator when the object being assigned
is declared in the same statement, such as if statements with initializer. For example, in this
code snippet, the variable tol
is declared and
assigned the return value of the function
tolerance
in the if
statement, but Polyspace does not report the use of the
=
operator as a
violation.
#include <iostream> #include <stdexcept> #include <string> extern float tolerance(float actual, float expected); enum STATUS { FAIL, PASS }; STATUS func(float val, float size) { if (auto tol = tolerance(val, size) < 0.01f) /* No defect. Equivalent to auto tol = tolerance(val, size); if( tol < 0.01f) */ { return PASS; } else { std::string errorMsg = "Tolerance exceeded by " + std::to_string(tol - 0.01f); throw std::runtime_error(errorMsg); } }
Risk
Conditional statement tests the wrong values— The single equal sign operation assigns the value of the right operand to the left operand. Then, because this assignment is inside the predicate of a conditional, the program checks whether the new value of the left operand is nonzero or not NULL.
Maintenance and readability issues — Even if the assignment is intended, someone reading or updating the code can misinterpret the assignment as an equality comparison instead of an assignment.
Fix
If the assignment is a bug, to check for equality, add a second equal sign (
==
).If the assignment inside the conditional statement was intentional, to improve readability, separate the assignment and the test. Move the assignment outside the control statement. In the control statement, simply test the result of the assignment.
If you do not want to fix the issue, 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_USE |
Impact: Medium |
Version History
Introduced in R2013b
See Also
Find defects (-checkers)
| Invalid use of == (equality) 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)