From 4e75c08f5884c35699a2e713c99cb4d86cbe840d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 10 Nov 2022 20:58:39 +0100 Subject: [PATCH] Respect [[maybe_unused]] in unusedPrivateFunction (#4579) --- lib/checkclass.cpp | 11 ++++++++--- test/testunusedprivfunc.cpp | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index d7576ccd3..eda8afce8 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1273,19 +1273,24 @@ void CheckClass::privateFunctions() } while (!privateFuncs.empty()) { + const auto& pf = privateFuncs.front(); + if (pf->retDef && pf->retDef->isAttributeMaybeUnused()) { + privateFuncs.pop_front(); + continue; + } // Check that all private functions are used - bool used = checkFunctionUsage(privateFuncs.front(), scope); // Usage in this class + bool used = checkFunctionUsage(pf, scope); // Usage in this class // Check in friend classes const std::vector& friendList = scope->definedType->friendList; for (int i = 0; i < friendList.size() && !used; i++) { if (friendList[i].type) - used = checkFunctionUsage(privateFuncs.front(), friendList[i].type->classScope); + used = checkFunctionUsage(pf, friendList[i].type->classScope); else used = true; // Assume, it is used if we do not see friend class } if (!used) - unusedPrivateFunctionError(privateFuncs.front()->tokenDef, scope->className, privateFuncs.front()->name()); + unusedPrivateFunctionError(pf->tokenDef, scope->className, pf->name()); privateFuncs.pop_front(); } diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index 02c1dec08..7574cd9d0 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -88,6 +88,7 @@ private: TEST_CASE(staticVariable); //ticket #5566 TEST_CASE(templateSimplification); //ticket #6183 + TEST_CASE(maybeUnused); } @@ -874,6 +875,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void maybeUnused() { + check("class C {\n" + " [[maybe_unused]] int f() { return 42; }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestUnusedPrivateFunction)