Respect [[maybe_unused]] in unusedPrivateFunction (#4579)

This commit is contained in:
chrchr-github 2022-11-10 20:58:39 +01:00 committed by GitHub
parent 7f74aad8e2
commit 4e75c08f58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -1273,19 +1273,24 @@ void CheckClass::privateFunctions()
} }
while (!privateFuncs.empty()) { 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 // 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 // Check in friend classes
const std::vector<Type::FriendInfo>& friendList = scope->definedType->friendList; const std::vector<Type::FriendInfo>& friendList = scope->definedType->friendList;
for (int i = 0; i < friendList.size() && !used; i++) { for (int i = 0; i < friendList.size() && !used; i++) {
if (friendList[i].type) if (friendList[i].type)
used = checkFunctionUsage(privateFuncs.front(), friendList[i].type->classScope); used = checkFunctionUsage(pf, friendList[i].type->classScope);
else else
used = true; // Assume, it is used if we do not see friend class used = true; // Assume, it is used if we do not see friend class
} }
if (!used) if (!used)
unusedPrivateFunctionError(privateFuncs.front()->tokenDef, scope->className, privateFuncs.front()->name()); unusedPrivateFunctionError(pf->tokenDef, scope->className, pf->name());
privateFuncs.pop_front(); privateFuncs.pop_front();
} }

View File

@ -88,6 +88,7 @@ private:
TEST_CASE(staticVariable); //ticket #5566 TEST_CASE(staticVariable); //ticket #5566
TEST_CASE(templateSimplification); //ticket #6183 TEST_CASE(templateSimplification); //ticket #6183
TEST_CASE(maybeUnused);
} }
@ -874,6 +875,13 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void maybeUnused() {
check("class C {\n"
" [[maybe_unused]] int f() { return 42; }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestUnusedPrivateFunction) REGISTER_TEST(TestUnusedPrivateFunction)