CodeQL documentation

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

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy