Skip to content

Commit 720bc68

Browse files
committed
Ignore direct init VarDecl and add some tests
1 parent b02bea3 commit 720bc68

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ AST_MATCHER(CXXRecordDecl, isPairType) {
184184
});
185185
}
186186

187+
AST_MATCHER(VarDecl, isDirectInitialization) {
188+
return Node.getInitStyle() != VarDecl::InitializationStyle::CInit;
189+
}
190+
187191
} // namespace
188192

189193
static auto getVarInitWithMemberMatcher(
@@ -267,7 +271,7 @@ void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
267271
declStmt(
268272
unless(isInMarco()),
269273
hasSingleDecl(
270-
varDecl(UnlessShouldBeIgnored,
274+
varDecl(UnlessShouldBeIgnored, unless(isDirectInitialization()),
271275
hasType(qualType(anyOf(PairType, lValueReferenceType(
272276
pointee(PairType))))
273277
.bind(PairVarTypeName)),

clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/use-structured-binding/fake_std_pair_tuple.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ namespace std {
33
struct pair {
44
T1 first;
55
T2 second;
6+
7+
pair() = default;
8+
pair(T1 first, T2 second) : first(first), second(second) {}
69
};
710

811
template<typename... Args>

clang-tools-extra/test/clang-tidy/checkers/modernize/use-structured-binding.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,23 @@ void StaticFieldPairTests() {
600600
int y = P.second;
601601
}
602602
}
603+
604+
void IgnoreDirectInit() {
605+
{
606+
std::pair<int, int> P{1, 1};
607+
int x = P.first;
608+
int y = P.second;
609+
}
610+
611+
{
612+
std::pair<int, int> P(1, 1);
613+
int x = P.first;
614+
int y = P.second;
615+
}
616+
617+
{
618+
std::pair<int, int> P;
619+
int x = P.first;
620+
int y = P.second;
621+
}
622+
}

0 commit comments

Comments
 (0)