[clang-tools-extra] 2cba22c - [clang-tidy] Simplify implementation of container-size-empty

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 5 06:04:07 PST 2021


Author: Stephen Kelly
Date: 2021-02-05T14:03:41Z
New Revision: 2cba22c23a769103f4f0c31968620a488f13b625

URL: https://github.com/llvm/llvm-project/commit/2cba22c23a769103f4f0c31968620a488f13b625
DIFF: https://github.com/llvm/llvm-project/commit/2cba22c23a769103f4f0c31968620a488f13b625.diff

LOG: [clang-tidy] Simplify implementation of container-size-empty

Use IgnoreUnlessSpelledInSource to make the matcher code smaller and
more visibly-related to the code.

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
    clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
    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 cd5e18913214..a1d8064d23a0 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -83,6 +83,9 @@ AST_MATCHER(Expr, usedInBooleanContext) {
   });
   return Result;
 }
+AST_MATCHER(CXXConstructExpr, isDefaultConstruction) {
+  return Node.getConstructor()->isDefaultConstructor();
+}
 } // namespace ast_matchers
 namespace tidy {
 namespace readability {
@@ -116,24 +119,16 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
   const auto ValidContainer = qualType(
       anyOf(ValidContainerNonTemplateType, ValidContainerTemplateType));
 
-  const auto WrongUse = traverse(
-      TK_AsIs,
-      anyOf(
-          hasParent(binaryOperator(isComparisonOperator(),
-                                   hasEitherOperand(ignoringImpCasts(
-                                       anyOf(integerLiteral(equals(1)),
-                                             integerLiteral(equals(0))))))
-                        .bind("SizeBinaryOp")),
-          hasParent(implicitCastExpr(
-              hasImplicitDestinationType(booleanType()),
-              anyOf(hasParent(
-                        unaryOperator(hasOperatorName("!")).bind("NegOnSize")),
-                    anything()))),
-          usedInBooleanContext()));
+  const auto WrongUse =
+      anyOf(hasParent(binaryOperator(
+                          isComparisonOperator(),
+                          hasEitherOperand(anyOf(integerLiteral(equals(1)),
+                                                 integerLiteral(equals(0)))))
+                          .bind("SizeBinaryOp")),
+            usedInBooleanContext());
 
   Finder->addMatcher(
-      cxxMemberCallExpr(unless(isInTemplateInstantiation()),
-                        on(expr(anyOf(hasType(ValidContainer),
+      cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer),
                                       hasType(pointsTo(ValidContainer)),
                                       hasType(references(ValidContainer))))
                                .bind("MemberCallObject")),
@@ -157,18 +152,9 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
           .bind("SizeCallExpr"),
       this);
 
-  // Empty constructor matcher.
-  const auto DefaultConstructor = cxxConstructExpr(
-          hasDeclaration(cxxConstructorDecl(isDefaultConstructor())));
   // Comparison to empty string or empty constructor.
   const auto WrongComparend = anyOf(
-      ignoringImpCasts(stringLiteral(hasSize(0))),
-      ignoringImpCasts(cxxBindTemporaryExpr(has(DefaultConstructor))),
-      ignoringImplicit(DefaultConstructor),
-      cxxConstructExpr(hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
-                       has(expr(ignoringImpCasts(DefaultConstructor)))),
-      cxxConstructExpr(hasDeclaration(cxxConstructorDecl(isMoveConstructor())),
-                       has(expr(ignoringImpCasts(DefaultConstructor)))),
+      stringLiteral(hasSize(0)), cxxConstructExpr(isDefaultConstruction()),
       cxxUnresolvedConstructExpr(argumentCountIs(0)));
   // Match the object being compared.
   const auto STLArg =
@@ -178,12 +164,11 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
                     expr(hasType(pointsTo(ValidContainer))).bind("Pointee"))),
             expr(hasType(ValidContainer)).bind("STLObject"));
   Finder->addMatcher(
-      binaryOperation(unless(isInTemplateInstantiation()),
-                      hasAnyOperatorName("==", "!="),
-                      hasOperands(ignoringParenImpCasts(WrongComparend),
-                                  ignoringParenImpCasts(STLArg)),
-                      unless(hasAncestor(cxxMethodDecl(
-                          ofClass(equalsBoundNode("container"))))))
+      binaryOperation(hasAnyOperatorName("==", "!="),
+                      hasOperands(WrongComparend,
+                                  STLArg),
+                          unless(hasAncestor(cxxMethodDecl(
+                              ofClass(equalsBoundNode("container"))))))
           .bind("BinCmp"),
       this);
 }

diff  --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
index 8afff2265b1c..6a4236065f43 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
@@ -33,6 +33,9 @@ class ContainerSizeEmptyCheck : public ClangTidyCheck {
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 };
 
 } // namespace readability

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 4fe75f46932d..31dccd20427e 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
@@ -477,9 +477,6 @@ static_assert(!L.size(), "");
 
 struct StructWithLazyNoexcept {
   void func() noexcept(L.size());
-// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: the 'empty' method should be used
-// CHECK-MESSAGES: :101:18: note: method 'Lazy'::empty() defined here
-// CHECK-FIXES: {{^  }}void func() noexcept(!L.empty());
 };
 
 #define CHECKSIZE(x) if (x.size()) {}


        


More information about the cfe-commits mailing list