Skip to content

Commit 25e62bd

Browse files
committed
Ignore types with anonymous fields
1 parent 30bfeae commit 25e62bd

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "UseDesignatedInitializersCheck.h"
10+
#include "clang/AST/APValue.h"
1011
#include "clang/AST/Decl.h"
1112
#include "clang/AST/Expr.h"
1213
#include "clang/AST/Stmt.h"
@@ -53,10 +54,19 @@ AST_MATCHER_FUNCTION(::internal::Matcher<CXXRecordDecl>, hasBaseWithFields) {
5354
return hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
5455
}
5556

57+
AST_MATCHER(FieldDecl, isAnonymousDecl) {
58+
if (const auto *Record =
59+
Node.getType().getCanonicalType()->getAsRecordDecl()) {
60+
return Record->isAnonymousStructOrUnion() || !Record->getIdentifier();
61+
}
62+
return false;
63+
}
64+
5665
void UseDesignatedInitializersCheck::registerMatchers(MatchFinder *Finder) {
5766
Finder->addMatcher(
5867
initListExpr(
59-
hasType(cxxRecordDecl(isAggregate(), unless(hasBaseWithFields()))
68+
hasType(cxxRecordDecl(isAggregate(), unless(hasBaseWithFields()),
69+
unless(has(fieldDecl(isAnonymousDecl()))))
6070
.bind("type")),
6171
unless(IgnoreSingleElementAggregates ? hasSingleElement()
6272
: unless(anything())),

clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,23 @@ S2 s27 = template2<S2>();
6161
struct S5: S2 { int x, y; };
6262

6363
S5 s51 {1, 2, .x = 3, .y = 4};
64+
65+
struct S6 {
66+
int i;
67+
struct { int j; } s;
68+
};
69+
70+
S6 s61 {1, 2};
71+
72+
struct S7 {
73+
union {
74+
int k;
75+
double d;
76+
} u;
77+
};
78+
79+
S7 s71 {1};
80+
81+
struct S8: S7 { int i; };
82+
83+
S8 s81{1, 2};

0 commit comments

Comments
 (0)