Fix FP virtualCallInConstructor with function pointer (#3913)

This commit is contained in:
chrchr-github 2022-03-19 20:00:06 +01:00 committed by GitHub
parent 9d524616b3
commit 2baf8f6cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -2561,7 +2561,8 @@ const std::list<const Token *> & CheckClass::getVirtualFunctionCalls(const Funct
const Function * callFunction = tok->function();
if (!callFunction ||
function.nestedIn != callFunction->nestedIn ||
(tok->previous() && tok->previous()->str() == "."))
Token::simpleMatch(tok->previous(), ".") ||
!(tok->astParent() && (tok->astParent()->str() == "(" || (tok->astParent()->str() == "::" && Token::simpleMatch(tok->astParent()->astParent(), "(")))))
continue;
if (tok->previous() &&

View File

@ -7286,6 +7286,24 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:9]: (style) Virtual function 'Copy' is called from copy constructor 'Derived(const Derived&Src)' at line 13. Dynamic binding is not used.\n",
errout.str());
checkVirtualFunctionCall("struct B {\n"
" B() { auto pf = &f; }\n"
" virtual void f() {}\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkVirtualFunctionCall("struct B {\n"
" B() { auto pf = &B::f; }\n"
" virtual void f() {}\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkVirtualFunctionCall("struct B {\n"
" B() { (f)(); }\n"
" virtual void f() {}\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Virtual function 'f' is called from constructor 'B()' at line 2. Dynamic binding is not used.\n", errout.str());
}
void pureVirtualFunctionCall() {
@ -7308,6 +7326,17 @@ private:
"{}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str());
checkVirtualFunctionCall("namespace N {\n"
" class A\n"
" {\n"
" virtual int pure() = 0;\n"
" A();\n"
" int m;\n"
" };\n"
"}\n"
"N::A::A() : m(N::A::pure()) {}\n");
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:4]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str());
checkVirtualFunctionCall("class A\n"
" {\n"
" virtual void pure()=0;\n"