1
- // RUN: %check_clang_tidy -std=c++17 -or-later %s modernize-use-structured-binding %t -- -- -I %S/Inputs/use-structured-binding/
2
-
1
+ // RUN: %check_clang_tidy -check-suffix=ALL,CPP20ORLATER - std=c++20 -or-later %s modernize-use-structured-binding %t -- -- -I %S/Inputs/use-structured-binding/
2
+ // RUN: %check_clang_tidy -check-suffix=ALL -std=c++17 %s modernize-use-structured-binding %t -- -- -I %S/Inputs/use-structured-binding/
3
3
#include " fake_std_pair_tuple.h"
4
4
5
5
template <typename T>
@@ -16,42 +16,42 @@ struct TestClass {
16
16
void DecomposeByAssignWarnCases () {
17
17
{
18
18
auto P = getPair<int , int >();
19
- // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
20
- // CHECK-FIXES: auto [x, y] = getPair<int, int>();
19
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
20
+ // CHECK-FIXES-ALL : auto [x, y] = getPair<int, int>();
21
21
int x = P.first ;
22
22
int y = P.second ;
23
23
}
24
24
25
25
{
26
26
auto P = getPair<int , int >();
27
- // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
28
- // CHECK-FIXES: auto [x, y] = getPair<int, int>();
27
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
28
+ // CHECK-FIXES-ALL : auto [x, y] = getPair<int, int>();
29
29
int x = P.first ;
30
30
auto y = P.second ;
31
31
}
32
32
33
33
{
34
34
const auto P = getPair<int , int >();
35
- // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
36
- // CHECK-FIXES: const auto [x, y] = getPair<int, int>();
35
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
36
+ // CHECK-FIXES-ALL : const auto [x, y] = getPair<int, int>();
37
37
const int x = P.first ;
38
38
const auto y = P.second ;
39
39
}
40
40
41
41
{
42
42
std::pair<int , int > otherP;
43
43
auto & P = otherP;
44
- // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
45
- // CHECK-FIXES: auto& [x, y] = otherP;
44
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
45
+ // CHECK-FIXES-ALL : auto& [x, y] = otherP;
46
46
int & x = P.first ;
47
47
auto & y = P.second ;
48
48
}
49
49
50
50
{
51
51
std::pair<int , int > otherP;
52
52
const auto & P = otherP;
53
- // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
54
- // CHECK-FIXES: const auto& [x, y] = otherP;
53
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:5: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
54
+ // CHECK-FIXES-ALL : const auto& [x, y] = otherP;
55
55
const int & x = P.first ;
56
56
const auto & y = P.second ;
57
57
}
@@ -60,44 +60,44 @@ void DecomposeByAssignWarnCases() {
60
60
void forRangeWarnCases () {
61
61
std::pair<int , int > Pairs[10 ];
62
62
for (auto P : Pairs) {
63
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
64
- // CHECK-FIXES: for (auto [x, y] : Pairs) {
63
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
64
+ // CHECK-FIXES-ALL : for (auto [x, y] : Pairs) {
65
65
int x = P.first ;
66
66
int y = P.second ;
67
67
}
68
68
69
69
for (const auto P : Pairs) {
70
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
71
- // CHECK-FIXES: for (const auto [x, y] : Pairs) {
70
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
71
+ // CHECK-FIXES-ALL : for (const auto [x, y] : Pairs) {
72
72
const int x = P.first ;
73
73
const int y = P.second ;
74
74
}
75
75
76
76
for (auto & P : Pairs) {
77
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
78
- // CHECK-FIXES: for (auto& [x, y] : Pairs) {
77
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
78
+ // CHECK-FIXES-ALL : for (auto& [x, y] : Pairs) {
79
79
int & x = P.first ;
80
80
int & y = P.second ;
81
81
}
82
82
83
83
for (const auto & P : Pairs) {
84
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
85
- // CHECK-FIXES: for (const auto& [x, y] : Pairs) {
84
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
85
+ // CHECK-FIXES-ALL : for (const auto& [x, y] : Pairs) {
86
86
const int & x = P.first ;
87
87
const int & y = P.second ;
88
88
}
89
89
90
90
std::pair<TestClass, TestClass> ClassPairs[10 ];
91
91
for (auto P : ClassPairs) {
92
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
93
- // CHECK-FIXES: for (auto [c1, c2] : ClassPairs) {
92
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
93
+ // CHECK-FIXES-ALL : for (auto [c1, c2] : ClassPairs) {
94
94
TestClass c1 = P.first ;
95
95
TestClass c2 = P.second ;
96
96
}
97
97
98
98
for (const auto P : ClassPairs) {
99
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
100
- // CHECK-FIXES: for (const auto [c1, c2] : ClassPairs) {
99
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
100
+ // CHECK-FIXES-ALL : for (const auto [c1, c2] : ClassPairs) {
101
101
const TestClass c1 = P.first ;
102
102
const TestClass c2 = P.second ;
103
103
}
@@ -142,20 +142,20 @@ void stdTieWarnCases() {
142
142
int a = 0 ;
143
143
int b = 0 ;
144
144
std::tie (a, b) = getPair<int , int >();
145
- // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
146
- // CHECK-FIXES: auto [a, b] = getPair<int, int>();
145
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
146
+ // CHECK-FIXES-ALL : auto [a, b] = getPair<int, int>();
147
147
148
148
int * pa = nullptr ;
149
149
int * pb = nullptr ;
150
150
std::tie (pa, pb) = getPair<int *, int *>();
151
- // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
152
- // CHECK-FIXES: auto [pa, pb] = getPair<int*, int*>();
151
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
152
+ // CHECK-FIXES-ALL : auto [pa, pb] = getPair<int*, int*>();
153
153
154
154
TestClass c1 (1 , 2 );
155
155
TestClass c2 = TestClass {3 , 4 };
156
156
std::tie (c1, c2) = getPair<TestClass, TestClass>();
157
- // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
158
- // CHECK-FIXES: auto [c1, c2] = getPair<TestClass, TestClass>();
157
+ // CHECK-MESSAGES-ALL : :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
158
+ // CHECK-FIXES-ALL : auto [c1, c2] = getPair<TestClass, TestClass>();
159
159
}
160
160
161
161
void stdTieNotWarnCases () {
@@ -250,3 +250,79 @@ void NotWarnForMacro2() {
250
250
int x = P.first ;
251
251
int y = P.second ;
252
252
}
253
+
254
+ void captureByVal () {
255
+ auto P = getPair<int , int >();
256
+ // CHECK-MESSAGES-CPP20ORLATER: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
257
+ // CHECK-FIXES-CPP20ORLATER: auto [x, y] = getPair<int, int>();
258
+ int x = P.first ;
259
+ int y = P.second ;
260
+
261
+ auto lambda = [x]() {
262
+ int y = x;
263
+ };
264
+ }
265
+
266
+ void captureByRef () {
267
+ auto P = getPair<int , int >();
268
+ // CHECK-MESSAGES-CPP20ORLATER: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
269
+ // CHECK-FIXES-CPP20ORLATER: auto [x, y] = getPair<int, int>();
270
+ int x = P.first ;
271
+ int y = P.second ;
272
+
273
+ auto lambda = [&x]() {
274
+ x = 1 ;
275
+ };
276
+ }
277
+
278
+ void captureByAllRef () {
279
+ auto P = getPair<int , int >();
280
+ // CHECK-MESSAGES-CPP20ORLATER: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
281
+ // CHECK-FIXES-CPP20ORLATER: auto [x, y] = getPair<int, int>();
282
+ int x = P.first ;
283
+ int y = P.second ;
284
+
285
+ auto lambda = [&]() {
286
+ x = 1 ;
287
+ };
288
+ }
289
+
290
+ void deepLambda () {
291
+ auto P = getPair<int , int >();
292
+ // CHECK-MESSAGES-CPP20ORLATER: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
293
+ // CHECK-FIXES-CPP20ORLATER: auto [x, y] = getPair<int, int>();
294
+ int x = P.first ;
295
+ int y = P.second ;
296
+
297
+ {
298
+ auto lambda = [x]() {
299
+ int y = x;
300
+ };
301
+ }
302
+ }
303
+
304
+ void forRangeNotWarn () {
305
+ std::pair<int , int > Pairs[10 ];
306
+ for (auto P : Pairs) {
307
+ // CHECK-MESSAGES-CPP20ORLATER: :[[@LINE-1]]:8: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
308
+ // CHECK-FIXES-CPP20ORLATER: for (auto [x, y] : Pairs) {
309
+ int x = P.first ;
310
+ int y = P.second ;
311
+
312
+ auto lambda = [&]() {
313
+ x = 1 ;
314
+ };
315
+ }
316
+ }
317
+
318
+ void stdTieNotWarn () {
319
+ int x = 0 ;
320
+ int y = 0 ;
321
+ std::tie (x, y) = getPair<int , int >();
322
+ // CHECK-MESSAGES-CPP20ORLATER: :[[@LINE-1]]:3: warning: use structured binding to decompose a pair [modernize-use-structured-binding]
323
+ // CHECK-FIXES-CPP20ORLATER: auto [x, y] = getPair<int, int>();
324
+
325
+ auto lambda = [&x]() {
326
+ x = 1 ;
327
+ };
328
+ }
0 commit comments