[clang-tools-extra] [clang-tidy] Ignore default ctor with user provided argument in `readability-container-size-empty` (PR #154782)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 23 05:03:52 PDT 2025
https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/154782
>From a2a7894dca6ec009974e5a25bfd11ce40a228d98 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Thu, 21 Aug 2025 23:06:31 +0800
Subject: [PATCH 1/3] [clang-tidy] Ignore default ctor with user provided
argument in `readability-container-size-empty`
---
.../readability/ContainerSizeEmptyCheck.cpp | 2 +-
clang-tools-extra/docs/ReleaseNotes.rst | 3 ++-
.../readability/container-size-empty.cpp | 24 +++++++++++++++++++
3 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
index c4dc319f23c38..c5df94dae50b1 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -190,7 +190,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
const auto WrongComparend =
anyOf(stringLiteral(hasSize(0)),
userDefinedLiteral(hasLiteral(stringLiteral(hasSize(0)))),
- cxxConstructExpr(isDefaultConstruction()),
+ cxxConstructExpr(isDefaultConstruction(), argumentCountIs(0)),
cxxUnresolvedConstructExpr(argumentCountIs(0)));
// Match the object being compared.
const auto STLArg =
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 388979d9577ba..adf1c039916ff 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -211,7 +211,8 @@ Changes in existing checks
- Improved :doc:`readability-container-size-empty
<clang-tidy/checks/readability/container-size-empty>` check by correctly
- generating fix-it hints when size method is called from implicit ``this``.
+ generating fix-it hints when size method is called from implicit ``this``
+ and ignoring default constructors with user provided arguments.
- Improved :doc:`readability-identifier-naming
<clang-tidy/checks/readability/identifier-naming>` check by ignoring
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 b1e68672a3a9a..48a3ca1726f39 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
@@ -908,3 +908,27 @@ class foo : public std::string{
};
}
+
+namespace GH154762 {
+class TypeRange {
+ std::vector<int> b;
+
+public:
+ TypeRange(std::vector<int> b = {});
+ TypeRange(int);
+ bool operator==(const TypeRange& other) const;
+
+ size_t size() const {
+ return b.size();
+ }
+
+ bool empty() const {
+ return size() == 0;
+ }
+};
+
+void foo(std::vector<int> v) {
+ if (TypeRange(1) == TypeRange(v)) { // no warning
+ }
+}
+}
>From e62b59163de940a86e22234bd9d4bbb6922264ce Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Fri, 22 Aug 2025 23:26:41 +0800
Subject: [PATCH 2/3] Remove isDefaultConstruction's use and matcher
---
.../clang-tidy/readability/ContainerSizeEmptyCheck.cpp | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
index c5df94dae50b1..4c1bef6d0799a 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -89,10 +89,6 @@ AST_MATCHER(Expr, usedInBooleanContext) {
return Result;
}
-AST_MATCHER(CXXConstructExpr, isDefaultConstruction) {
- return Node.getConstructor()->isDefaultConstructor();
-}
-
AST_MATCHER(QualType, isIntegralType) {
return Node->isIntegralType(Finder->getASTContext());
}
@@ -190,7 +186,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
const auto WrongComparend =
anyOf(stringLiteral(hasSize(0)),
userDefinedLiteral(hasLiteral(stringLiteral(hasSize(0)))),
- cxxConstructExpr(isDefaultConstruction(), argumentCountIs(0)),
+ cxxConstructExpr(argumentCountIs(0)),
cxxUnresolvedConstructExpr(argumentCountIs(0)));
// Match the object being compared.
const auto STLArg =
>From 0764238576502aa8a3088d2f860d3aa5bcd66220 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 23 Aug 2025 20:03:41 +0800
Subject: [PATCH 3/3] [NFC] Add more testcases
---
.../checkers/readability/container-size-empty.cpp | 10 ++++++++++
1 file changed, 10 insertions(+)
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 48a3ca1726f39..e9a01a1783f30 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
@@ -930,5 +930,15 @@ class TypeRange {
void foo(std::vector<int> v) {
if (TypeRange(1) == TypeRange(v)) { // no warning
}
+
+ if (TypeRange(1) == TypeRange()) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object
+ // CHECK-FIXES: if (TypeRange(1).empty()) {
+ }
+
+ if (TypeRange(v) == TypeRange()) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object
+ // CHECK-FIXES: if (TypeRange(v).empty()) {
+ }
}
}
More information about the cfe-commits
mailing list