Skip to content

Commit

Permalink
Fix #12485 Regression: FN: unusedStructMember (#6079)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Mar 4, 2024
1 parent 3a31d83 commit 0ab4257
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type)

// Is there a member variable with possible side effects
for (const Variable& var : type->classScope->varlist) {
withoutSideEffects = isVariableWithoutSideEffects(var);
withoutSideEffects = isVariableWithoutSideEffects(var, type);
if (!withoutSideEffects) {
return withoutSideEffects;
}
Expand All @@ -1623,10 +1623,10 @@ bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type)
return (withoutSideEffects = true);
}

bool CheckUnusedVar::isVariableWithoutSideEffects(const Variable& var)
bool CheckUnusedVar::isVariableWithoutSideEffects(const Variable& var, const Type* type)
{
const Type* variableType = var.type();
if (variableType) {
if (variableType && variableType != type) {
if (!isRecordTypeWithoutSideEffects(variableType))
return false;
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/checkunusedvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CPPCHECKLIB CheckUnusedVar : public Check {
void checkStructMemberUsage();

bool isRecordTypeWithoutSideEffects(const Type* type);
bool isVariableWithoutSideEffects(const Variable& var);
bool isVariableWithoutSideEffects(const Variable& var, const Type* type = nullptr);
bool isEmptyType(const Type* type);
bool isFunctionWithoutSideEffects(const Function& func, const Token* functionUsageToken,
std::list<const Function*> checkedFuncs);
Expand Down
17 changes: 17 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(structmember22); // #11016
TEST_CASE(structmember23);
TEST_CASE(structmember24); // #10847
TEST_CASE(structmember25);
TEST_CASE(structmember_macro);
TEST_CASE(classmember);

Expand Down Expand Up @@ -1957,6 +1958,22 @@ class TestUnusedVar : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void structmember25() {
checkStructMemberUsage("struct S {\n" // #12485
" S* p;\n"
" int i;\n"
"};\n"
"struct T {\n"
" S s;\n"
" int j;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (style) struct member 'S::p' is never used.\n"
"[test.cpp:3]: (style) struct member 'S::i' is never used.\n"
"[test.cpp:6]: (style) struct member 'T::s' is never used.\n"
"[test.cpp:7]: (style) struct member 'T::j' is never used.\n",
errout.str());
}

void structmember_macro() {
checkStructMemberUsageP("#define S(n) struct n { int a, b, c; };\n"
"S(unused);\n");
Expand Down

0 comments on commit 0ab4257

Please sign in to comment.