Unused private function: return pointer to private function

This commit is contained in:
Daniel Marjamäki 2008-11-11 20:19:28 +00:00
parent 0c910c9775
commit 58f0f03449
2 changed files with 41 additions and 1 deletions

View File

@ -511,6 +511,8 @@ void CheckClass::CheckUnusedPrivateFunctions()
const char *_pattern[] = {"=","",NULL}; const char *_pattern[] = {"=","",NULL};
_pattern[1] = FuncList.front().c_str(); _pattern[1] = FuncList.front().c_str();
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL); fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
_pattern[0] = "return";
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
_pattern[0] = "("; _pattern[0] = "(";
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL); fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
_pattern[0] = ")"; _pattern[0] = ")";

View File

@ -36,6 +36,9 @@ private:
void run() void run()
{ {
TEST_CASE( test1 ); TEST_CASE( test1 );
// [ 2236547 ] False positive --style unused function, called via pointer
TEST_CASE( func_pointer );
} }
@ -69,7 +72,7 @@ private:
" Fred();\n" " Fred();\n"
"};\n" "};\n"
"\n" "\n"
"unsigned int Fred::Fred()\n" "Fred::Fred()\n"
"{ }\n" "{ }\n"
"\n" "\n"
"unsigned int Fred::f()\n" "unsigned int Fred::f()\n"
@ -77,6 +80,41 @@ private:
ASSERT_EQUALS( std::string("Class 'Fred', unused private function: 'f'\n"), errout.str() ); ASSERT_EQUALS( std::string("Class 'Fred', unused private function: 'f'\n"), errout.str() );
} }
void func_pointer()
{
check( "class Fred\n"
"{\n"
"private:\n"
" typedef void (*testfp)();\n"
"\n"
" testfp get()\n"
" {\n"
" return test;\n"
" }\n"
"\n"
" static void test()\n"
" { }\n"
"\n"
"public:\n"
" Fred();\n"
"};\n"
"\n"
"Fred::Fred()\n"
"{}\n" );
std::string str( errout.str() );
ASSERT_EQUALS( std::string("Class 'Fred', unused private function: 'get'\n"), str );
}
}; };
REGISTER_TEST( TestUnusedPrivateFunction ) REGISTER_TEST( TestUnusedPrivateFunction )