CXX.POSSIBLE_COPY_PASTE.LOGICAL_OP.CMP_SAME_OBJECT
Possible copy-paste error: An object should not be logically compared to itself.
This checker reports defects when the same objects are compared by using a logical operator.
Vulnerable code example
Copy
int f1() {
bool isAFruit = false;
const int ORANGE = 8;
if (ORANGE == ORANGE)
isAFruit = true;
return 0;
}
In the above example, line 5 is noncompliant as an int object ORANGE is compared with itself. Klocwork reports a CXX.POSSIBLE_COPY_PASTE.LOGICAL_OP.CMP_SAME_OBJECT defect at line 5 with a message, indicating, "Possible copy-paste error: An object should not be logically compared to itself."
Fixed code example
Copy
int f1() {
bool isAFruit = false;
const int ORANGE = 8;
if (ORANGE == 8)
isAFruit = true;
return 0;
}
In the fixed example, Klocwork no longer reports the defect CXX.POSSIBLE_COPY_PASTE.LOGICAL_OP.CMP_SAME_OBJECT at line 5.