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 @@ -164,12 +164,23 @@ hasUnaryOperand( expr(hasType(pointsTo(ValidContainer))).bind("Pointee"))), expr(hasType(ValidContainer)).bind("STLObject")); + + const auto StdArrayType = qualType(anyOf( + hasDeclaration(cxxRecordDecl(hasName("::std::array")).bind("array")), + hasCanonicalType(hasDeclaration( + cxxRecordDecl(hasName("::std::array")).bind("array"))))); + const auto SameStdArrayType = + qualType(anyOf(hasDeclaration(cxxRecordDecl(equalsBoundNode("array"))), + hasCanonicalType(hasDeclaration( + cxxRecordDecl(equalsBoundNode("array")))))); + Finder->addMatcher( binaryOperation(hasAnyOperatorName("==", "!="), - hasOperands(WrongComparend, - STLArg), - unless(hasAncestor(cxxMethodDecl( - ofClass(equalsBoundNode("container")))))) + hasOperands(WrongComparend, STLArg), + unless(allOf(hasLHS(hasType(StdArrayType)), + hasRHS(hasType(SameStdArrayType)))), + unless(hasAncestor(cxxMethodDecl( + ofClass(equalsBoundNode("container")))))) .bind("BinCmp"), this); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -130,6 +130,10 @@ Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Fixed a false positive in :doc:`readability-container-size-empty + ` check object of + ``std::array`` type were compared to the default constructed ``std::array``. + Removed checks ^^^^^^^^^^^^^^ 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 --- 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 @@ -724,10 +724,37 @@ size_type size() const; bool empty() const; }; -void test() { + +void testTypedefSize() { TypedefSize ts; if (ts.size() == 0) ; // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used // CHECK-FIXES: {{^ }}if (ts.empty()){{$}} } + +namespace std { + +template struct array { + bool operator==(const array& other) const; + bool operator!=(const array& other) const; + unsigned long size() const { return N; } + bool empty() const { return N != 0U; } + + T data[N]; +}; + +} + +typedef std::array Array; + +bool testArraySize(const Array& value) { + return value.size() == 0U; +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty] +// CHECK-FIXES: {{^ }}return value.empty();{{$}} +// CHECK-MESSAGES: :742:8: note: method 'array'::empty() defined here +} + +bool testArrayCompareToEmptye(const Array& value) { + return value == std::array(); +}