Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ struct InvalidContainerAnalyzer {
const Token* ftok;
};
std::unordered_map<int, Reference> expressions;
ErrorPath errorPath;

void add(const std::vector<Reference>& refs) {
for (const Reference& r : refs) {
add(r);
Expand Down
13 changes: 5 additions & 8 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,25 +1455,20 @@ void CheckUnusedVar::checkStructMemberUsage()
continue;
}

// Bail out if struct/union contains any functions
if (!scope.functionList.empty())
continue;

// Bail out for template struct, members might be used in non-matching instantiations
if (scope.className.find('<') != std::string::npos)
continue;

// bail out if struct is inherited
bool bailout = std::any_of(symbolDatabase->scopeList.cbegin(), symbolDatabase->scopeList.cend(), [&](const Scope& derivedScope) {
const bool isInherited = std::any_of(symbolDatabase->scopeList.cbegin(), symbolDatabase->scopeList.cend(), [&](const Scope& derivedScope) {
const Type* dType = derivedScope.definedType;
return dType && std::any_of(dType->derivedFrom.cbegin(), dType->derivedFrom.cend(), [&](const Type::BaseInfo& derivedFrom) {
return derivedFrom.type == scope.definedType;
return derivedFrom.type == scope.definedType && derivedFrom.access != AccessControl::Private;
});
});
if (bailout)
continue;

// bail out for extern/global struct
bool bailout = false;
for (const Variable* var : symbolDatabase->variableList()) {
if (var && (var->isExtern() || (var->isGlobal() && !var->isStatic())) && var->typeEndToken()->str() == scope.className) {
bailout = true;
Expand Down Expand Up @@ -1510,6 +1505,8 @@ void CheckUnusedVar::checkStructMemberUsage()
// only warn for variables without side effects
if (!var.typeStartToken()->isStandardType() && !var.isPointer() && !astIsContainer(var.nameToken()) && !isRecordTypeWithoutSideEffects(var.type()))
continue;
if (isInherited && !var.isPrivate())
continue;

// Check if the struct member variable is used anywhere in the file
bool use = false;
Expand Down
28 changes: 28 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,34 @@ class TestUnusedVar : public TestFixture {
" int i{};\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (style) class member 'C::i' is never used.\n", errout.str());

checkStructMemberUsage("class C {\n"
" int i{}, j{};\n"
"public:\n"
" int& get() { return i; }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (style) class member 'C::j' is never used.\n", errout.str());

checkStructMemberUsage("class C {\n"
"private:\n"
" int i;\n"
"};\n"
"class D : public C {};\n");
ASSERT_EQUALS("[test.cpp:3]: (style) class member 'C::i' is never used.\n", errout.str());

checkStructMemberUsage("class C {\n"
"public:\n"
" int i;\n"
"};\n"
"class D : C {};\n");
ASSERT_EQUALS("[test.cpp:3]: (style) class member 'C::i' is never used.\n", errout.str());

checkStructMemberUsage("class C {\n"
"public:\n"
" int i;\n"
"};\n"
"class D : public C {};\n");
ASSERT_EQUALS("", errout.str());
}

void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
Expand Down