Partial fix for #9602 False positives with function pointers (#5042)

* Partial fix for #9602 False positives with function pointers

* Add test for #9222
This commit is contained in:
chrchr-github 2023-05-14 12:16:17 +02:00 committed by GitHub
parent 096d3a78b0
commit cf4d59a835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -223,7 +223,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
funcname = tok->next();
while (Token::Match(funcname, "%name% :: %name%"))
funcname = funcname->tokAt(2);
} else if (tok->scope()->type != Scope::ScopeType::eEnum && Token::Match(tok, "[;{}.,()[=+-/|!?:]")) {
} else if (tok->scope()->type != Scope::ScopeType::eEnum && (Token::Match(tok, "[;{}.,()[=+-/|!?:]") || Token::Match(tok, "return|throw"))) {
funcname = tok->next();
if (funcname && funcname->str() == "&")
funcname = funcname->next();

View File

@ -37,6 +37,7 @@ private:
TEST_CASE(incondition);
TEST_CASE(return1);
TEST_CASE(return2);
TEST_CASE(return3);
TEST_CASE(callback1);
TEST_CASE(callback2);
TEST_CASE(else1);
@ -124,6 +125,24 @@ private:
ASSERT_EQUALS("", errout.str());
}
void return3() {
check("typedef void (*VoidFunc)();\n" // #9602
"void sayHello() {\n"
" printf(\"Hello World\\n\");\n"
"}\n"
"VoidFunc getEventHandler() {\n"
" return sayHello;\n"
"}\n"
"void indirectHello() {\n"
" VoidFunc handler = getEventHandler();\n"
" handler();\n"
"}\n"
"int main() {\n"
" indirectHello();\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void callback1() {
check("void f1()\n"
"{\n"
@ -314,8 +333,8 @@ private:
ASSERT_EQUALS("", errout.str());
}
void template9() { // #7739
check("template<class T>\n"
void template9() {
check("template<class T>\n" // #7739
"void f(T const& t) {}\n"
"template<class T>\n"
"void g(T const& t) {\n"
@ -328,6 +347,12 @@ private:
" g(3.14);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("template <typename T> T f(T);\n" // #9222
"template <typename T> T f(T i) { return i; }\n"
"template int f<int>(int);\n"
"int main() { return f(int(2)); }\n");
ASSERT_EQUALS("", errout.str());
}
void throwIsNotAFunction() {