[PATCH] D19262: [clang-tidy] readability-container-size-empty fixes

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 19 06:14:15 PDT 2016


xazax.hun created this revision.
xazax.hun added reviewers: hokein, Eugene.Zelenko.
xazax.hun added subscribers: o.gyorgy, cfe-commits.

This patch fixes PR27410 and adds std::basic_string support.

http://reviews.llvm.org/D19262

Files:
  clang-tidy/readability/ContainerSizeEmptyCheck.cpp
  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
@@ -7,6 +7,15 @@
   bool empty() const;
 };
 
+template <typename T> struct basic_string {
+  basic_string();
+  unsigned long size() const;
+  bool empty() const;
+};
+
+typedef basic_string<char> string;
+typedef basic_string<wchar_t> wstring;
+
 inline namespace __v2 {
 template <typename T> struct set {
   set();
@@ -20,10 +29,24 @@
 
 int main() {
   std::set<int> intSet;
+  std::string str;
+  std::wstring wstr;
+  str.size() + 0;
+  str.size() - 0;
+  0 + str.size();
+  0 - str.size();
   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]
   // CHECK-FIXES: {{^  }}if (intSet.empty()){{$}}
+  if (str.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]
+  // CHECK-FIXES: {{^  }}if (str.empty()){{$}}
+  if (wstr.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]
+  // CHECK-FIXES: {{^  }}if (wstr.empty()){{$}}
   std::vector<int> vect;
   if (vect.size() == 0)
     ;
Index: clang-tidy/readability/ContainerSizeEmptyCheck.cpp
===================================================================
--- clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -16,6 +16,7 @@
 
 static bool isContainerName(llvm::StringRef ClassName) {
   static const char *const ContainerNames[] = {"array",
+                                               "basic_string",
                                                "deque",
                                                "forward_list",
                                                "list",
@@ -59,14 +60,13 @@
     return;
 
   const auto WrongUse = anyOf(
-      hasParent(
-          binaryOperator(
-              anyOf(has(integerLiteral(equals(0))),
-                    allOf(anyOf(hasOperatorName("<"), hasOperatorName(">="),
-                                hasOperatorName(">"), hasOperatorName("<=")),
-                          hasEitherOperand(
-                              ignoringImpCasts(integerLiteral(equals(1)))))))
-              .bind("SizeBinaryOp")),
+      hasParent(binaryOperator(
+                    anyOf(hasOperatorName("<"), hasOperatorName(">="),
+                          hasOperatorName(">"), hasOperatorName("<="),
+                          hasOperatorName("=="), hasOperatorName("!=")),
+                    hasEitherOperand(ignoringImpCasts(anyOf(
+                        integerLiteral(equals(1)), integerLiteral(equals(0))))))
+                    .bind("SizeBinaryOp")),
       hasParent(implicitCastExpr(
           hasImplicitDestinationType(booleanType()),
           anyOf(
@@ -122,6 +122,10 @@
     if (Value > 1)
       return;
 
+    if (Value == 1 && (OpCode == BinaryOperatorKind::BO_EQ ||
+                       OpCode == BinaryOperatorKind::BO_NE))
+      return;
+
     // Always true, no warnings for that.
     if ((OpCode == BinaryOperatorKind::BO_GE && Value == 0 && ContainerIsLHS) ||
         (OpCode == BinaryOperatorKind::BO_LE && Value == 0 && !ContainerIsLHS))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19262.54182.patch
Type: text/x-patch
Size: 3564 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160419/8a561b16/attachment.bin>


More information about the cfe-commits mailing list