diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 660735f6e..8ac210ce5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1996,14 +1996,19 @@ const std::list & CheckClass::callsPureVirtualFunction(const Func for (const Token *tok = function.arg->link(); tok && tok != function.functionScope->classEnd; tok = tok->next()) { - if ((Token::simpleMatch(tok,") {") && - tok->link() && - Token::Match(tok->link()->previous(),"if|switch")) || - Token::simpleMatch(tok,"else {") - ) { - // Assume pure virtual function call is prevented by "if|else|switch" condition - tok = tok->linkAt(1); - continue; + if (function.type != Function::eConstructor && + function.type != Function::eCopyConstructor && + function.type != Function::eMoveConstructor && + function.type != Function::eDestructor) { + if ((Token::simpleMatch(tok,") {") && + tok->link() && + Token::Match(tok->link()->previous(),"if|switch")) || + Token::simpleMatch(tok,"else {") + ) { + // Assume pure virtual function call is prevented by "if|else|switch" condition + tok = tok->linkAt(1); + continue; + } } const Function * callFunction=tok->function(); if (!callFunction || diff --git a/test/testclass.cpp b/test/testclass.cpp index 0e5a21c22..206a140fe 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -5679,6 +5679,26 @@ private: "A::~A()\n" "{nonpure();}\n"); ASSERT_EQUALS("[test.cpp:10] -> [test.cpp:5] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in destructor.\n", errout.str()); + + checkPureVirtualFunctionCall("class A\n" + "{\n" + " virtual void pure()=0;\n" + " A(bool b);\n" + "};\n" + "A::A(bool b)\n" + "{if (b) pure();}\n"); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str()); + + checkPureVirtualFunctionCall("class A\n" + " {\n" + " virtual void pure()=0; \n" + " virtual ~A(); \n" + " int m; \n" + "};\n" + "A::~A()\n" + "{if (b) pure();}\n"); + ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in destructor.\n", errout.str()); + } void pureVirtualFunctionCallOtherClass() {