Skip to content

Commit 787bb49

Browse files
committed
improve
1 parent 4360b18 commit 787bb49

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,14 +1601,19 @@ void CheckUnusedVar::checkStructMemberUsage()
16011601

16021602
// #7458 - if struct is declared inside union and any struct member is used,
16031603
// then don't warn about other struct members
1604-
bool structInUnionWithUsedMember = false;
16051604
if (scope.type == ScopeType::eStruct && scope.nestedIn && scope.nestedIn->type == ScopeType::eUnion) {
1605+
bool structMemberUsed = false;
1606+
16061607
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
1607-
if (tok->variable() && tok->variable()->scope() == scope.nestedIn) {
1608-
structInUnionWithUsedMember = true;
1608+
if (tok->variable() && tok != tok->variable()->nameToken() && tok->variable()->scope() == &scope) {
1609+
structMemberUsed = true;
16091610
break;
16101611
}
16111612
}
1613+
1614+
// Skip reporting unused members if this struct is in a union and any member is used
1615+
if (structMemberUsed)
1616+
continue;
16121617
}
16131618

16141619
for (const Variable &var : scope.varlist) {
@@ -1624,10 +1629,6 @@ void CheckUnusedVar::checkStructMemberUsage()
16241629
if (mTokenizer->isVarUsedInTemplate(var.declarationId()))
16251630
continue;
16261631

1627-
// Skip reporting unused members if this struct is in a union and any member is used
1628-
if (structInUnionWithUsedMember)
1629-
continue;
1630-
16311632
// Check if the struct member variable is used anywhere in the file
16321633
bool use = false;
16331634
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {

0 commit comments

Comments
 (0)