Fix #12044 FN knownConditionTrueFalse comparing qualified constant with number (#5518)

This commit is contained in:
chrchr-github 2023-10-23 11:18:05 +02:00 committed by GitHub
parent a765a1310d
commit 7e2fcea3d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -1506,8 +1506,12 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
if (cpp) { if (cpp) {
if (tok1->str() == "." && tok1->astOperand1() && tok1->astOperand1()->str() == "this") if (tok1->str() == "." && tok1->astOperand1() && tok1->astOperand1()->str() == "this")
tok1 = tok1->astOperand2(); tok1 = tok1->astOperand2();
while (Token::simpleMatch(tok1, "::") && tok1->astOperand2())
tok1 = tok1->astOperand2();
if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this") if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this")
tok2 = tok2->astOperand2(); tok2 = tok2->astOperand2();
while (Token::simpleMatch(tok2, "::") && tok2->astOperand2())
tok2 = tok2->astOperand2();
} }
// Skip double not // Skip double not
if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=") && astIsBoolLike(tok2)) { if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=") && astIsBoolLike(tok2)) {

View File

@ -6875,6 +6875,31 @@ private:
"[test.cpp:4]: (style) The comparison 'E0 > 0' is always false.\n" "[test.cpp:4]: (style) The comparison 'E0 > 0' is always false.\n"
"[test.cpp:5]: (style) The comparison 'E0 == 0' is always true.\n", "[test.cpp:5]: (style) The comparison 'E0 == 0' is always true.\n",
errout.str()); errout.str());
check("struct S {\n" // #12040, #12044
" static const int I = 0;\n"
" enum { E0 };\n"
" enum F { F0 };\n"
" void f() {\n"
" if (0 > I) {}\n"
" if (0 > S::I) {}\n"
" if (0 > E0) {}\n"
" if (0 > S::E0) {}\n"
" }\n"
"};\n"
"void g() {\n"
" if (0 > S::I) {}\n"
" if (0 > S::E0) {}\n"
" if (0 > S::F::F0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:6]: (style) The comparison '0 > I' is always false.\n"
"[test.cpp:2] -> [test.cpp:7]: (style) The comparison '0 > S::I' is always false.\n"
"[test.cpp:8]: (style) The comparison '0 > E0' is always false.\n"
"[test.cpp:9]: (style) The comparison '0 > S::E0' is always false.\n"
"[test.cpp:2] -> [test.cpp:13]: (style) The comparison '0 > S::I' is always false.\n"
"[test.cpp:14]: (style) The comparison '0 > S::E0' is always false.\n"
"[test.cpp:15]: (style) The comparison '0 > S::F::F0' is always false.\n",
errout.str());
} }
void duplicateExpressionLoop() { void duplicateExpressionLoop() {