Skip to content

Commit

Permalink
Fix #8159 FN unusedFunction: sole recursive call
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jun 12, 2024
1 parent d33d29c commit 86837c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ static std::string stripTemplateParameters(const std::string& funcName) {
// FUNCTION USAGE - Check for unused functions etc
//---------------------------------------------------------------------------

static bool isRecursiveCall(const Token* ftok)
{
return ftok->function() && ftok->function() == Scope::nestedInFunction(ftok->scope());
}

void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Settings &settings)
{
const char * const FileName = tokenizer.list.getFiles().front().c_str();
Expand Down Expand Up @@ -251,7 +256,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Setting

if (func.filename.empty() || func.filename == "+" || func.filename != called_from_file)
func.usedOtherFile = true;
else
else if (!isRecursiveCall(funcname))
func.usedSameFile = true;

mFunctionCalls.insert(baseName);
Expand Down
21 changes: 15 additions & 6 deletions test/testunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TestUnusedFunctions : public TestFixture {
TEST_CASE(member_function_ternary);
TEST_CASE(boost);
TEST_CASE(enumValues);
TEST_CASE(recursive);

TEST_CASE(multipleFiles); // same function name in multiple files

Expand Down Expand Up @@ -116,23 +117,23 @@ class TestUnusedFunctions : public TestFixture {
" if (f1())\n"
" { }\n"
"}");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f1' is never used.\n", errout_str());
}

void return1() {
check("int f1()\n"
"{\n"
" return f1();\n"
"}");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f1' is never used.\n", errout_str());
}

void return2() {
check("char * foo()\n"
"{\n"
" return *foo();\n"
"}");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
}

void return3() {
Expand Down Expand Up @@ -180,7 +181,7 @@ class TestUnusedFunctions : public TestFixture {
" if (cond) ;\n"
" else f1();\n"
"}");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout_str());
}

void functionpointer() {
Expand Down Expand Up @@ -255,7 +256,7 @@ class TestUnusedFunctions : public TestFixture {
"}\n"
"\n"
"void h() { g<int>(); h(); }");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:8]: (style) The function 'h' is never used.\n", errout_str());
}

void template3() { // #4701
Expand Down Expand Up @@ -286,7 +287,7 @@ class TestUnusedFunctions : public TestFixture {
" test();\n"
" }\n"
"};");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:11]: (style) The function 'test' is never used.\n", errout_str());
}

void template5() { // #9220
Expand Down Expand Up @@ -541,6 +542,14 @@ class TestUnusedFunctions : public TestFixture {
errout_str());
}

void recursive() {
check("void f() {\n" // #8159
" f();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'f' is never used.\n",
errout_str());
}

void multipleFiles() {
CheckUnusedFunctions c;

Expand Down

0 comments on commit 86837c5

Please sign in to comment.