Added (back) support for enums to CheckCondition::checkIncorrectLogicOperator() (#7794)

This commit is contained in:
PKEuS 2017-03-23 18:57:48 +01:00
parent 996501a449
commit 9225bff31d
2 changed files with 26 additions and 8 deletions

View File

@ -618,22 +618,30 @@ static bool parseComparison(const Token *comp, bool *not1, std::string *op, std:
if (!comp)
return false;
if (!comp->isComparisonOp() || !comp->astOperand1() || !comp->astOperand2()) {
const Token* op1 = comp->astOperand1();
const Token* op2 = comp->astOperand2();
if (!comp->isComparisonOp() || !op1 || !op2) {
*op = "!=";
*value = "0";
*expr = comp;
} else if (comp->astOperand1()->isLiteral()) {
if (comp->astOperand1()->isExpandedMacro())
} else if (op1->isLiteral()) {
if (op1->isExpandedMacro())
return false;
*op = invertOperatorForOperandSwap(comp->str());
*value = comp->astOperand1()->str();
*expr = comp->astOperand2();
if (op1->enumerator() && op1->enumerator()->value_known)
*value = MathLib::toString(op1->enumerator()->value);
else
*value = op1->str();
*expr = op2;
} else if (comp->astOperand2()->isLiteral()) {
if (comp->astOperand2()->isExpandedMacro())
if (op2->isExpandedMacro())
return false;
*op = comp->str();
*value = comp->astOperand2()->str();
*expr = comp->astOperand1();
if (op2->enumerator() && op2->enumerator()->value_known)
*value = MathLib::toString(op2->enumerator()->value);
else
*value = op2->str();
*expr = op1;
} else {
*op = "!=";
*value = "0";

View File

@ -62,6 +62,7 @@ private:
TEST_CASE(incorrectLogicOperator7); // opposite expressions: (expr || !expr)
TEST_CASE(incorrectLogicOperator8); // !
TEST_CASE(incorrectLogicOperator9);
TEST_CASE(incorrectLogicOperator10); // enum
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
TEST_CASE(incorrectLogicOp_condSwapping);
TEST_CASE(testBug5895);
@ -1123,6 +1124,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void incorrectLogicOperator10() { // #7794 - enum
check("typedef enum { A, B } Type_t;\n"
"void f(Type_t t) {\n"
" if ((t == A) && (t == B))\n"
" {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Logical conjunction always evaluates to false: t == 0 && t == 1.\n", errout.str());
}
void secondAlwaysTrueFalseWhenFirstTrueError() {
check("void f(int x) {\n"
" if (x > 5 && x != 1)\n"