[clang-tools-extra] 7dc410c - [clang-tidy] Fix a regression of readability-container-size-empty after the AST ElaboratedType change.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 8 05:41:00 PDT 2022


Author: Haojian Wu
Date: 2022-08-08T14:40:36+02:00
New Revision: 7dc410cbff28270fcfcf7f2dd80f57ead7ab7d18

URL: https://github.com/llvm/llvm-project/commit/7dc410cbff28270fcfcf7f2dd80f57ead7ab7d18
DIFF: https://github.com/llvm/llvm-project/commit/7dc410cbff28270fcfcf7f2dd80f57ead7ab7d18.diff

LOG: [clang-tidy] Fix a regression of readability-container-size-empty after the AST ElaboratedType change.

With 15f3cd6bfc670ba6106184a903eb04be059e5977, we no longer emit a
diagnostic on a real std::vector case where the size method returns a
sugar `size_type`. This patch fixes it.

```
std::vector<int> v;
if (v.size() == 0) // => no check diagnostics
  ;
```

Differential Revision: https://reviews.llvm.org/D131390

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
index cc7a6834e3b6e..56556b515abd6 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -86,6 +86,9 @@ AST_MATCHER(Expr, usedInBooleanContext) {
 AST_MATCHER(CXXConstructExpr, isDefaultConstruction) {
   return Node.getConstructor()->isDefaultConstructor();
 }
+AST_MATCHER(QualType, isIntegralType) {
+  return Node->isIntegralType(Finder->getASTContext());
+}
 } // namespace ast_matchers
 namespace tidy {
 namespace readability {
@@ -99,10 +102,9 @@ ContainerSizeEmptyCheck::ContainerSizeEmptyCheck(StringRef Name,
 void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
   const auto ValidContainerRecord = cxxRecordDecl(isSameOrDerivedFrom(
       namedDecl(
-          has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(),
-                            hasName("size"),
-                            returns(qualType(isInteger(), unless(booleanType()),
-                                             unless(elaboratedType()))))
+          has(cxxMethodDecl(
+                  isConst(), parameterCountIs(0), isPublic(), hasName("size"),
+                  returns(qualType(isIntegralType(), unless(booleanType()))))
                   .bind("size")),
           has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(),
                             hasName("empty"), returns(booleanType()))

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
index 0759e0308e91a..3819c7fcbabe5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
@@ -718,3 +718,16 @@ bool call_through_unique_ptr_deref(const std::unique_ptr<std::vector<int>> &ptr)
   // CHECK-MESSAGES: :9:8: note: method 'vector'::empty() defined here
   // CHECK-FIXES: {{^  }}return !(*ptr).empty();
 }
+
+struct TypedefSize {
+  typedef int size_type;
+  size_type size() const;
+  bool empty() const;
+};
+void test() {
+  TypedefSize ts;
+  if (ts.size() == 0)
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
+  // CHECK-FIXES: {{^  }}if (ts.empty()){{$}}
+}


        


More information about the cfe-commits mailing list