Fixed #2480 (false positive on unused private function)

This commit is contained in:
Pete Johns 2011-01-19 11:58:44 +11:00
parent 856a631f35
commit 94aafa482c
2 changed files with 68 additions and 5 deletions

View File

@ -734,8 +734,11 @@ void CheckClass::privateFunctions()
// or if the function address is used somewhere...
// eg. sigc::mem_fun(this, &className::classFunction)
const std::string _pattern2("& " + classname + " :: " + FuncList.front()->str());
const std::string methodAsArgument("(|, " + classname + " :: " + FuncList.front()->str() + " ,|)");
if (!Token::findmatch(_tokenizer->tokens(), _pattern.c_str()) &&
!Token::findmatch(_tokenizer->tokens(), _pattern2.c_str()))
!Token::findmatch(_tokenizer->tokens(), _pattern2.c_str()) &&
!Token::findmatch(_tokenizer->tokens(), methodAsArgument.c_str())
)
{
unusedPrivateFunctionError(FuncList.front(), classname, FuncList.front()->str());
}

View File

@ -60,7 +60,9 @@ private:
// #2407 - FP when called from operator()
TEST_CASE(fp_operator);
TEST_CASE(testDoesNotIdentifyCallback); // #2480
TEST_CASE(testDoesNotIdentifyMethodAsFirstFunctionArgument); // #2480
TEST_CASE(testDoesNotIdentifyMethodAsMiddleFunctionArgument);
TEST_CASE(testDoesNotIdentifyMethodAsLastFunctionArgument);
}
@ -449,7 +451,7 @@ private:
ASSERT_EQUALS("", errout.str());
}
void testDoesNotIdentifyCallback()
void testDoesNotIdentifyMethodAsFirstFunctionArgument()
{
check("#include <iostream>"
"void callback(void (*func)(int), int arg)"
@ -474,8 +476,66 @@ private:
"{"
" MountOperation aExample(10);"
"}"
);
TODO_ASSERT_EQUALS("", errout.str());
);
ASSERT_EQUALS("", errout.str());
}
void testDoesNotIdentifyMethodAsMiddleFunctionArgument()
{
check("#include <iostream>"
"void callback(char, void (*func)(int), int arg)"
"{"
" (*func)(arg);"
"}"
"class MountOperation"
"{"
" static void Completed(int i);"
"public:"
" MountOperation(int i);"
"};"
"void MountOperation::Completed(int i)"
"{"
" std::cerr << i << std::endl;"
"}"
"MountOperation::MountOperation(int i)"
"{"
" callback('a', MountOperation::Completed, i);"
"}"
"int main(void)"
"{"
" MountOperation aExample(10);"
"}"
);
ASSERT_EQUALS("", errout.str());
}
void testDoesNotIdentifyMethodAsLastFunctionArgument()
{
check("#include <iostream>"
"void callback(int arg, void (*func)(int))"
"{"
" (*func)(arg);"
"}"
"class MountOperation"
"{"
" static void Completed(int i);"
"public:"
" MountOperation(int i);"
"};"
"void MountOperation::Completed(int i)"
"{"
" std::cerr << i << std::endl;"
"}"
"MountOperation::MountOperation(int i)"
"{"
" callback(i, MountOperation::Completed);"
"}"
"int main(void)"
"{"
" MountOperation aExample(10);"
"}"
);
ASSERT_EQUALS("", errout.str());
}
};