Skip to content

Commit 7dc410c

Browse files
committed
[clang-tidy] Fix a regression of readability-container-size-empty after the AST ElaboratedType change.
With 15f3cd6, 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
1 parent bce3da9 commit 7dc410c

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ AST_MATCHER(Expr, usedInBooleanContext) {
8686
AST_MATCHER(CXXConstructExpr, isDefaultConstruction) {
8787
return Node.getConstructor()->isDefaultConstructor();
8888
}
89+
AST_MATCHER(QualType, isIntegralType) {
90+
return Node->isIntegralType(Finder->getASTContext());
91+
}
8992
} // namespace ast_matchers
9093
namespace tidy {
9194
namespace readability {
@@ -99,10 +102,9 @@ ContainerSizeEmptyCheck::ContainerSizeEmptyCheck(StringRef Name,
99102
void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
100103
const auto ValidContainerRecord = cxxRecordDecl(isSameOrDerivedFrom(
101104
namedDecl(
102-
has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(),
103-
hasName("size"),
104-
returns(qualType(isInteger(), unless(booleanType()),
105-
unless(elaboratedType()))))
105+
has(cxxMethodDecl(
106+
isConst(), parameterCountIs(0), isPublic(), hasName("size"),
107+
returns(qualType(isIntegralType(), unless(booleanType()))))
106108
.bind("size")),
107109
has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(),
108110
hasName("empty"), returns(booleanType()))

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,16 @@ bool call_through_unique_ptr_deref(const std::unique_ptr<std::vector<int>> &ptr)
718718
// CHECK-MESSAGES: :9:8: note: method 'vector'::empty() defined here
719719
// CHECK-FIXES: {{^ }}return !(*ptr).empty();
720720
}
721+
722+
struct TypedefSize {
723+
typedef int size_type;
724+
size_type size() const;
725+
bool empty() const;
726+
};
727+
void test() {
728+
TypedefSize ts;
729+
if (ts.size() == 0)
730+
;
731+
// CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should be used
732+
// CHECK-FIXES: {{^ }}if (ts.empty()){{$}}
733+
}

0 commit comments

Comments
 (0)