1// RUN: %check_clang_tidy %s modernize-loop-convert %t
2
3template <class T>
4struct Vector {
5 using iterator = T*;
6 unsigned size() const;
7 const T &operator[](int) const;
8 T &operator[](int);
9 T *begin();
10 T *end();
11 const T *begin() const;
12 const T *end() const;
13};
14
15template <typename T>
16void copyArg(T);
17
18class TestArrayOfVector {
19 Vector<int> W[2];
20
21 void foo() const {
22 for (int I = 0; I < W[0].size(); ++I) {
23 if (W[0][I])
24 copyArg(W[0][I]);
25 }
26 // CHECK-MESSAGES: :[[@LINE-4]]:5: warning: use range-based for loop
27 // CHECK-FIXES: for (int I : W[0]) {
28 // CHECK-FIXES-NEXT: if (I)
29 // CHECK-FIXES-NEXT: copyArg(I);
30 }
31};
32
33class TestVectorOfVector {
34 Vector<Vector<int>> X;
35
36 void foo() const {
37 for (int J = 0; J < X[0].size(); ++J) {
38 if (X[0][J])
39 copyArg(X[0][J]);
40 }
41 // CHECK-MESSAGES: :[[@LINE-4]]:5: warning: use range-based for loop
42 // CHECK-FIXES: for (int J : X[0]) {
43 // CHECK-FIXES-NEXT: if (J)
44 // CHECK-FIXES-NEXT: copyArg(J);
45 }
46};
47
48void testVectorOfVectorOfVector() {
49 Vector<Vector<Vector<int>>> Y;
50 for (int J = 0; J < Y[3].size(); ++J) {
51 if (Y[3][J][7])
52 copyArg(Y[3][J][8]);
53 }
54 // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop
55 // CHECK-FIXES: for (auto & J : Y[3]) {
56 // CHECK-FIXES-NEXT: if (J[7])
57 // CHECK-FIXES-NEXT: copyArg(J[8]);
58
59 for (int J = 0; J < Y[3][4].size(); ++J) {
60 if (Y[3][4][J])
61 copyArg(Y[3][4][J]);
62 }
63 // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop
64 // CHECK-FIXES: for (int J : Y[3][4]) {
65 // CHECK-FIXES-NEXT: if (J)
66 // CHECK-FIXES-NEXT: copyArg(J);
67}
68
69void testVectorOfVectorIterator() {
70 Vector<Vector<int>> Z;
71 for (Vector<int>::iterator it = Z[4].begin(); it != Z[4].end(); ++it) {
72 if (*it)
73 copyArg(*it);
74 }
75 // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop
76 // CHECK-FIXES: for (int & it : Z[4]) {
77 // CHECK-FIXES-NEXT: if (it)
78 // CHECK-FIXES-NEXT: copyArg(it);
79}
80

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-multidimensional.cpp