[clang-tools-extra] r255431 - [clang-tidy] Fix PR25812.

Gabor Horvath via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 12 03:31:35 PST 2015


Author: xazax
Date: Sat Dec 12 05:31:25 2015
New Revision: 255431

URL: http://llvm.org/viewvc/llvm-project?rev=255431&view=rev
Log:
[clang-tidy] Fix PR25812.

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp?rev=255431&r1=255430&r2=255431&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp Sat Dec 12 05:31:25 2015
@@ -15,24 +15,22 @@
 using namespace clang::ast_matchers;
 
 static bool isContainer(llvm::StringRef ClassName) {
-  static const char *const ContainerNames[] = {
-    "std::array",
-    "std::deque",
-    "std::forward_list",
-    "std::list",
-    "std::map",
-    "std::multimap",
-    "std::multiset",
-    "std::priority_queue",
-    "std::queue",
-    "std::set",
-    "std::stack",
-    "std::unordered_map",
-    "std::unordered_multimap",
-    "std::unordered_multiset",
-    "std::unordered_set",
-    "std::vector"
-  };
+  static const char *const ContainerNames[] = {"std::array",
+                                               "std::deque",
+                                               "std::forward_list",
+                                               "std::list",
+                                               "std::map",
+                                               "std::multimap",
+                                               "std::multiset",
+                                               "std::priority_queue",
+                                               "std::queue",
+                                               "std::set",
+                                               "std::stack",
+                                               "std::unordered_map",
+                                               "std::unordered_multimap",
+                                               "std::unordered_multiset",
+                                               "std::unordered_set",
+                                               "std::vector"};
   return std::binary_search(std::begin(ContainerNames),
                             std::end(ContainerNames), ClassName);
 }
@@ -65,7 +63,8 @@ void ContainerSizeEmptyCheck::registerMa
               anyOf(has(integerLiteral(equals(0))),
                     allOf(anyOf(hasOperatorName("<"), hasOperatorName(">="),
                                 hasOperatorName(">"), hasOperatorName("<=")),
-                          hasEitherOperand(integerLiteral(equals(1))))))
+                          hasEitherOperand(
+                              ignoringImpCasts(integerLiteral(equals(1)))))))
               .bind("SizeBinaryOp")),
       hasParent(implicitCastExpr(
           hasImplicitDestinationType(isBoolType()),
@@ -101,19 +100,21 @@ void ContainerSizeEmptyCheck::check(cons
 
   if (BinaryOp) { // Determine the correct transformation.
     bool Negation = false;
-    const bool ContainerIsLHS = !llvm::isa<IntegerLiteral>(BinaryOp->getLHS());
+    const bool ContainerIsLHS =
+        !llvm::isa<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts());
     const auto OpCode = BinaryOp->getOpcode();
     uint64_t Value = 0;
     if (ContainerIsLHS) {
-      if (const auto *Literal =
-              llvm::dyn_cast<IntegerLiteral>(BinaryOp->getRHS()))
+      if (const auto *Literal = llvm::dyn_cast<IntegerLiteral>(
+              BinaryOp->getRHS()->IgnoreImpCasts()))
         Value = Literal->getValue().getLimitedValue();
       else
         return;
     } else {
-      Value = llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS())
-                  ->getValue()
-                  .getLimitedValue();
+      Value =
+          llvm::dyn_cast<IntegerLiteral>(BinaryOp->getLHS()->IgnoreImpCasts())
+              ->getValue()
+              .getLimitedValue();
     }
 
     // Constant that is not handled.

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp?rev=255431&r1=255430&r2=255431&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp Sat Dec 12 05:31:25 2015
@@ -3,7 +3,7 @@
 namespace std {
 template <typename T> struct vector {
   vector() {}
-  int size() const {}
+  unsigned long size() const {}
   bool empty() const {}
 };
 }




More information about the cfe-commits mailing list