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

source code of clang-tools-extra/test/clang-tidy/checkers/readability/use-anyofallof.cpp