Fix #10618 FP knownConditionTrueFalse with virtual function (#4194)

* Fix #10618 FP knownConditionTrueFalse with virtual function

* Remove redundant check
This commit is contained in:
chrchr-github 2022-06-10 20:17:57 +02:00 committed by GitHub
parent 375880988c
commit 51c8630bb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -870,9 +870,9 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth)
// Abort recursion to avoid stack overflow // Abort recursion to avoid stack overflow
return true; return true;
++depth; ++depth;
// calling nonstatic method? // calling nonstatic method?
if (Token::Match(expr->previous(), "!!:: %name% (") && expr->function() && expr->function()->nestedIn && if (Token::Match(expr, "%name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct() && !expr->function()->isStatic()) {
expr->function()->nestedIn->isClassOrStruct()) {
// is it a method of this? // is it a method of this?
const Scope* fScope = expr->scope(); const Scope* fScope = expr->scope();
while (!fScope->functionOf && fScope->nestedIn) while (!fScope->functionOf && fScope->nestedIn)

View File

@ -4196,6 +4196,29 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (warning) Logical conjunction always evaluates to false: s.bar(1) == 0 && s.bar(1) > 0.\n", ASSERT_EQUALS("[test.cpp:3]: (warning) Logical conjunction always evaluates to false: s.bar(1) == 0 && s.bar(1) > 0.\n",
errout.str()); errout.str());
check("struct B {\n" // #10618
" void Modify();\n"
" static void Static();\n"
" virtual void CalledByModify();\n"
"};\n"
"struct D : B {\n"
" int i{};\n"
" void testV();\n"
" void testS();\n"
" void CalledByModify() override { i = 0; }\n"
"};\n"
"void D::testV() {\n"
" i = 1;\n"
" B::Modify();\n"
" if (i == 1) {}\n"
"}\n"
"void D::testS() {\n"
" i = 1;\n"
" B::Static();\n"
" if (i == 1) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:20]: (style) Condition 'i==1' is always true\n", errout.str());
} }
void alwaysTrueSymbolic() void alwaysTrueSymbolic()