From 86837c5a5fe7e5c7dd5ea9dc4b655cb8e0564b5c Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 12 Jun 2024 23:08:05 +0200 Subject: [PATCH] Fix #8159 FN unusedFunction: sole recursive call --- lib/checkunusedfunctions.cpp | 7 ++++++- test/testunusedfunctions.cpp | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index f538b3a3544..8310dafaa1f 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -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(); @@ -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); diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 91f1c1e11bf..0faae42bc03 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -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 @@ -116,7 +117,7 @@ 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() { @@ -124,7 +125,7 @@ class TestUnusedFunctions : public TestFixture { "{\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() { @@ -132,7 +133,7 @@ class TestUnusedFunctions : public TestFixture { "{\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() { @@ -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() { @@ -255,7 +256,7 @@ class TestUnusedFunctions : public TestFixture { "}\n" "\n" "void h() { g(); h(); }"); - ASSERT_EQUALS("", errout_str()); + ASSERT_EQUALS("[test.cpp:8]: (style) The function 'h' is never used.\n", errout_str()); } void template3() { // #4701 @@ -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 @@ -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;