diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h --- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h +++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h @@ -33,6 +33,9 @@ } void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + llvm::Optional getCheckTraversalKind() const override { + return TK_IgnoreUnlessSpelledInSource; + } }; } // namespace readability diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -16,6 +16,11 @@ using namespace clang::ast_matchers; namespace clang { +namespace ast_matchers { +AST_MATCHER(CXXConstructExpr, isDefaultConstruction) { + return Node.getConstructor()->isDefaultConstructor(); +} +} // namespace ast_matchers namespace tidy { namespace readability { @@ -48,27 +53,15 @@ const auto ValidContainer = qualType( anyOf(ValidContainerNonTemplateType, ValidContainerTemplateType)); - const auto WrongUse = traverse( - ast_type_traits::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()))), - hasParent(explicitCastExpr(hasDestinationType(booleanType()))), - hasParent(ifStmt()), hasParent(whileStmt()), - hasParent(binaryOperator(hasAnyOperatorName("&&", "||"))), - hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")))); + const auto WrongUse = anyOf( + hasParent(ifStmt()), hasParent(whileStmt()), + hasParent(binaryOperator(isComparisonOperator()).bind("SizeBinaryOp")), + hasParent(binaryOperator(hasAnyOperatorName("&&", "||"))), + hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")), + hasParent(explicitCastExpr(hasDestinationType(booleanType())))); Finder->addMatcher( - cxxMemberCallExpr(unless(isInTemplateInstantiation()), - on(expr(anyOf(hasType(ValidContainer), + cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer), hasType(pointsTo(ValidContainer)), hasType(references(ValidContainer)))) .bind("MemberCallObject")), @@ -92,19 +85,10 @@ .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)))), - cxxUnresolvedConstructExpr(argummentCountIs(0))); + stringLiteral(hasSize(0)), cxxConstructExpr(isDefaultConstruction()), + cxxUnresolvedConstructExpr(argumentCountIs(0))); // Match the object being compared. const auto STLArg = anyOf(unaryOperator( @@ -114,7 +98,6 @@ expr(hasType(ValidContainer)).bind("STLObject")); Finder->addMatcher( cxxOperatorCallExpr( - unless(isInTemplateInstantiation()), hasAnyOverloadedOperatorName("==", "!="), anyOf(allOf(hasArgument(0, WrongComparend), hasArgument(1, STLArg)), allOf(hasArgument(0, STLArg), hasArgument(1, WrongComparend))),