Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #13107: FP unusedStructMember with structured binding #7177

Merged
merged 9 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 20 additions & 0 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,26 @@ void CheckUnusedVar::checkStructMemberUsage()
if (bailout)
continue;

// Bailout if struct is used in structured binding
for (const Variable *var : symbolDatabase->variableList()) {
if (!var || !Token::Match(var->typeStartToken(), "auto &|&&| [ %varid%", var->declarationId()))
danmar marked this conversation as resolved.
Show resolved Hide resolved
continue;

const Token *tok = var->nameToken()->linkAt(-1);
if (!Token::Match(tok, "] %assign%"))
ludviggunne marked this conversation as resolved.
Show resolved Hide resolved
continue;

tok = tok->next()->astOperand2();
const ValueType *valueType = tok->valueType();

if (valueType && valueType->typeScope == &scope) {
bailout = true;
break;
}
}
if (bailout)
continue;

for (const Variable &var : scope.varlist) {
// only warn for variables without side effects
if (!var.typeStartToken()->isStandardType() && !var.isPointer() && !astIsContainer(var.nameToken()) && !isRecordTypeWithoutSideEffects(var.type()))
Expand Down
22 changes: 22 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(structmember27); // #13367
TEST_CASE(structmember_macro);
TEST_CASE(classmember);
TEST_CASE(structmemberStructuredBinding); // #13107

TEST_CASE(localvar1);
TEST_CASE(localvar2);
Expand Down Expand Up @@ -2024,6 +2025,27 @@ class TestUnusedVar : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void structmemberStructuredBinding() { // #13107
checkStructMemberUsage("struct S { int a, b; };\n"
ludviggunne marked this conversation as resolved.
Show resolved Hide resolved
"S f() {\n"
" S s{};\n"
" auto& [x, y] = s;\n"
" x = y = 1;\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

checkStructMemberUsage("struct S { int a, b; };\n"
"struct T { S s; };\n"
"T f() {\n"
" T t{};\n"
" auto& [x, y] = t.s;\n"
" x = y = 1;\n"
" return t;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void functionVariableUsage_(const char* file, int line, const char code[], bool cpp = true) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
Expand Down
Loading