Skip to content

Commit cf4d59a

Browse files
Partial fix for #9602 False positives with function pointers (danmar#5042)
* Partial fix for #9602 False positives with function pointers * Add test for #9222
1 parent 096d3a7 commit cf4d59a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/checkunusedfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
223223
funcname = tok->next();
224224
while (Token::Match(funcname, "%name% :: %name%"))
225225
funcname = funcname->tokAt(2);
226-
} else if (tok->scope()->type != Scope::ScopeType::eEnum && Token::Match(tok, "[;{}.,()[=+-/|!?:]")) {
226+
} else if (tok->scope()->type != Scope::ScopeType::eEnum && (Token::Match(tok, "[;{}.,()[=+-/|!?:]") || Token::Match(tok, "return|throw"))) {
227227
funcname = tok->next();
228228
if (funcname && funcname->str() == "&")
229229
funcname = funcname->next();

test/testunusedfunctions.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class TestUnusedFunctions : public TestFixture {
3737
TEST_CASE(incondition);
3838
TEST_CASE(return1);
3939
TEST_CASE(return2);
40+
TEST_CASE(return3);
4041
TEST_CASE(callback1);
4142
TEST_CASE(callback2);
4243
TEST_CASE(else1);
@@ -124,6 +125,24 @@ class TestUnusedFunctions : public TestFixture {
124125
ASSERT_EQUALS("", errout.str());
125126
}
126127

128+
void return3() {
129+
check("typedef void (*VoidFunc)();\n" // #9602
130+
"void sayHello() {\n"
131+
" printf(\"Hello World\\n\");\n"
132+
"}\n"
133+
"VoidFunc getEventHandler() {\n"
134+
" return sayHello;\n"
135+
"}\n"
136+
"void indirectHello() {\n"
137+
" VoidFunc handler = getEventHandler();\n"
138+
" handler();\n"
139+
"}\n"
140+
"int main() {\n"
141+
" indirectHello();\n"
142+
"}");
143+
ASSERT_EQUALS("", errout.str());
144+
}
145+
127146
void callback1() {
128147
check("void f1()\n"
129148
"{\n"
@@ -314,8 +333,8 @@ class TestUnusedFunctions : public TestFixture {
314333
ASSERT_EQUALS("", errout.str());
315334
}
316335

317-
void template9() { // #7739
318-
check("template<class T>\n"
336+
void template9() {
337+
check("template<class T>\n" // #7739
319338
"void f(T const& t) {}\n"
320339
"template<class T>\n"
321340
"void g(T const& t) {\n"
@@ -328,6 +347,12 @@ class TestUnusedFunctions : public TestFixture {
328347
" g(3.14);\n"
329348
"}\n");
330349
ASSERT_EQUALS("", errout.str());
350+
351+
check("template <typename T> T f(T);\n" // #9222
352+
"template <typename T> T f(T i) { return i; }\n"
353+
"template int f<int>(int);\n"
354+
"int main() { return f(int(2)); }\n");
355+
ASSERT_EQUALS("", errout.str());
331356
}
332357

333358
void throwIsNotAFunction() {

0 commit comments

Comments
 (0)