Fix #10650 FN knownConditionTrueFalse with const int value (#4078)

This commit is contained in:
chrchr-github 2022-05-04 14:25:00 +02:00 committed by GitHub
parent 767b12b6c2
commit e2069dd1b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -1148,11 +1148,19 @@ static inline bool isDifferentKnownValues(const Token * const tok1, const Token
});
}
static inline bool isSameConstantValue(bool macro, const Token * const tok1, const Token * const tok2)
static inline bool isSameConstantValue(bool macro, const Token* tok1, const Token* tok2)
{
if (tok1 == nullptr || tok2 == nullptr)
return false;
auto adjustForCast = [](const Token* tok) {
if (Token::Match(tok->previous(), "%type% (|{") && tok->previous()->isStandardType() && tok->astOperand2())
return tok->astOperand2();
return tok;
};
tok1 = adjustForCast(tok1);
tok2 = adjustForCast(tok2);
if (!tok1->isNumber() || !tok2->isNumber())
return false;

View File

@ -159,6 +159,7 @@ private:
TEST_CASE(duplicateExpression12); // #10026
TEST_CASE(duplicateExpression13); // #7899
TEST_CASE(duplicateExpression14); // #9871
TEST_CASE(duplicateExpression15); // #10650
TEST_CASE(duplicateExpressionLoop);
TEST_CASE(duplicateValueTernary);
TEST_CASE(duplicateExpressionTernary); // #6391
@ -5757,6 +5758,20 @@ private:
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4] -> [test.cpp:5]: (style) The comparison 'f+4 != g+4' is always false because 'f+4' and 'g+4' represent the same value.\n", errout.str());
}
void duplicateExpression15() { //#10650
check("bool f() {\n"
" const int i = int(0);\n"
" return i == 0;\n"
"}\n"
"bool g() {\n"
" const int i = int{ 0 };\n"
" return i == 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i == 0' is always true.\n"
"[test.cpp:6] -> [test.cpp:7]: (style) The comparison 'i == 0' is always true.\n",
errout.str());
}
void duplicateExpressionLoop() {
check("void f() {\n"
" int a = 1;\n"