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