Added (back) support for enums to CheckCondition::checkIncorrectLogicOperator() (#7794)
This commit is contained in:
parent
996501a449
commit
9225bff31d
|
@ -618,22 +618,30 @@ static bool parseComparison(const Token *comp, bool *not1, std::string *op, std:
|
||||||
if (!comp)
|
if (!comp)
|
||||||
return false;
|
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 = "!=";
|
*op = "!=";
|
||||||
*value = "0";
|
*value = "0";
|
||||||
*expr = comp;
|
*expr = comp;
|
||||||
} else if (comp->astOperand1()->isLiteral()) {
|
} else if (op1->isLiteral()) {
|
||||||
if (comp->astOperand1()->isExpandedMacro())
|
if (op1->isExpandedMacro())
|
||||||
return false;
|
return false;
|
||||||
*op = invertOperatorForOperandSwap(comp->str());
|
*op = invertOperatorForOperandSwap(comp->str());
|
||||||
*value = comp->astOperand1()->str();
|
if (op1->enumerator() && op1->enumerator()->value_known)
|
||||||
*expr = comp->astOperand2();
|
*value = MathLib::toString(op1->enumerator()->value);
|
||||||
|
else
|
||||||
|
*value = op1->str();
|
||||||
|
*expr = op2;
|
||||||
} else if (comp->astOperand2()->isLiteral()) {
|
} else if (comp->astOperand2()->isLiteral()) {
|
||||||
if (comp->astOperand2()->isExpandedMacro())
|
if (op2->isExpandedMacro())
|
||||||
return false;
|
return false;
|
||||||
*op = comp->str();
|
*op = comp->str();
|
||||||
*value = comp->astOperand2()->str();
|
if (op2->enumerator() && op2->enumerator()->value_known)
|
||||||
*expr = comp->astOperand1();
|
*value = MathLib::toString(op2->enumerator()->value);
|
||||||
|
else
|
||||||
|
*value = op2->str();
|
||||||
|
*expr = op1;
|
||||||
} else {
|
} else {
|
||||||
*op = "!=";
|
*op = "!=";
|
||||||
*value = "0";
|
*value = "0";
|
||||||
|
|
|
@ -62,6 +62,7 @@ private:
|
||||||
TEST_CASE(incorrectLogicOperator7); // opposite expressions: (expr || !expr)
|
TEST_CASE(incorrectLogicOperator7); // opposite expressions: (expr || !expr)
|
||||||
TEST_CASE(incorrectLogicOperator8); // !
|
TEST_CASE(incorrectLogicOperator8); // !
|
||||||
TEST_CASE(incorrectLogicOperator9);
|
TEST_CASE(incorrectLogicOperator9);
|
||||||
|
TEST_CASE(incorrectLogicOperator10); // enum
|
||||||
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
|
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
|
||||||
TEST_CASE(incorrectLogicOp_condSwapping);
|
TEST_CASE(incorrectLogicOp_condSwapping);
|
||||||
TEST_CASE(testBug5895);
|
TEST_CASE(testBug5895);
|
||||||
|
@ -1123,6 +1124,15 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
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() {
|
void secondAlwaysTrueFalseWhenFirstTrueError() {
|
||||||
check("void f(int x) {\n"
|
check("void f(int x) {\n"
|
||||||
" if (x > 5 && x != 1)\n"
|
" if (x > 5 && x != 1)\n"
|
||||||
|
|
Loading…
Reference in New Issue