longyiqi/oclint — Issue #7: Detecting constant results for variable comparisons for constant if statement rule
Author: lqi
Created: 2011-11-04 15:29:18+00:00
State: open

We need to detect the results for variable comparisons, like result of comparison 1 == 2 always be false. We should treat it as a code smell in constant if statement rule. To achieve this, we have some test cases.

void ConstantIfStatementRuleTest::testCompareWithTwoConstantVariables() {
checkRule("int main() { int a = 1, b = 2; if (a == b) {;} return 0; }", true);
}

void ConstantIfStatementRuleTest::testSamePointerAlwaysContant() {
string sourceCode = "int main() { int *a, *b; int i = 1; a = &i; b = &i;\
if (a == b) {;} return 0; }";
checkRule(sourceCode, true);
}

void ConstantIfStatementRuleTest::testSameValueDifferentPointerComparedByValueIsConstant() {
string sourceCode = "int main() { int *a, *b; int i = 1; a = &i; b = &i;\
if (*a = *b) {;} return 0; }";
checkRule(sourceCode, true);
}