Skip to content

Commit bd83630

Browse files
committed
Fixed #8990 (False positive: struct member not used (union))
1 parent 9133f9f commit bd83630

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,10 @@ void CheckUnusedVar::checkFunctionVariableUsage()
12211221

12221222
const Token *expr = varDecl ? varDecl : tok->astOperand1();
12231223

1224+
// Is variable in lhs a union member?
1225+
if (tok->previous() && tok->previous()->variable() && tok->previous()->variable()->nameToken()->scope()->type == Scope::eUnion)
1226+
continue;
1227+
12241228
FwdAnalysis fwdAnalysis(mTokenizer->isCPP(), mSettings->library);
12251229
if (fwdAnalysis.unusedValue(expr, start, scope->bodyEnd)) {
12261230
if (!bailoutTypeName.empty() && bailoutTypeName != "auto") {

test/testunusedvar.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class TestUnusedVar : public TestFixture {
161161
TEST_CASE(localvarStruct5);
162162
TEST_CASE(localvarStruct6);
163163
TEST_CASE(localvarStruct7);
164+
TEST_CASE(localvarStruct8);
164165
TEST_CASE(localvarStructArray);
165166

166167
TEST_CASE(localvarOp); // Usage with arithmetic operators
@@ -3445,6 +3446,25 @@ class TestUnusedVar : public TestFixture {
34453446
ASSERT_EQUALS("", errout.str());
34463447
}
34473448

3449+
void localvarStruct8() {
3450+
functionVariableUsage("struct s {\n"
3451+
" union {\n"
3452+
" struct {\n"
3453+
" int fld1 : 16;\n"
3454+
" int fld2 : 16;\n"
3455+
" };\n"
3456+
" int raw;\n"
3457+
" };\n"
3458+
"};\n"
3459+
"\n"
3460+
"void foo() {\n"
3461+
" struct s test;\n"
3462+
" test.raw = 0x100;\n"
3463+
" dostuff(test.fld1, test.fld2);\n"
3464+
"}");
3465+
ASSERT_EQUALS("", errout.str());
3466+
}
3467+
34483468
void localvarStructArray() {
34493469
// #3633 - detect that struct array is assigned a value
34503470
functionVariableUsage("void f() {\n"

0 commit comments

Comments
 (0)