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

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 8 04:24:59 PDT 2022


hokein created this revision.
hokein added reviewers: aaron.ballman, ymandel.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
hokein requested review of this revision.
Herald added a project: clang-tools-extra.

With 15f3cd6bfc670ba6106184a903eb04be059e5977 <https://reviews.llvm.org/rG15f3cd6bfc670ba6106184a903eb04be059e5977>, 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
    ;


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131390

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


Index: clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
@@ -718,3 +718,16 @@
   // 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()){{$}}
+}
Index: clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -86,6 +86,9 @@
 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 @@
 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()))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131390.450765.patch
Type: text/x-patch
Size: 2189 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220808/791b7b53/attachment.bin>


More information about the cfe-commits mailing list