aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPiotr Zegar <me@piotrzegar.pl>2024-05-30 15:56:04 +0200
committerGitHub <noreply@github.com>2024-05-30 15:56:04 +0200
commit57da0407c44b187feed40b02cdfd24d71df755cc (patch)
treef4b910afdc4f82e51ff5452d444c753da85a2acb
parent1bf1f93d94cb395e04329b17a4fcff65b4ff8122 (diff)
downloadllvm-57da0407c44b187feed40b02cdfd24d71df755cc.zip
llvm-57da0407c44b187feed40b02cdfd24d71df755cc.tar.gz
llvm-57da0407c44b187feed40b02cdfd24d71df755cc.tar.bz2
[clang-tidy] Check number of arguments to size/length in readability-container-size-empty (#93724)
Verify that size/length methods are called with no arguments. Closes #88203
-rw-r--r--clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp4
-rw-r--r--clang-tools-extra/docs/ReleaseNotes.rst4
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp28
3 files changed, 35 insertions, 1 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
index 19307b4..bbc1b47 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -150,6 +150,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
cxxMemberCallExpr(
+ argumentCountIs(0),
on(expr(anyOf(hasType(ValidContainer),
hasType(pointsTo(ValidContainer)),
hasType(references(ValidContainer))))
@@ -163,7 +164,8 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
this);
Finder->addMatcher(
- callExpr(has(cxxDependentScopeMemberExpr(
+ callExpr(argumentCountIs(0),
+ has(cxxDependentScopeMemberExpr(
hasObjectExpression(
expr(anyOf(hasType(ValidContainer),
hasType(pointsTo(ValidContainer)),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a5e87d2..4f674d1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -368,6 +368,10 @@ Changes in existing checks
<clang-tidy/checks/readability/const-return-type>` check to eliminate false
positives when returning types with const not at the top level.
+- Improved :doc:`readability-container-size-empty
+ <clang-tidy/checks/readability/container-size-empty>` check to prevent false
+ positives when utilizing ``size`` or ``length`` methods that accept parameter.
+
- Improved :doc:`readability-duplicate-include
<clang-tidy/checks/readability/duplicate-include>` check by excluding include
directives that form the filename using macro.
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 84bdbd5..ecaf97f 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
@@ -861,3 +861,31 @@ namespace PR72619 {
if (0 >= s.size()) {}
}
}
+
+namespace PR88203 {
+ struct SS {
+ bool empty() const;
+ int size() const;
+ int length(int) const;
+ };
+
+ struct SU {
+ bool empty() const;
+ int size(int) const;
+ int length() const;
+ };
+
+ void f(const SS& s) {
+ if (0 == s.length(1)) {}
+ if (0 == s.size()) {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
+ // CHECK-FIXES: {{^ }}if (s.empty()) {}{{$}}
+ }
+
+ void f(const SU& s) {
+ if (0 == s.size(1)) {}
+ if (0 == s.length()) {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty]
+ // CHECK-FIXES: {{^ }}if (s.empty()) {}{{$}}
+ }
+}