Skip to content

Commit a97e284

Browse files
author
Daniel Marjamäki
committed
Fixed cppcheck-opensource#2407 (False positive: unused private function)
1 parent f72fd69 commit a97e284

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎lib/checkclass.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ void CheckClass::privateFunctions()
199199
if (Token::findmatch(_tokenizer->tokens(), "; __property ;"))
200200
return;
201201

202+
// #2407 calls from operator() is not detected
203+
// TODO: Don't bailout. Detect the call.
204+
if (Token::findmatch(_tokenizer->tokens(), "operator ( )"))
205+
return;
206+
202207
createSymbolDatabase();
203208

204209
std::list<SymbolDatabase::SpaceInfo *>::iterator i;

‎test/testunusedprivfunc.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class TestUnusedPrivateFunction : public TestFixture
5757

5858
// No false positives when there are "unused" templates that are removed in the simplified token list
5959
TEST_CASE(template1);
60+
61+
// #2407 - FP when called from operator()
62+
TEST_CASE(fp_operator);
6063
}
6164

6265

@@ -414,6 +417,36 @@ class TestUnusedPrivateFunction : public TestFixture
414417
"};\n");
415418
ASSERT_EQUALS("", errout.str());
416419
}
420+
421+
void fp_operator()
422+
{
423+
// #2407 - FP when function is called from operator()
424+
check("class Fred\n"
425+
"{\n"
426+
"public:\n"
427+
" void operator()(int x) {\n"
428+
" startListening();\n"
429+
" }\n"
430+
"\n"
431+
"private:\n"
432+
" void startListening() {\n"
433+
" }\n"
434+
"};\n");
435+
ASSERT_EQUALS("", errout.str());
436+
437+
check("class Fred\n"
438+
"{\n"
439+
"public:\n"
440+
" void operator()(int x) {\n"
441+
" }\n"
442+
"\n"
443+
"private:\n"
444+
" void startListening() {\n"
445+
" }\n"
446+
"};\n");
447+
TODO_ASSERT_EQUALS("[test.cpp:8]: (style) Unused private function 'Fred::startListening'\n", errout.str());
448+
ASSERT_EQUALS("", errout.str());
449+
}
417450
};
418451

419452
REGISTER_TEST(TestUnusedPrivateFunction)

0 commit comments

Comments
 (0)