1 // RUN: %check_clang_tidy -std=c++14,c++17 %s readability-use-anyofallof %t -- -- -fexceptions
2
good_any_of()3 bool good_any_of() {
4 int v[] = {1, 2, 3};
5 // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: replace loop by 'std::any_of()' [readability-use-anyofallof]
6 for (int i : v)
7 if (i)
8 return true;
9 return false;
10 }
11
12 bool cond(int i);
13
good_any_of2()14 bool good_any_of2() {
15 int v[] = {1, 2, 3};
16 for (int i : v) {
17 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: replace loop by 'std::any_of()'
18 int k = i / 2;
19 if (cond(k))
20 return true;
21 }
22 return false;
23 }
24
good_any_of3()25 bool good_any_of3() {
26 int v[] = {1, 2, 3};
27 for (int i : v) {
28 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: replace loop by 'std::any_of()'
29 if (i == 3)
30 continue;
31 if (i)
32 return true;
33 }
34
35 return false;
36 }
37
good_any_of_use_external(int comp)38 bool good_any_of_use_external(int comp) {
39 int v[] = {1, 2, 3};
40 for (int i : v) {
41 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: replace loop by 'std::any_of()'
42 if (i == comp)
43 return true;
44 }
45
46 return false;
47 }
48
good_any_of_no_cond()49 bool good_any_of_no_cond() {
50 int v[] = {1, 2, 3};
51 for (int i : v) {
52 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: replace loop by 'std::any_of()'
53 return true; // Not a real loop, but technically can become any_of.
54 }
55
56 return false;
57 }
58
good_any_of_local_modification()59 bool good_any_of_local_modification() {
60 int v[] = {1, 2, 3};
61 for (int i : v) {
62 int j = i;
63 j++; // FIXME: Any non-const use disables check.
64 if (j > 3)
65 return true;
66 }
67
68 return false;
69 }
70
good_any_of_throw()71 bool good_any_of_throw() {
72 int v[] = {1, 2, 3};
73 for (int i : v) {
74 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: replace loop by 'std::any_of()'
75 if (i > 3)
76 return true;
77 if (i == 42)
78 throw 0;
79 }
80
81 return false;
82 }
83
bad_any_of1()84 bool bad_any_of1() {
85 int v[] = {1, 2, 3};
86 for (int i : v) {
87 if (i)
88 return false; // bad constant
89 }
90 return false;
91 }
92
bad_any_of2()93 bool bad_any_of2() {
94 int v[] = {1, 2, 3};
95 for (int i : v)
96 if (i)
97 return true;
98
99 return true; // bad return
100 }
101
bad_any_of3()102 bool bad_any_of3() {
103 int v[] = {1, 2, 3};
104 for (int i : v)
105 if (i)
106 return true;
107 else
108 return i / 2; // bad return
109
110 return false;
111 }
112
bad_any_of_control_flow1()113 bool bad_any_of_control_flow1() {
114 int v[] = {1, 2, 3};
115 for (int i : v) {
116 break; // bad control flow
117 if (i)
118 return true;
119 }
120
121 return false;
122 }
123
bad_any_of_control_flow2()124 bool bad_any_of_control_flow2() {
125 int v[] = {1, 2, 3};
126 for (int i : v) {
127 goto end; // bad control flow
128 if (i)
129 return true;
130 }
131
132 end:
133 return false;
134 }
135
bad_any_of4()136 bool bad_any_of4() {
137 return false; // wrong order
138
139 int v[] = {1, 2, 3};
140 for (int i : v) {
141 if (i)
142 return true;
143 }
144 }
145
bad_any_of5()146 bool bad_any_of5() {
147 int v[] = {1, 2, 3};
148 int j = 0;
149 for (int i : v) {
150 j++; // modifications
151 if (i)
152 return true;
153 }
154 return false;
155 }
156
bad_any_of6()157 bool bad_any_of6() {
158 int v[] = {1, 2, 3};
159 for (int i : v) {
160 if (i)
161 return true;
162 }
163 int j = 0; // Statements between loop and return
164 j++;
165 return false;
166 }
167
bad_any_of7()168 bool bad_any_of7() {
169 int v[] = {1, 2, 3};
170 for (int i : v) {
171 i; // No 'return true' in body.
172 }
173 return false;
174 }
175
good_all_of()176 bool good_all_of() {
177 int v[] = {1, 2, 3};
178 // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: replace loop by 'std::all_of()' [readability-use-anyofallof]
179 for (int i : v)
180 if (i)
181 return false;
182 return true;
183 }
184