Redundant Expression Checker is updated to be able to match redundant expressions that contain binary overloaded operators.
Two basic cases are checked:
1.) Operators that take two instances of the same class/struct as arguments.
Example:
struct MyStruct { int x; bool operator==(const MyStruct& rhs) const {return this->x == rhs.x; } }; void testOperators(){ MyStruct S; if(S == S) return; // Sides are equivalent. }
2.) Overloaded operators that compare an instance of a class/struct with an integer constant expression.
Example:
struct S { S(){x = 1;} int x; bool operator==(const int &i) const {return x == i;} bool operator!=(int i) const {return x != i;} }; void testOperators(){ S s; if (s == 1 && s != 1) return; // Expression is always false. }
It is checked, whether the arguments can be modified by the operator, filtering some of the dangerous side-effects.
Limitations:
- The checker is only able to recognize comparison ( ==, !=, >, <, >=, <= ) operators, and does not check for any other operator kind. This is because these operators are considered to be implemented to behave properly and conventionally in most cases.
- However, it is not checked whether the overloaded functions actually compare their arguments using the corresponding binary operator, or behave completely different from how one expects based on their name.
Unnecessary empty line