Fix FP uninitMemberVarPrivate with anonymous union (#3927)

This commit is contained in:
chrchr-github 2022-03-23 07:35:41 +01:00 committed by GitHub
parent 39567ea8c8
commit e0830693c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -170,7 +170,7 @@ void CheckClass::constructors()
if (var.isPrivate() && !var.isStatic() && if (var.isPrivate() && !var.isStatic() &&
(!var.isClass() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True))) { (!var.isClass() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True))) {
++needInit; ++needInit;
if (!var.isInit() && !var.hasDefault()) if (!var.isInit() && !var.hasDefault() && var.nameToken()->scope() == scope) // don't warn for anonymous union members
uninitVars.emplace_back(&var); uninitVars.emplace_back(&var);
else else
++haveInit; ++haveInit;

View File

@ -3187,8 +3187,8 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void uninitVarUnion1() { // ticket #3196 void uninitVarUnion1() {
check("class Fred\n" check("class Fred\n" // ticket #3196
"{\n" "{\n"
"private:\n" "private:\n"
" union { int a; int b; };\n" " union { int a; int b; };\n"
@ -3197,6 +3197,12 @@ private:
" { a = 0; }\n" " { a = 0; }\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("class Fred {\n"
"private:\n"
" union { int a{}; int b; };\n"
"};");
ASSERT_EQUALS("", errout.str());
} }
void uninitVarUnion2() { void uninitVarUnion2() {