[PATCH] D56644: [clang-tidy] readability-container-size-empty handle std::string length()

Dmitry Venikov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 12 23:26:28 PST 2019


Quolyk created this revision.
Quolyk added reviewers: Eugene.Zelenko, omtcyfz, aaron.ballman, alexfh.
Herald added subscribers: cfe-commits, xazax.hun.

Extends readability-container-size-empty to check std::string length() similar to size().
Motivation: https://bugs.llvm.org/show_bug.cgi?id=38255.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D56644

Files:
  test/clang-tidy/readability-container-size-empty.cpp


Index: test/clang-tidy/readability-container-size-empty.cpp
===================================================================
--- test/clang-tidy/readability-container-size-empty.cpp
+++ test/clang-tidy/readability-container-size-empty.cpp
@@ -17,6 +17,7 @@
   bool operator!=(const char *) const;
   basic_string<T> operator+(const basic_string<T>& other) const;
   unsigned long size() const;
+  unsigned long length() const;
   bool empty() const;
 };
 
@@ -106,9 +107,13 @@
   std::string str2;
   std::wstring wstr;
   (void)(str.size() + 0);
+  (void)(str.length() + 0);
   (void)(str.size() - 0);
+  (void)(str.length() - 0);
   (void)(0 + str.size());
+  (void)(0 + str.length());
   (void)(0 - str.size());
+  (void)(0 - str.length());
   if (intSet.size() == 0)
     ;
   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
@@ -125,10 +130,14 @@
   // CHECK-FIXES: {{^  }}if (s_func().empty()){{$}}
   if (str.size() == 0)
     ;
+  if (str.length() == 0)
+    ;
   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
   // CHECK-FIXES: {{^  }}if (str.empty()){{$}}
   if ((str + str2).size() == 0)
     ;
+  if ((str + str2).length() == 0)
+    ;
   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
   // CHECK-FIXES: {{^  }}if ((str + str2).empty()){{$}}
   if (str == "")
@@ -141,6 +150,8 @@
   // CHECK-FIXES: {{^  }}if ((str + str2).empty()){{$}}
   if (wstr.size() == 0)
     ;
+  if (wstr.length() == 0)
+    ;
   // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
   // CHECK-FIXES: {{^  }}if (wstr.empty()){{$}}
   if (wstr == "")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56644.181463.patch
Type: text/x-patch
Size: 1736 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190113/7e8794f7/attachment-0001.bin>


More information about the cfe-commits mailing list