Skip to content

Commit 00285d9

Browse files
committed
Stop collecting when we got two VarDecls from DS1
1 parent 7ac2a3e commit 00285d9

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ matchTwoVarDecl(const DeclStmt *DS1, const DeclStmt *DS2,
5555
return true;
5656
};
5757

58-
if (!CollectVarsInDeclStmt(DS1) || !CollectVarsInDeclStmt(DS2))
59-
return false;
60-
61-
if (Vars.size() != 2)
58+
// If we already get two VarDecls, then don't need to collect from DS2.
59+
if (!CollectVarsInDeclStmt(DS1) ||
60+
(Vars.size() != 2 && !CollectVarsInDeclStmt(DS2)) || Vars.size() != 2)
6261
return false;
6362

6463
if (InnerMatcher1.matches(*Vars[0].first, Finder, Builder) &&

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ void DecomposeByAssignWarnCases() {
3232
// CHECK-FIXES-ALL: // REMOVE
3333
}
3434

35+
{
36+
auto P = getPair<int, int>();
37+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
38+
// CHECK-FIXES-ALL: auto [x, y] = getPair<int, int>();
39+
int x = P.first, y = P.second; // REMOVE
40+
// CHECK-FIXES-ALL: // REMOVE
41+
int z;
42+
}
43+
3544
{
3645
auto P = getPair<int, int>();
3746
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
@@ -93,6 +102,14 @@ void forRangeWarnCases() {
93102
// CHECK-FIXES-ALL: // REMOVE
94103
}
95104

105+
for (auto P : Pairs) {
106+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
107+
// CHECK-FIXES-ALL: for (auto [x, y] : Pairs) {
108+
int x = P.first, y = P.second; // REMOVE
109+
// CHECK-FIXES-ALL: // REMOVE
110+
int z;
111+
}
112+
96113
for (const auto P : Pairs) {
97114
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
98115
// CHECK-FIXES-ALL: for (const auto [x, y] : Pairs) {

0 commit comments

Comments
 (0)