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/
33#include " fake_std_pair_tuple.h"
44
55template <typename T>
@@ -16,42 +16,42 @@ struct TestClass {
1616void DecomposeByAssignWarnCases () {
1717 {
1818 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>();
2121 int x = P.first ;
2222 int y = P.second ;
2323 }
2424
2525 {
2626 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>();
2929 int x = P.first ;
3030 auto y = P.second ;
3131 }
3232
3333 {
3434 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>();
3737 const int x = P.first ;
3838 const auto y = P.second ;
3939 }
4040
4141 {
4242 std::pair<int , int > otherP;
4343 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;
4646 int & x = P.first ;
4747 auto & y = P.second ;
4848 }
4949
5050 {
5151 std::pair<int , int > otherP;
5252 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;
5555 const int & x = P.first ;
5656 const auto & y = P.second ;
5757 }
@@ -60,44 +60,44 @@ void DecomposeByAssignWarnCases() {
6060void forRangeWarnCases () {
6161 std::pair<int , int > Pairs[10 ];
6262 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) {
6565 int x = P.first ;
6666 int y = P.second ;
6767 }
6868
6969 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) {
7272 const int x = P.first ;
7373 const int y = P.second ;
7474 }
7575
7676 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) {
7979 int & x = P.first ;
8080 int & y = P.second ;
8181 }
8282
8383 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) {
8686 const int & x = P.first ;
8787 const int & y = P.second ;
8888 }
8989
9090 std::pair<TestClass, TestClass> ClassPairs[10 ];
9191 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) {
9494 TestClass c1 = P.first ;
9595 TestClass c2 = P.second ;
9696 }
9797
9898 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) {
101101 const TestClass c1 = P.first ;
102102 const TestClass c2 = P.second ;
103103 }
@@ -142,20 +142,20 @@ void stdTieWarnCases() {
142142 int a = 0 ;
143143 int b = 0 ;
144144 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>();
147147
148148 int * pa = nullptr ;
149149 int * pb = nullptr ;
150150 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*>();
153153
154154 TestClass c1 (1 , 2 );
155155 TestClass c2 = TestClass {3 , 4 };
156156 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>();
159159}
160160
161161void stdTieNotWarnCases () {
@@ -250,3 +250,79 @@ void NotWarnForMacro2() {
250250 int x = P.first ;
251251 int y = P.second ;
252252}
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