xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp (revision 429e5be768c21d208ab688f8dfa1399c04ec5626)
1ffad4f8fSFelix // RUN: %check_clang_tidy -std=c++14-or-later %s readability-container-size-empty %t -- \
21af159e9SPiotr Zegar // RUN: -config="{CheckOptions: {readability-container-size-empty.ExcludedComparisonTypes: '::std::array;::IgnoredDummyType'}}" \
30a93abd9SMike Crowe // RUN: -- -fno-delayed-template-parsing -isystem %clang_tidy_headers
40a93abd9SMike Crowe #include <string>
589a1d03eSRichard 
689a1d03eSRichard namespace std {
789a1d03eSRichard template <typename T> struct vector {
889a1d03eSRichard   vector();
989a1d03eSRichard   bool operator==(const vector<T>& other) const;
1089a1d03eSRichard   bool operator!=(const vector<T>& other) const;
1189a1d03eSRichard   unsigned long size() const;
1289a1d03eSRichard   bool empty() const;
1389a1d03eSRichard };
1489a1d03eSRichard 
1589a1d03eSRichard inline namespace __v2 {
1689a1d03eSRichard template <typename T> struct set {
1789a1d03eSRichard   set();
1889a1d03eSRichard   bool operator==(const set<T>& other) const;
1989a1d03eSRichard   bool operator!=(const set<T>& other) const;
2089a1d03eSRichard   unsigned long size() const;
2189a1d03eSRichard   bool empty() const;
2289a1d03eSRichard };
2389a1d03eSRichard }
2489a1d03eSRichard 
254001ae17SFelix namespace string_literals{
26ffad4f8fSFelix string operator""s(const char *, size_t);
274001ae17SFelix }
284001ae17SFelix 
2989a1d03eSRichard }
3089a1d03eSRichard 
3189a1d03eSRichard template <typename T>
3289a1d03eSRichard class TemplatedContainer {
3389a1d03eSRichard public:
3489a1d03eSRichard   bool operator==(const TemplatedContainer<T>& other) const;
3589a1d03eSRichard   bool operator!=(const TemplatedContainer<T>& other) const;
367f1d757fSPiotr Zegar   unsigned long size() const;
3789a1d03eSRichard   bool empty() const;
3889a1d03eSRichard };
3989a1d03eSRichard 
4089a1d03eSRichard template <typename T>
4189a1d03eSRichard class PrivateEmpty {
4289a1d03eSRichard public:
4389a1d03eSRichard   bool operator==(const PrivateEmpty<T>& other) const;
4489a1d03eSRichard   bool operator!=(const PrivateEmpty<T>& other) const;
457f1d757fSPiotr Zegar   unsigned long size() const;
4689a1d03eSRichard private:
4789a1d03eSRichard   bool empty() const;
4889a1d03eSRichard };
4989a1d03eSRichard 
5089a1d03eSRichard struct BoolSize {
5189a1d03eSRichard   bool size() const;
5289a1d03eSRichard   bool empty() const;
5389a1d03eSRichard };
5489a1d03eSRichard 
5589a1d03eSRichard struct EnumSize {
5689a1d03eSRichard   enum E { one };
5789a1d03eSRichard   enum E size() const;
5889a1d03eSRichard   bool empty() const;
5989a1d03eSRichard };
6089a1d03eSRichard 
6189a1d03eSRichard class Container {
6289a1d03eSRichard public:
6389a1d03eSRichard   bool operator==(const Container& other) const;
647f1d757fSPiotr Zegar   unsigned long size() const;
6589a1d03eSRichard   bool empty() const;
6689a1d03eSRichard };
6789a1d03eSRichard 
6889a1d03eSRichard class Derived : public Container {
6989a1d03eSRichard };
7089a1d03eSRichard 
7189a1d03eSRichard class Container2 {
7289a1d03eSRichard public:
737f1d757fSPiotr Zegar   unsigned long size() const;
empty() const7489a1d03eSRichard   bool empty() const { return size() == 0; }
7589a1d03eSRichard };
7689a1d03eSRichard 
7789a1d03eSRichard class Container3 {
7889a1d03eSRichard public:
797f1d757fSPiotr Zegar   unsigned long size() const;
8089a1d03eSRichard   bool empty() const;
8189a1d03eSRichard };
8289a1d03eSRichard 
empty() const8389a1d03eSRichard bool Container3::empty() const { return this->size() == 0; }
8489a1d03eSRichard 
8589a1d03eSRichard class Container4 {
8689a1d03eSRichard public:
8789a1d03eSRichard   bool operator==(const Container4& rhs) const;
887f1d757fSPiotr Zegar   unsigned long size() const;
empty() const8989a1d03eSRichard   bool empty() const { return *this == Container4(); }
9089a1d03eSRichard };
9189a1d03eSRichard 
9289a1d03eSRichard struct Lazy {
sizeLazy9389a1d03eSRichard   constexpr unsigned size() const { return 0; }
emptyLazy9489a1d03eSRichard   constexpr bool empty() const { return true; }
9589a1d03eSRichard };
9689a1d03eSRichard 
s_func()9789a1d03eSRichard std::string s_func() {
9889a1d03eSRichard   return std::string();
9989a1d03eSRichard }
10089a1d03eSRichard 
takesBool(bool)10189a1d03eSRichard void takesBool(bool)
10289a1d03eSRichard {
10389a1d03eSRichard 
10489a1d03eSRichard }
10589a1d03eSRichard 
returnsBool()10689a1d03eSRichard bool returnsBool() {
10789a1d03eSRichard   std::set<int> intSet;
10889a1d03eSRichard   std::string str;
10989a1d03eSRichard   std::string str2;
11089a1d03eSRichard   std::wstring wstr;
11189a1d03eSRichard   (void)(str.size() + 0);
112efebb4e0SPiotr Zegar   (void)(str.length() + 0);
11389a1d03eSRichard   (void)(str.size() - 0);
114efebb4e0SPiotr Zegar   (void)(str.length() - 0);
11589a1d03eSRichard   (void)(0 + str.size());
116efebb4e0SPiotr Zegar   (void)(0 + str.length());
11789a1d03eSRichard   (void)(0 - str.size());
118efebb4e0SPiotr Zegar   (void)(0 - str.length());
11989a1d03eSRichard   if (intSet.size() == 0)
12089a1d03eSRichard     ;
12189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
12289a1d03eSRichard   // CHECK-FIXES: {{^  }}if (intSet.empty()){{$}}
1230a93abd9SMike Crowe   // CHECK-MESSAGES: :21:8: note: method 'set'::empty() defined here
12489a1d03eSRichard   if (intSet == std::set<int>())
12589a1d03eSRichard     ;
12689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness
12789a1d03eSRichard   // CHECK-FIXES: {{^  }}if (intSet.empty()){{$}}
1280a93abd9SMike Crowe   // CHECK-MESSAGES: :21:8: note: method 'set'::empty() defined here
12989a1d03eSRichard   if (s_func() == "")
13089a1d03eSRichard     ;
13189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
13289a1d03eSRichard   // CHECK-FIXES: {{^  }}if (s_func().empty()){{$}}
13389a1d03eSRichard   if (str.size() == 0)
13489a1d03eSRichard     ;
135efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
136efebb4e0SPiotr Zegar   // CHECK-FIXES: {{^  }}if (str.empty()){{$}}
137efebb4e0SPiotr Zegar   if (str.length() == 0)
138efebb4e0SPiotr Zegar     ;
139efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'length'
14089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (str.empty()){{$}}
14189a1d03eSRichard   if ((str + str2).size() == 0)
14289a1d03eSRichard     ;
143efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
144efebb4e0SPiotr Zegar   // CHECK-FIXES: {{^  }}if ((str + str2).empty()){{$}}
145efebb4e0SPiotr Zegar   if ((str + str2).length() == 0)
146efebb4e0SPiotr Zegar     ;
147efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'length'
14889a1d03eSRichard   // CHECK-FIXES: {{^  }}if ((str + str2).empty()){{$}}
14989a1d03eSRichard   if (str == "")
15089a1d03eSRichard     ;
15189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
15289a1d03eSRichard   // CHECK-FIXES: {{^  }}if (str.empty()){{$}}
15389a1d03eSRichard   if (str + str2 == "")
15489a1d03eSRichard     ;
15589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
15689a1d03eSRichard   // CHECK-FIXES: {{^  }}if ((str + str2).empty()){{$}}
15789a1d03eSRichard   if (wstr.size() == 0)
15889a1d03eSRichard     ;
159efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
160efebb4e0SPiotr Zegar   // CHECK-FIXES: {{^  }}if (wstr.empty()){{$}}
161efebb4e0SPiotr Zegar   if (wstr.length() == 0)
162efebb4e0SPiotr Zegar     ;
163efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'length'
16489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (wstr.empty()){{$}}
1650a93abd9SMike Crowe   if (wstr == L"")
16689a1d03eSRichard     ;
16789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
16889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (wstr.empty()){{$}}
16989a1d03eSRichard   std::vector<int> vect;
17089a1d03eSRichard   if (vect.size() == 0)
17189a1d03eSRichard     ;
172efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size'
17389a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect.empty()){{$}}
17489a1d03eSRichard   if (vect == std::vector<int>())
17589a1d03eSRichard     ;
17689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
17789a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect.empty()){{$}}
17889a1d03eSRichard   if (vect.size() != 0)
17989a1d03eSRichard     ;
18089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
18189a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
18289a1d03eSRichard   if (vect != std::vector<int>())
18389a1d03eSRichard     ;
18489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
18589a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
18689a1d03eSRichard   if (0 == vect.size())
18789a1d03eSRichard     ;
18889a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: the 'empty' method should be used
18989a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect.empty()){{$}}
19089a1d03eSRichard   if (0 != vect.size())
19189a1d03eSRichard     ;
19289a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: the 'empty' method should be used
19389a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
19489a1d03eSRichard   if (std::vector<int>() == vect)
19589a1d03eSRichard     ;
19689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
19789a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect.empty()){{$}}
19889a1d03eSRichard   if (std::vector<int>() != vect)
19989a1d03eSRichard     ;
20089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
20189a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
20289a1d03eSRichard   if (vect.size() > 0)
20389a1d03eSRichard     ;
20489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
20589a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
20689a1d03eSRichard   if (0 < vect.size())
20789a1d03eSRichard     ;
20889a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: the 'empty' method should be used
20989a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
21089a1d03eSRichard   if (vect.size() < 1)
21189a1d03eSRichard     ;
21289a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
21389a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect.empty()){{$}}
21489a1d03eSRichard   if (1 > vect.size())
21589a1d03eSRichard     ;
21689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: the 'empty' method should be used
21789a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect.empty()){{$}}
21889a1d03eSRichard   if (vect.size() >= 1)
21989a1d03eSRichard     ;
22089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
22189a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
22289a1d03eSRichard   if (1 <= vect.size())
22389a1d03eSRichard     ;
22489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: the 'empty' method should be used
22589a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
22689a1d03eSRichard   if (vect.size() > 1) // no warning
22789a1d03eSRichard     ;
22889a1d03eSRichard   if (1 < vect.size()) // no warning
22989a1d03eSRichard     ;
23089a1d03eSRichard   if (vect.size() <= 1) // no warning
23189a1d03eSRichard     ;
23289a1d03eSRichard   if (1 >= vect.size()) // no warning
23389a1d03eSRichard     ;
23489a1d03eSRichard   if (!vect.size())
23589a1d03eSRichard     ;
23689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: the 'empty' method should be used
23789a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect.empty()){{$}}
23889a1d03eSRichard   if (vect.size())
23989a1d03eSRichard     ;
24089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
24189a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect.empty()){{$}}
24289a1d03eSRichard 
24389a1d03eSRichard   if (vect.empty())
24489a1d03eSRichard     ;
24589a1d03eSRichard 
24689a1d03eSRichard   const std::vector<int> vect2;
24789a1d03eSRichard   if (vect2.size() != 0)
24889a1d03eSRichard     ;
24989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
25089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!vect2.empty()){{$}}
25189a1d03eSRichard 
25289a1d03eSRichard   std::vector<int> *vect3 = new std::vector<int>();
25389a1d03eSRichard   if (vect3->size() == 0)
25489a1d03eSRichard     ;
25589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
25689a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect3->empty()){{$}}
25789a1d03eSRichard   if ((*vect3).size() == 0)
25889a1d03eSRichard     ;
25989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
26089a1d03eSRichard   // CHECK-FIXES: {{^  }}if ((*vect3).empty()){{$}}
26189a1d03eSRichard   if ((*vect3) == std::vector<int>())
26289a1d03eSRichard     ;
26389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
26489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect3->empty()){{$}}
26589a1d03eSRichard   if (*vect3 == std::vector<int>())
26689a1d03eSRichard     ;
26789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
26889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect3->empty()){{$}}
26989a1d03eSRichard 
27089a1d03eSRichard   delete vect3;
27189a1d03eSRichard 
27289a1d03eSRichard   const std::vector<int> &vect4 = vect2;
27389a1d03eSRichard   if (vect4.size() == 0)
27489a1d03eSRichard     ;
27589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
27689a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect4.empty()){{$}}
27789a1d03eSRichard   if (vect4 == std::vector<int>())
27889a1d03eSRichard     ;
27989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
28089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (vect4.empty()){{$}}
28189a1d03eSRichard 
28289a1d03eSRichard   TemplatedContainer<void> templated_container;
28389a1d03eSRichard   if (templated_container.size() == 0)
28489a1d03eSRichard     ;
28589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
28689a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
28789a1d03eSRichard   if (templated_container == TemplatedContainer<void>())
28889a1d03eSRichard     ;
28989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
29089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
29189a1d03eSRichard   if (templated_container.size() != 0)
29289a1d03eSRichard     ;
29389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
29489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
29589a1d03eSRichard   if (templated_container != TemplatedContainer<void>())
29689a1d03eSRichard     ;
29789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
29889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
29989a1d03eSRichard   if (0 == templated_container.size())
30089a1d03eSRichard     ;
30189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: the 'empty' method should be used
30289a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
30389a1d03eSRichard   if (TemplatedContainer<void>() == templated_container)
30489a1d03eSRichard     ;
30589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
30689a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
30789a1d03eSRichard   if (0 != templated_container.size())
30889a1d03eSRichard     ;
30989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: the 'empty' method should be used
31089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
31189a1d03eSRichard   if (TemplatedContainer<void>() != templated_container)
31289a1d03eSRichard     ;
31389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
31489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
31589a1d03eSRichard   if (templated_container.size() > 0)
31689a1d03eSRichard     ;
31789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
31889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
31989a1d03eSRichard   if (0 < templated_container.size())
32089a1d03eSRichard     ;
32189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: the 'empty' method should be used
32289a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
32389a1d03eSRichard   if (templated_container.size() < 1)
32489a1d03eSRichard     ;
32589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
32689a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
32789a1d03eSRichard   if (1 > templated_container.size())
32889a1d03eSRichard     ;
32989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: the 'empty' method should be used
33089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
33189a1d03eSRichard   if (templated_container.size() >= 1)
33289a1d03eSRichard     ;
33389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
33489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
33589a1d03eSRichard   if (1 <= templated_container.size())
33689a1d03eSRichard     ;
33789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: the 'empty' method should be used
33889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
33989a1d03eSRichard   if (templated_container.size() > 1) // no warning
34089a1d03eSRichard     ;
34189a1d03eSRichard   if (1 < templated_container.size()) // no warning
34289a1d03eSRichard     ;
34389a1d03eSRichard   if (templated_container.size() <= 1) // no warning
34489a1d03eSRichard     ;
34589a1d03eSRichard   if (1 >= templated_container.size()) // no warning
34689a1d03eSRichard     ;
34789a1d03eSRichard   if (!templated_container.size())
34889a1d03eSRichard     ;
34989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: the 'empty' method should be used
35089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
35189a1d03eSRichard   if (templated_container.size())
35289a1d03eSRichard     ;
35389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
35489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
35589a1d03eSRichard 
35689a1d03eSRichard   if (templated_container.empty())
35789a1d03eSRichard     ;
35889a1d03eSRichard 
35989a1d03eSRichard   // No warnings expected.
36089a1d03eSRichard   PrivateEmpty<void> private_empty;
36189a1d03eSRichard   if (private_empty.size() == 0)
36289a1d03eSRichard     ;
36389a1d03eSRichard   if (private_empty == PrivateEmpty<void>())
36489a1d03eSRichard     ;
36589a1d03eSRichard   if (private_empty.size() != 0)
36689a1d03eSRichard     ;
36789a1d03eSRichard   if (private_empty != PrivateEmpty<void>())
36889a1d03eSRichard     ;
36989a1d03eSRichard   if (0 == private_empty.size())
37089a1d03eSRichard     ;
37189a1d03eSRichard   if (PrivateEmpty<void>() == private_empty)
37289a1d03eSRichard     ;
37389a1d03eSRichard   if (0 != private_empty.size())
37489a1d03eSRichard     ;
37589a1d03eSRichard   if (PrivateEmpty<void>() != private_empty)
37689a1d03eSRichard     ;
37789a1d03eSRichard   if (private_empty.size() > 0)
37889a1d03eSRichard     ;
37989a1d03eSRichard   if (0 < private_empty.size())
38089a1d03eSRichard     ;
38189a1d03eSRichard   if (private_empty.size() < 1)
38289a1d03eSRichard     ;
38389a1d03eSRichard   if (1 > private_empty.size())
38489a1d03eSRichard     ;
38589a1d03eSRichard   if (private_empty.size() >= 1)
38689a1d03eSRichard     ;
38789a1d03eSRichard   if (1 <= private_empty.size())
38889a1d03eSRichard     ;
38989a1d03eSRichard   if (private_empty.size() > 1)
39089a1d03eSRichard     ;
39189a1d03eSRichard   if (1 < private_empty.size())
39289a1d03eSRichard     ;
39389a1d03eSRichard   if (private_empty.size() <= 1)
39489a1d03eSRichard     ;
39589a1d03eSRichard   if (1 >= private_empty.size())
39689a1d03eSRichard     ;
39789a1d03eSRichard   if (!private_empty.size())
39889a1d03eSRichard     ;
39989a1d03eSRichard   if (private_empty.size())
40089a1d03eSRichard     ;
40189a1d03eSRichard 
40289a1d03eSRichard   // Types with weird size() return type.
40389a1d03eSRichard   BoolSize bs;
40489a1d03eSRichard   if (bs.size() == 0)
40589a1d03eSRichard     ;
40689a1d03eSRichard   EnumSize es;
40789a1d03eSRichard   if (es.size() == 0)
40889a1d03eSRichard     ;
40989a1d03eSRichard 
41089a1d03eSRichard   Derived derived;
41189a1d03eSRichard   if (derived.size() == 0)
41289a1d03eSRichard     ;
41389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
41489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (derived.empty()){{$}}
41589a1d03eSRichard   if (derived == Derived())
41689a1d03eSRichard     ;
41789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
41889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (derived.empty()){{$}}
41989a1d03eSRichard 
42089a1d03eSRichard   takesBool(derived.size());
42189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
42289a1d03eSRichard   // CHECK-FIXES: {{^  }}takesBool(!derived.empty());
42389a1d03eSRichard 
42489a1d03eSRichard   takesBool(derived.size() == 0);
42589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
42689a1d03eSRichard   // CHECK-FIXES: {{^  }}takesBool(derived.empty());
42789a1d03eSRichard 
42889a1d03eSRichard   takesBool(derived.size() != 0);
42989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
43089a1d03eSRichard   // CHECK-FIXES: {{^  }}takesBool(!derived.empty());
43189a1d03eSRichard 
43289a1d03eSRichard   bool b1 = derived.size();
43389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
43489a1d03eSRichard   // CHECK-FIXES: {{^  }}bool b1 = !derived.empty();
43589a1d03eSRichard 
43689a1d03eSRichard   bool b2(derived.size());
43789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the 'empty' method should be used
43889a1d03eSRichard   // CHECK-FIXES: {{^  }}bool b2(!derived.empty());
43989a1d03eSRichard 
44089a1d03eSRichard   auto b3 = static_cast<bool>(derived.size());
44189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: the 'empty' method should be used
44289a1d03eSRichard   // CHECK-FIXES: {{^  }}auto b3 = static_cast<bool>(!derived.empty());
44389a1d03eSRichard 
44489a1d03eSRichard   auto b4 = (bool)derived.size();
44589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: the 'empty' method should be used
44689a1d03eSRichard   // CHECK-FIXES: {{^  }}auto b4 = (bool)!derived.empty();
44789a1d03eSRichard 
44889a1d03eSRichard   auto b5 = bool(derived.size());
44989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: the 'empty' method should be used
45089a1d03eSRichard   // CHECK-FIXES: {{^  }}auto b5 = bool(!derived.empty());
45189a1d03eSRichard 
45289a1d03eSRichard   return derived.size();
45389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
45489a1d03eSRichard   // CHECK-FIXES: {{^  }}return !derived.empty();
45589a1d03eSRichard }
45689a1d03eSRichard 
45789a1d03eSRichard class ConstructWithBoolField {
45889a1d03eSRichard   bool B;
45989a1d03eSRichard public:
ConstructWithBoolField(const std::vector<int> & C)46089a1d03eSRichard   ConstructWithBoolField(const std::vector<int> &C) : B(C.size()) {}
46189a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:57: warning: the 'empty' method should be used
4620a93abd9SMike Crowe // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
46389a1d03eSRichard // CHECK-FIXES: {{^  }}ConstructWithBoolField(const std::vector<int> &C) : B(!C.empty()) {}
46489a1d03eSRichard };
46589a1d03eSRichard 
46689a1d03eSRichard struct StructWithNSDMI {
46789a1d03eSRichard   std::vector<int> C;
46889a1d03eSRichard   bool B = C.size();
46989a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: the 'empty' method should be used
4700a93abd9SMike Crowe // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
47189a1d03eSRichard // CHECK-FIXES: {{^  }}bool B = !C.empty();
47289a1d03eSRichard };
47389a1d03eSRichard 
func(const std::vector<int> & C)47489a1d03eSRichard int func(const std::vector<int> &C) {
47589a1d03eSRichard   return C.size() ? 0 : 1;
47689a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
4770a93abd9SMike Crowe // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
47889a1d03eSRichard // CHECK-FIXES: {{^  }}return !C.empty() ? 0 : 1;
47989a1d03eSRichard }
48089a1d03eSRichard 
48189a1d03eSRichard constexpr Lazy L;
48289a1d03eSRichard static_assert(!L.size(), "");
48389a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: the 'empty' method should be used
4844001ae17SFelix // CHECK-MESSAGES: :94:18: note: method 'Lazy'::empty() defined here
48589a1d03eSRichard // CHECK-FIXES: {{^}}static_assert(L.empty(), "");
48689a1d03eSRichard 
48789a1d03eSRichard struct StructWithLazyNoexcept {
48889a1d03eSRichard   void func() noexcept(L.size());
48989a1d03eSRichard };
49089a1d03eSRichard 
49189a1d03eSRichard #define CHECKSIZE(x) if (x.size()) {}
49289a1d03eSRichard // CHECK-FIXES: #define CHECKSIZE(x) if (x.size()) {}
49389a1d03eSRichard 
f()49489a1d03eSRichard template <typename T> void f() {
49589a1d03eSRichard   std::vector<T> v;
49689a1d03eSRichard   if (v.size())
49789a1d03eSRichard     ;
49889a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
4990a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
50089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!v.empty()){{$}}
50189a1d03eSRichard   if (v == std::vector<T>())
50289a1d03eSRichard     ;
50389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object [readability-container-size-empty]
50489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (v.empty()){{$}}
50589a1d03eSRichard   // CHECK-FIXES-NEXT: ;
50689a1d03eSRichard   CHECKSIZE(v);
50789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
5080a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
50989a1d03eSRichard   // CHECK-FIXES: CHECKSIZE(v);
51089a1d03eSRichard 
51189a1d03eSRichard   TemplatedContainer<T> templated_container;
51289a1d03eSRichard   if (templated_container.size())
51389a1d03eSRichard     ;
51489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
5154001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
51689a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
51789a1d03eSRichard   if (templated_container != TemplatedContainer<T>())
51889a1d03eSRichard     ;
51989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
5204001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
52189a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
52289a1d03eSRichard   // CHECK-FIXES-NEXT: ;
52389a1d03eSRichard   CHECKSIZE(templated_container);
52489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
5254001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
52689a1d03eSRichard   // CHECK-FIXES: CHECKSIZE(templated_container);
527efebb4e0SPiotr Zegar   std::basic_string<T> s;
528efebb4e0SPiotr Zegar   if (s.size())
529efebb4e0SPiotr Zegar     ;
530efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
531bc8cff1dSNicolas van Kempen   // CHECK-MESSAGES: string:{{[0-9]+}}:8: note: method 'basic_string'::empty() defined here
532efebb4e0SPiotr Zegar   // CHECK-FIXES: {{^  }}if (!s.empty()){{$}}
533efebb4e0SPiotr Zegar   if (s.length())
534efebb4e0SPiotr Zegar     ;
535efebb4e0SPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty]
536bc8cff1dSNicolas van Kempen   // CHECK-MESSAGES: string:{{[0-9]+}}:8: note: method 'basic_string'::empty() defined here
537efebb4e0SPiotr Zegar   // CHECK-FIXES: {{^  }}if (!s.empty()){{$}}
53889a1d03eSRichard }
53989a1d03eSRichard 
g()54089a1d03eSRichard void g() {
54189a1d03eSRichard   f<int>();
54289a1d03eSRichard   f<double>();
54389a1d03eSRichard   f<char *>();
54489a1d03eSRichard }
54589a1d03eSRichard 
54689a1d03eSRichard template <typename T>
neverInstantiatedTemplate()54789a1d03eSRichard bool neverInstantiatedTemplate() {
54889a1d03eSRichard   std::vector<T> v;
54989a1d03eSRichard   if (v.size())
55089a1d03eSRichard     ;
55189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
5520a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
55389a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!v.empty()){{$}}
55489a1d03eSRichard 
55589a1d03eSRichard   if (v == std::vector<T>())
55689a1d03eSRichard     ;
55789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object [readability-container-size-empty]
55889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (v.empty()){{$}}
55989a1d03eSRichard   // CHECK-FIXES-NEXT: ;
56089a1d03eSRichard   if (v.size() == 0)
56189a1d03eSRichard     ;
56289a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
5630a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
56489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (v.empty()){{$}}
56589a1d03eSRichard   if (v.size() != 0)
56689a1d03eSRichard     ;
56789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
5680a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
56989a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!v.empty()){{$}}
57089a1d03eSRichard   if (v.size() < 1)
57189a1d03eSRichard     ;
57289a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
5730a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
57489a1d03eSRichard   // CHECK-FIXES: {{^  }}if (v.empty()){{$}}
57589a1d03eSRichard   if (v.size() > 0)
57689a1d03eSRichard     ;
57789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
5780a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
57989a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!v.empty()){{$}}
58089a1d03eSRichard   if (v.size() == 1)
58189a1d03eSRichard     ;
58289a1d03eSRichard   if (v.size() != 1)
58389a1d03eSRichard     ;
58489a1d03eSRichard   if (v.size() == 2)
58589a1d03eSRichard     ;
58689a1d03eSRichard   if (v.size() != 2)
58789a1d03eSRichard     ;
58889a1d03eSRichard 
58989a1d03eSRichard   if (static_cast<bool>(v.size()))
59089a1d03eSRichard     ;
59189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:25: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
5920a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
59389a1d03eSRichard   // CHECK-FIXES: {{^  }}if (static_cast<bool>(!v.empty())){{$}}
59489a1d03eSRichard   if (v.size() && false)
59589a1d03eSRichard     ;
59689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
5970a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
59889a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!v.empty() && false){{$}}
59989a1d03eSRichard   if (!v.size())
60089a1d03eSRichard     ;
60189a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
6020a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
60389a1d03eSRichard   // CHECK-FIXES: {{^  }}if (v.empty()){{$}}
60489a1d03eSRichard 
60589a1d03eSRichard   TemplatedContainer<T> templated_container;
60689a1d03eSRichard   if (templated_container.size())
60789a1d03eSRichard     ;
60889a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
6094001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
61089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
61189a1d03eSRichard   if (templated_container != TemplatedContainer<T>())
61289a1d03eSRichard     ;
61389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
6144001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
61589a1d03eSRichard   // CHECK-FIXES: {{^  }}if (!templated_container.empty()){{$}}
61689a1d03eSRichard   // CHECK-FIXES-NEXT: ;
61789a1d03eSRichard   while (templated_container.size())
61889a1d03eSRichard     ;
61989a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:10: warning: the 'empty' method should be used
6204001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
62189a1d03eSRichard   // CHECK-FIXES: {{^  }}while (!templated_container.empty()){{$}}
62289a1d03eSRichard 
62389a1d03eSRichard   do {
62489a1d03eSRichard   }
62589a1d03eSRichard   while (templated_container.size());
62689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
6274001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
62889a1d03eSRichard   // CHECK-FIXES: {{^  }}while (!templated_container.empty());
62989a1d03eSRichard 
63089a1d03eSRichard   for (; templated_container.size();)
63189a1d03eSRichard     ;
63289a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:10: warning: the 'empty' method should be used
6334001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
63489a1d03eSRichard   // CHECK-FIXES: {{^  }}for (; !templated_container.empty();){{$}}
63589a1d03eSRichard 
63689a1d03eSRichard   if (true && templated_container.size())
63789a1d03eSRichard     ;
63889a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: the 'empty' method should be used
6394001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
64089a1d03eSRichard   // CHECK-FIXES: {{^  }}if (true && !templated_container.empty()){{$}}
64189a1d03eSRichard 
64289a1d03eSRichard   if (true || templated_container.size())
64389a1d03eSRichard     ;
64489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: the 'empty' method should be used
6454001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
64689a1d03eSRichard   // CHECK-FIXES: {{^  }}if (true || !templated_container.empty()){{$}}
64789a1d03eSRichard 
64889a1d03eSRichard   if (!templated_container.size())
64989a1d03eSRichard     ;
65089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: the 'empty' method should be used
6514001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
65289a1d03eSRichard   // CHECK-FIXES: {{^  }}if (templated_container.empty()){{$}}
65389a1d03eSRichard 
65489a1d03eSRichard   bool b1 = templated_container.size();
65589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: the 'empty' method should be used
6564001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
65789a1d03eSRichard   // CHECK-FIXES: {{^  }}bool b1 = !templated_container.empty();
65889a1d03eSRichard 
65989a1d03eSRichard   bool b2(templated_container.size());
66089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the 'empty' method should be used
6614001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
66289a1d03eSRichard   // CHECK-FIXES: {{^  }}bool b2(!templated_container.empty());
66389a1d03eSRichard 
66489a1d03eSRichard   auto b3 = static_cast<bool>(templated_container.size());
66589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: the 'empty' method should be used
6664001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
66789a1d03eSRichard   // CHECK-FIXES: {{^  }}auto b3 = static_cast<bool>(!templated_container.empty());
66889a1d03eSRichard 
66989a1d03eSRichard   auto b4 = (bool)templated_container.size();
67089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: the 'empty' method should be used
6714001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
67289a1d03eSRichard   // CHECK-FIXES: {{^  }}auto b4 = (bool)!templated_container.empty();
67389a1d03eSRichard 
67489a1d03eSRichard   auto b5 = bool(templated_container.size());
67589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: the 'empty' method should be used
6764001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
67789a1d03eSRichard   // CHECK-FIXES: {{^  }}auto b5 = bool(!templated_container.empty());
67889a1d03eSRichard 
67989a1d03eSRichard   takesBool(templated_container.size());
68089a1d03eSRichard   // We don't detect this one because we don't know the parameter of takesBool
68189a1d03eSRichard   // until the type of templated_container.size() is known
68289a1d03eSRichard 
68389a1d03eSRichard   return templated_container.size();
68489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
6854001ae17SFelix   // CHECK-MESSAGES: :37:8: note: method 'TemplatedContainer'::empty() defined here
68689a1d03eSRichard   // CHECK-FIXES: {{^  }}return !templated_container.empty();
68789a1d03eSRichard }
68889a1d03eSRichard 
68989a1d03eSRichard template <typename TypeRequiresSize>
instantiatedTemplateWithSizeCall()69089a1d03eSRichard void instantiatedTemplateWithSizeCall() {
69189a1d03eSRichard   TypeRequiresSize t;
69289a1d03eSRichard   // The instantiation of the template with std::vector<int> should not
69389a1d03eSRichard   // result in changing the template, because we don't know that
69489a1d03eSRichard   // TypeRequiresSize generally has `.empty()`
69589a1d03eSRichard   if (t.size())
69689a1d03eSRichard     ;
69789a1d03eSRichard 
69889a1d03eSRichard   if (t == TypeRequiresSize{})
69989a1d03eSRichard     ;
70089a1d03eSRichard 
70189a1d03eSRichard   if (t != TypeRequiresSize{})
70289a1d03eSRichard     ;
70389a1d03eSRichard }
70489a1d03eSRichard 
70589a1d03eSRichard class TypeWithSize {
70689a1d03eSRichard public:
70789a1d03eSRichard   TypeWithSize();
70889a1d03eSRichard   bool operator==(const TypeWithSize &other) const;
70989a1d03eSRichard   bool operator!=(const TypeWithSize &other) const;
71089a1d03eSRichard 
size() const71189a1d03eSRichard   unsigned size() const { return 0; }
71289a1d03eSRichard   // Does not have `.empty()`
71389a1d03eSRichard };
71489a1d03eSRichard 
instantiator()71589a1d03eSRichard void instantiator() {
71689a1d03eSRichard   instantiatedTemplateWithSizeCall<TypeWithSize>();
71789a1d03eSRichard   instantiatedTemplateWithSizeCall<std::vector<int>>();
71889a1d03eSRichard }
71989a1d03eSRichard 
72089a1d03eSRichard namespace std {
72189a1d03eSRichard template <typename T>
72289a1d03eSRichard struct unique_ptr {
72389a1d03eSRichard   T *operator->() const;
72489a1d03eSRichard   T &operator*() const;
72589a1d03eSRichard };
72689a1d03eSRichard } // namespace std
72789a1d03eSRichard 
call_through_unique_ptr(const std::unique_ptr<std::vector<int>> & ptr)72889a1d03eSRichard bool call_through_unique_ptr(const std::unique_ptr<std::vector<int>> &ptr) {
72989a1d03eSRichard   return ptr->size() > 0;
73089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
7310a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
73289a1d03eSRichard   // CHECK-FIXES: {{^  }}return !ptr->empty();
73389a1d03eSRichard }
73489a1d03eSRichard 
call_through_unique_ptr_deref(const std::unique_ptr<std::vector<int>> & ptr)73589a1d03eSRichard bool call_through_unique_ptr_deref(const std::unique_ptr<std::vector<int>> &ptr) {
73689a1d03eSRichard   return (*ptr).size() > 0;
73789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
7380a93abd9SMike Crowe   // CHECK-MESSAGES: :12:8: note: method 'vector'::empty() defined here
73989a1d03eSRichard   // CHECK-FIXES: {{^  }}return !(*ptr).empty();
74089a1d03eSRichard }
7417dc410cbSHaojian Wu 
7427dc410cbSHaojian Wu struct TypedefSize {
7437dc410cbSHaojian Wu   typedef int size_type;
7447dc410cbSHaojian Wu   size_type size() const;
7457dc410cbSHaojian Wu   bool empty() const;
7467dc410cbSHaojian Wu };
747e08cc52cSPiotr Zegar 
testTypedefSize()748e08cc52cSPiotr Zegar void testTypedefSize() {
7497dc410cbSHaojian Wu   TypedefSize ts;
7507dc410cbSHaojian Wu   if (ts.size() == 0)
7517dc410cbSHaojian Wu     ;
7527dc410cbSHaojian Wu   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
7537dc410cbSHaojian Wu   // CHECK-FIXES: {{^  }}if (ts.empty()){{$}}
7547dc410cbSHaojian Wu }
755e08cc52cSPiotr Zegar 
756e08cc52cSPiotr Zegar namespace std {
757e08cc52cSPiotr Zegar 
758e08cc52cSPiotr Zegar template <typename T, unsigned long N> struct array {
759e08cc52cSPiotr Zegar   bool operator==(const array& other) const;
760e08cc52cSPiotr Zegar   bool operator!=(const array& other) const;
sizestd::array761e08cc52cSPiotr Zegar   unsigned long size() const { return N; }
emptystd::array762e08cc52cSPiotr Zegar   bool empty() const { return N != 0U; }
763e08cc52cSPiotr Zegar 
764e08cc52cSPiotr Zegar   T data[N];
765e08cc52cSPiotr Zegar };
766e08cc52cSPiotr Zegar 
767e08cc52cSPiotr Zegar }
768e08cc52cSPiotr Zegar 
769e08cc52cSPiotr Zegar struct DummyType {
770e08cc52cSPiotr Zegar   bool operator==(const DummyType&) const;
771e08cc52cSPiotr Zegar   unsigned long size() const;
772e08cc52cSPiotr Zegar   bool empty() const;
773e08cc52cSPiotr Zegar };
774e08cc52cSPiotr Zegar 
775e08cc52cSPiotr Zegar struct IgnoredDummyType {
776e08cc52cSPiotr Zegar   bool operator==(const IgnoredDummyType&) const;
777e08cc52cSPiotr Zegar   unsigned long size() const;
778e08cc52cSPiotr Zegar   bool empty() const;
779e08cc52cSPiotr Zegar };
780e08cc52cSPiotr Zegar 
781e08cc52cSPiotr Zegar typedef std::array<int, 10U> Array;
782e08cc52cSPiotr Zegar 
testArraySize(const Array & value)783e08cc52cSPiotr Zegar bool testArraySize(const Array& value) {
784e08cc52cSPiotr Zegar   return value.size() == 0U;
785e08cc52cSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
786e08cc52cSPiotr Zegar // CHECK-FIXES: {{^  }}return value.empty();{{$}}
787efebb4e0SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-25]]:8: note: method 'array'::empty() defined here
788e08cc52cSPiotr Zegar }
789e08cc52cSPiotr Zegar 
testArrayCompareToEmpty(const Array & value)790e08cc52cSPiotr Zegar bool testArrayCompareToEmpty(const Array& value) {
791e08cc52cSPiotr Zegar   return value == std::array<int, 10U>();
792e08cc52cSPiotr Zegar }
793e08cc52cSPiotr Zegar 
testDummyType(const DummyType & value)794e08cc52cSPiotr Zegar bool testDummyType(const DummyType& value) {
795e08cc52cSPiotr Zegar   return value == DummyType();
796e08cc52cSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object [readability-container-size-empty]
797e08cc52cSPiotr Zegar // CHECK-FIXES: {{^  }}return value.empty();{{$}}
798efebb4e0SPiotr Zegar // CHECK-MESSAGES: :[[@LINE-26]]:8: note: method 'DummyType'::empty() defined here
799e08cc52cSPiotr Zegar }
800e08cc52cSPiotr Zegar 
testIgnoredDummyType(const IgnoredDummyType & value)801e08cc52cSPiotr Zegar bool testIgnoredDummyType(const IgnoredDummyType& value) {
802e08cc52cSPiotr Zegar   return value == IgnoredDummyType();
803e08cc52cSPiotr Zegar }
8044001ae17SFelix 
testStringLiterals(const std::string & s)8054001ae17SFelix bool testStringLiterals(const std::string& s)
8064001ae17SFelix {
8074001ae17SFelix   using namespace std::string_literals;
808ffad4f8fSFelix   return s == ""s;
8094001ae17SFelix   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
8104001ae17SFelix   // CHECK-FIXES: {{^  }}return s.empty()
8114001ae17SFelix }
8124001ae17SFelix 
testNotEmptyStringLiterals(const std::string & s)8134001ae17SFelix bool testNotEmptyStringLiterals(const std::string& s)
8144001ae17SFelix {
8154001ae17SFelix   using namespace std::string_literals;
816ffad4f8fSFelix   return s == "foo"s;
8174001ae17SFelix }
8187f1d757fSPiotr Zegar 
8197f1d757fSPiotr Zegar namespace PR72619 {
8207f1d757fSPiotr Zegar   struct SS {
8217f1d757fSPiotr Zegar     bool empty() const;
8227f1d757fSPiotr Zegar     int size() const;
8237f1d757fSPiotr Zegar   };
8247f1d757fSPiotr Zegar 
8257f1d757fSPiotr Zegar   struct SU {
8267f1d757fSPiotr Zegar     bool empty() const;
8277f1d757fSPiotr Zegar     unsigned size() const;
8287f1d757fSPiotr Zegar   };
8297f1d757fSPiotr Zegar 
f(const SU & s)8307f1d757fSPiotr Zegar   void f(const SU& s) {
8317f1d757fSPiotr Zegar     if (s.size() < 0) {}
8327f1d757fSPiotr Zegar     if (0 > s.size()) {}
8337f1d757fSPiotr Zegar     if (s.size() >= 0) {}
8347f1d757fSPiotr Zegar     if (0 <= s.size()) {}
8357f1d757fSPiotr Zegar     if (s.size() < 1)
8367f1d757fSPiotr Zegar       ;
8377f1d757fSPiotr Zegar     // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
8387f1d757fSPiotr Zegar     // CHECK-FIXES: {{^    }}if (s.empty()){{$}}
8397f1d757fSPiotr Zegar     if (1 > s.size())
8407f1d757fSPiotr Zegar       ;
8417f1d757fSPiotr Zegar     // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
8427f1d757fSPiotr Zegar     // CHECK-FIXES: {{^    }}if (s.empty()){{$}}
8437f1d757fSPiotr Zegar     if (s.size() <= 0)
8447f1d757fSPiotr Zegar       ;
8457f1d757fSPiotr Zegar     // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
8467f1d757fSPiotr Zegar     // CHECK-FIXES: {{^    }}if (s.empty()){{$}}
8477f1d757fSPiotr Zegar     if (0 >= s.size())
8487f1d757fSPiotr Zegar       ;
8497f1d757fSPiotr Zegar     // CHECK-MESSAGES: :[[@LINE-2]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
8507f1d757fSPiotr Zegar     // CHECK-FIXES: {{^    }}if (s.empty()){{$}}
8517f1d757fSPiotr Zegar   }
8527f1d757fSPiotr Zegar 
f(const SS & s)8537f1d757fSPiotr Zegar   void f(const SS& s) {
8547f1d757fSPiotr Zegar     if (s.size() < 0) {}
8557f1d757fSPiotr Zegar     if (0 > s.size()) {}
8567f1d757fSPiotr Zegar     if (s.size() >= 0) {}
8577f1d757fSPiotr Zegar     if (0 <= s.size()) {}
8587f1d757fSPiotr Zegar     if (s.size() < 1) {}
8597f1d757fSPiotr Zegar     if (1 > s.size()) {}
8607f1d757fSPiotr Zegar     if (s.size() <= 0) {}
8617f1d757fSPiotr Zegar     if (0 >= s.size()) {}
8627f1d757fSPiotr Zegar   }
8637f1d757fSPiotr Zegar }
86457da0407SPiotr Zegar 
86557da0407SPiotr Zegar namespace PR88203 {
86657da0407SPiotr Zegar   struct SS {
86757da0407SPiotr Zegar     bool empty() const;
86857da0407SPiotr Zegar     int size() const;
86957da0407SPiotr Zegar     int length(int) const;
87057da0407SPiotr Zegar   };
87157da0407SPiotr Zegar 
87257da0407SPiotr Zegar   struct SU {
87357da0407SPiotr Zegar     bool empty() const;
87457da0407SPiotr Zegar     int size(int) const;
87557da0407SPiotr Zegar     int length() const;
87657da0407SPiotr Zegar   };
87757da0407SPiotr Zegar 
f(const SS & s)87857da0407SPiotr Zegar   void f(const SS& s) {
87957da0407SPiotr Zegar     if (0 == s.length(1)) {}
88057da0407SPiotr Zegar     if (0 == s.size()) {}
88157da0407SPiotr Zegar     // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
88257da0407SPiotr Zegar     // CHECK-FIXES: {{^    }}if (s.empty()) {}{{$}}
88357da0407SPiotr Zegar   }
88457da0407SPiotr Zegar 
f(const SU & s)88557da0407SPiotr Zegar   void f(const SU& s) {
88657da0407SPiotr Zegar     if (0 == s.size(1)) {}
88757da0407SPiotr Zegar     if (0 == s.length()) {}
88857da0407SPiotr Zegar     // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty]
88957da0407SPiotr Zegar     // CHECK-FIXES: {{^    }}if (s.empty()) {}{{$}}
89057da0407SPiotr Zegar   }
89157da0407SPiotr Zegar }
892*429e5be7SPiotr Zegar 
893*429e5be7SPiotr Zegar namespace PR94454 {
894*429e5be7SPiotr Zegar   template <char...>
operator ""_ci()895*429e5be7SPiotr Zegar   int operator""_ci() { return 0; }
896*429e5be7SPiotr Zegar   auto eq = 0_ci == 0;
897*429e5be7SPiotr Zegar }
898