Skip to content

Commit 4e75c08

Browse files
Respect [[maybe_unused]] in unusedPrivateFunction (danmar#4579)
1 parent 7f74aad commit 4e75c08

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/checkclass.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,19 +1273,24 @@ void CheckClass::privateFunctions()
12731273
}
12741274

12751275
while (!privateFuncs.empty()) {
1276+
const auto& pf = privateFuncs.front();
1277+
if (pf->retDef && pf->retDef->isAttributeMaybeUnused()) {
1278+
privateFuncs.pop_front();
1279+
continue;
1280+
}
12761281
// Check that all private functions are used
1277-
bool used = checkFunctionUsage(privateFuncs.front(), scope); // Usage in this class
1282+
bool used = checkFunctionUsage(pf, scope); // Usage in this class
12781283
// Check in friend classes
12791284
const std::vector<Type::FriendInfo>& friendList = scope->definedType->friendList;
12801285
for (int i = 0; i < friendList.size() && !used; i++) {
12811286
if (friendList[i].type)
1282-
used = checkFunctionUsage(privateFuncs.front(), friendList[i].type->classScope);
1287+
used = checkFunctionUsage(pf, friendList[i].type->classScope);
12831288
else
12841289
used = true; // Assume, it is used if we do not see friend class
12851290
}
12861291

12871292
if (!used)
1288-
unusedPrivateFunctionError(privateFuncs.front()->tokenDef, scope->className, privateFuncs.front()->name());
1293+
unusedPrivateFunctionError(pf->tokenDef, scope->className, pf->name());
12891294

12901295
privateFuncs.pop_front();
12911296
}

test/testunusedprivfunc.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class TestUnusedPrivateFunction : public TestFixture {
8888
TEST_CASE(staticVariable); //ticket #5566
8989

9090
TEST_CASE(templateSimplification); //ticket #6183
91+
TEST_CASE(maybeUnused);
9192
}
9293

9394

@@ -874,6 +875,13 @@ class TestUnusedPrivateFunction : public TestFixture {
874875
"}");
875876
ASSERT_EQUALS("", errout.str());
876877
}
878+
879+
void maybeUnused() {
880+
check("class C {\n"
881+
" [[maybe_unused]] int f() { return 42; }\n"
882+
"};");
883+
ASSERT_EQUALS("", errout.str());
884+
}
877885
};
878886

879887
REGISTER_TEST(TestUnusedPrivateFunction)

0 commit comments

Comments
 (0)