Skip to content

Commit 118c166

Browse files
committed
Fix
1 parent 29f44eb commit 118c166

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,13 @@ static llvm::StringLiteral getInitializer(QualType QT, bool UseAssignment) {
432432
}
433433
}
434434

435-
void ProTypeMemberInitCheck::checkMissingMemberInitializer(
436-
ASTContext &Context, const CXXRecordDecl &ClassDecl,
437-
const CXXConstructorDecl *Ctor) {
438-
const bool IsUnion = ClassDecl.isUnion();
439-
440-
if (IsUnion && ClassDecl.hasInClassInitializer())
441-
return;
442-
443-
// Gather all fields (direct and indirect) that need to be initialized.
444-
SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
435+
static void
436+
computeFieldsToInit(const ASTContext &Context, const RecordDecl &Record,
437+
bool IgnoreArrays,
438+
SmallPtrSetImpl<const FieldDecl *> &FieldsToInit) {
445439
bool AnyMemberHasInitPerUnion = false;
446440
forEachFieldWithFilter(
447-
ClassDecl, ClassDecl.fields(), AnyMemberHasInitPerUnion,
441+
Record, Record.fields(), AnyMemberHasInitPerUnion,
448442
[&](const FieldDecl *F) {
449443
if (IgnoreArrays && F->getType()->isArrayType())
450444
return;
@@ -459,6 +453,19 @@ void ProTypeMemberInitCheck::checkMissingMemberInitializer(
459453
!AnyMemberHasInitPerUnion)
460454
FieldsToInit.insert(F);
461455
});
456+
}
457+
458+
void ProTypeMemberInitCheck::checkMissingMemberInitializer(
459+
ASTContext &Context, const CXXRecordDecl &ClassDecl,
460+
const CXXConstructorDecl *Ctor) {
461+
const bool IsUnion = ClassDecl.isUnion();
462+
463+
if (IsUnion && ClassDecl.hasInClassInitializer())
464+
return;
465+
466+
// Gather all fields (direct and indirect) that need to be initialized.
467+
SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
468+
computeFieldsToInit(Context, ClassDecl, IgnoreArrays, FieldsToInit);
462469
if (FieldsToInit.empty())
463470
return;
464471

@@ -508,7 +515,7 @@ void ProTypeMemberInitCheck::checkMissingMemberInitializer(
508515
// Collect all fields but only suggest a fix for the first member of unions,
509516
// as initializing more than one union member is an error.
510517
SmallPtrSet<const FieldDecl *, 16> FieldsToFix;
511-
AnyMemberHasInitPerUnion = false;
518+
bool AnyMemberHasInitPerUnion = false;
512519
forEachFieldWithFilter(ClassDecl, ClassDecl.fields(),
513520
AnyMemberHasInitPerUnion, [&](const FieldDecl *F) {
514521
if (!FieldsToInit.contains(F))
@@ -589,23 +596,7 @@ void ProTypeMemberInitCheck::checkUninitializedTrivialType(
589596
return;
590597

591598
SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
592-
bool AnyMemberHasInitPerUnion = false;
593-
forEachFieldWithFilter(
594-
*Record, Record->fields(), AnyMemberHasInitPerUnion,
595-
[&](const FieldDecl *F) {
596-
if (IgnoreArrays && F->getType()->isArrayType())
597-
return;
598-
if (F->hasInClassInitializer() && F->getParent()->isUnion()) {
599-
AnyMemberHasInitPerUnion = true;
600-
removeFieldInitialized(F, FieldsToInit);
601-
}
602-
if (!F->hasInClassInitializer() &&
603-
utils::type_traits::isTriviallyDefaultConstructible(F->getType(),
604-
Context) &&
605-
!isEmpty(Context, F->getType()) && !F->isUnnamedBitField() &&
606-
!AnyMemberHasInitPerUnion)
607-
FieldsToInit.insert(F);
608-
});
599+
computeFieldsToInit(Context, *Record, IgnoreArrays, FieldsToInit);
609600

610601
if (FieldsToInit.empty())
611602
return;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ Changes in existing checks
426426

427427
- Improved :doc:`cppcoreguidelines-pro-type-member-init
428428
<clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check to
429-
correctly ignore ``std::array`` and other containers when `IgnoreArrays`
430-
option is set to `true`.
429+
correctly ignore ``std::array`` and other array-like containers when
430+
`IgnoreArrays` option is set to `true`.
431431

432432
- Improved :doc:`google-readability-casting
433433
<clang-tidy/checks/google/readability-casting>` check by adding fix-it

0 commit comments

Comments
 (0)