You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang-tidy] Extend readability-container-size-empty to arbitrary class with size() and empty()
This patch extends readability-container-size-empty check allowing it to produce
warnings not only for STL containers, but also for containers, which provide two
functions matching following signatures:
* `size_type size() const;`
* `bool empty() const;`
Where `size_type` can be any kind of integer type.
This functionality was proposed in https://llvm.org/bugs/show_bug.cgi?id=26823
by Eugene Zelenko.
Approval: alexfh
Reviewers: alexfh, aaron.ballman, Eugene.Zelenko
Subscribers: etienneb, Prazek, hokein, xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D24349
llvm-svn: 281307
Copy file name to clipboardExpand all lines: clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp
+157-2
Original file line number
Diff line number
Diff line change
@@ -24,9 +24,43 @@ template <typename T> struct set {
24
24
};
25
25
}
26
26
27
-
28
27
}
29
28
29
+
template <typename T>
30
+
classTemplatedContainer {
31
+
public:
32
+
intsize() const;
33
+
boolempty() const;
34
+
};
35
+
36
+
template <typename T>
37
+
classPrivateEmpty {
38
+
public:
39
+
intsize() const;
40
+
private:
41
+
boolempty() const;
42
+
};
43
+
44
+
structBoolSize {
45
+
boolsize() const;
46
+
boolempty() const;
47
+
};
48
+
49
+
structEnumSize {
50
+
enum E { one };
51
+
enum E size() const;
52
+
boolempty() const;
53
+
};
54
+
55
+
classContainer {
56
+
public:
57
+
intsize() const;
58
+
boolempty() const;
59
+
};
60
+
61
+
classDerived : publicContainer {
62
+
};
63
+
30
64
intmain() {
31
65
std::set<int> intSet;
32
66
std::string str;
@@ -39,6 +73,7 @@ int main() {
39
73
;
40
74
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
41
75
// CHECK-FIXES: {{^ }}if (intSet.empty()){{$}}
76
+
// CHECK-MESSAGES: :23:8: note: method 'set<int>'::empty() defined here
42
77
if (str.size() == 0)
43
78
;
44
79
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
@@ -127,6 +162,116 @@ int main() {
127
162
;
128
163
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
129
164
// CHECK-FIXES: {{^ }}if (vect4.empty()){{$}}
165
+
166
+
TemplatedContainer<void> templated_container;
167
+
if (templated_container.size() == 0)
168
+
;
169
+
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
284
+
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
140
285
// CHECK-FIXES: {{^ }}if (!v.empty()){{$}}
141
286
// CHECK-FIXES-NEXT: ;
142
287
CHECKSIZE(v);
143
288
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
144
289
// CHECK-MESSAGES: CHECKSIZE(v);
290
+
291
+
TemplatedContainer<T> templated_container;
292
+
if (templated_container.size())
293
+
;
294
+
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
0 commit comments