[clang-tools-extra] b161e8d - [clang-tidy] Ignore default ctor with user provided argument in `readability-container-size-empty` (#154782)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 31 01:21:15 PDT 2025
Author: flovent
Date: 2025-08-31T11:21:11+03:00
New Revision: b161e8d7dff06d8fb410de897f1a3f0c561ec509
URL: https://github.com/llvm/llvm-project/commit/b161e8d7dff06d8fb410de897f1a3f0c561ec509
DIFF: https://github.com/llvm/llvm-project/commit/b161e8d7dff06d8fb410de897f1a3f0c561ec509.diff
LOG: [clang-tidy] Ignore default ctor with user provided argument in `readability-container-size-empty` (#154782)
Closes #154762
Added:
Modified:
clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
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 38c0330c02a7e..c3f8106c34dcb 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());
}
@@ -211,7 +207,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
const auto WrongComparend =
anyOf(stringLiteral(hasSize(0)),
userDefinedLiteral(hasLiteral(stringLiteral(hasSize(0)))),
- cxxConstructExpr(isDefaultConstruction()),
+ cxxConstructExpr(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 06641a602e28f..d83fb8b10ddba 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -232,8 +232,9 @@ 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``
- and adding detection in container's method except ``empty``.
+ generating fix-it hints when size method is called from implicit ``this``,
+ ignoring default constructors with user provided arguments and adding
+ detection in container's method except ``empty``.
- 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 decc9d2782218..6a673101c078a 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
@@ -909,6 +909,40 @@ 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
+ }
+
+ 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()) {
+ }
+}
+}
+
class ReportInContainerNonEmptyMethod {
public:
int size() const;
More information about the cfe-commits
mailing list