Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8159 FN unusedFunction: sole recursive call #6517

Merged
merged 12 commits into from
Jun 16, 2024
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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add the missing cond variable when fixing this up.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - too late.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cond could be an unknown global variable.
The return2() test is not compilable though.

" 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
Loading