Comparison where assignment was intended¶
ID: cpp/compare-where-assign-meant
Kind: problem
Security severity:
Severity: error
Precision: high
Tags:
- reliability
- correctness
- external/cwe/cwe-482
Query suites:
- cpp-security-and-quality.qls
Click to see the query in the CodeQL repository
This rule finds uses of the equality operator ==
in places where the assignment operator =
would make more sense. This is a common mistake in C and C++, because of the similarity of the =
and the ==
operator, and the fact that expressions are valid as top-level statements.
The rule flags every occurrence of an equality operator in a position where its result is discarded.
Recommendation¶
Check to ensure that the flagged expressions are not typos. If the result of an equality test is really intended to be discarded, it should be explicitly cast to void
.
Example¶
int x;
x == 4; // most likely = was intended. Otherwise this statement has no effect.
...
References¶
Tutorialspoint - The C++ Programming Language: Operators in C++
Wikipedia: Operators in C and C++
Common Weakness Enumeration: CWE-482.