Fixed #1537 ('Unused private function' when using pointer to member function)

This commit is contained in:
Daniel Marjamäki 2010-04-08 19:06:54 +02:00
parent 26c447279c
commit 1b81a9d435
2 changed files with 39 additions and 3 deletions

View File

@ -704,7 +704,7 @@ void CheckClass::privateFunctions()
} }
// Check member class functions to see what functions are used.. // Check member class functions to see what functions are used..
if ((inclass && indent_level == 1 && Token::Match(ftok, ") const| {")) || if ((inclass && indent_level == 1 && Token::Match(ftok, "%var% (")) ||
(Token::Match(ftok, (classname + " :: ~| %var% (").c_str()))) (Token::Match(ftok, (classname + " :: ~| %var% (").c_str())))
{ {
while (ftok && ftok->str() != ")") while (ftok && ftok->str() != ")")
@ -714,8 +714,22 @@ void CheckClass::privateFunctions()
if (Token::Match(ftok, ") : %var% (")) if (Token::Match(ftok, ") : %var% ("))
{ {
while (!Token::Match(ftok->next(), "[{};]")) while (!Token::Match(ftok->next(), "[{};]"))
{
if (Token::Match(ftok, "::|,|( %var% ,|)"))
{
// Remove function from FuncList
std::list<const Token *>::iterator it = FuncList.begin();
while (it != FuncList.end())
{
if (ftok->next()->str() == (*it)->str())
FuncList.erase(it++);
else
it++;
}
}
ftok = ftok->next(); ftok = ftok->next();
} }
}
if (!Token::Match(ftok, ") const| {")) if (!Token::Match(ftok, ") const| {"))
continue; continue;

View File

@ -41,7 +41,8 @@ private:
TEST_CASE(test5); TEST_CASE(test5);
// [ 2236547 ] False positive --style unused function, called via pointer // [ 2236547 ] False positive --style unused function, called via pointer
TEST_CASE(func_pointer); TEST_CASE(func_pointer1);
TEST_CASE(func_pointer2);
TEST_CASE(ctor); TEST_CASE(ctor);
@ -208,7 +209,7 @@ private:
void func_pointer() void func_pointer1()
{ {
check("class Fred\n" check("class Fred\n"
"{\n" "{\n"
@ -234,6 +235,27 @@ private:
} }
void func_pointer2()
{
check("class UnusedPrivateFunctionMemberPointer\n"
"{\n"
"public:\n"
" UnusedPrivateFunctionMemberPointer()\n"
" : mObserver(this, &UnusedPrivateFunctionMemberPointer::callback)\n"
" {}\n"
"\n"
"private:\n"
" void callback(const& unsigned) const {}\n"
"\n"
" Observer<UnusedPrivateFunctionMemberPointer, unsigned> mObserver;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void ctor() void ctor()
{ {
check("class PrivateCtor\n" check("class PrivateCtor\n"