From cf4d59a835b48def7c9bb24ca5f6fc0a1c69561a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 14 May 2023 12:16:17 +0200 Subject: [PATCH] Partial fix for #9602 False positives with function pointers (#5042) * Partial fix for #9602 False positives with function pointers * Add test for #9222 --- lib/checkunusedfunctions.cpp | 2 +- test/testunusedfunctions.cpp | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 83dd5dd51..39be044cd 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -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(); diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 0b147059c..0eed7a68f 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -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\n" + void template9() { + check("template\n" // #7739 "void f(T const& t) {}\n" "template\n" "void g(T const& t) {\n" @@ -328,6 +347,12 @@ private: " g(3.14);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("template T f(T);\n" // #9222 + "template T f(T i) { return i; }\n" + "template int f(int);\n" + "int main() { return f(int(2)); }\n"); + ASSERT_EQUALS("", errout.str()); } void throwIsNotAFunction() {