Skip to content

Commit 57da040

Browse files
authored
[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
1 parent 1bf1f93 commit 57da040

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
150150

151151
Finder->addMatcher(
152152
cxxMemberCallExpr(
153+
argumentCountIs(0),
153154
on(expr(anyOf(hasType(ValidContainer),
154155
hasType(pointsTo(ValidContainer)),
155156
hasType(references(ValidContainer))))
@@ -163,7 +164,8 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
163164
this);
164165

165166
Finder->addMatcher(
166-
callExpr(has(cxxDependentScopeMemberExpr(
167+
callExpr(argumentCountIs(0),
168+
has(cxxDependentScopeMemberExpr(
167169
hasObjectExpression(
168170
expr(anyOf(hasType(ValidContainer),
169171
hasType(pointsTo(ValidContainer)),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ Changes in existing checks
368368
<clang-tidy/checks/readability/const-return-type>` check to eliminate false
369369
positives when returning types with const not at the top level.
370370

371+
- Improved :doc:`readability-container-size-empty
372+
<clang-tidy/checks/readability/container-size-empty>` check to prevent false
373+
positives when utilizing ``size`` or ``length`` methods that accept parameter.
374+
371375
- Improved :doc:`readability-duplicate-include
372376
<clang-tidy/checks/readability/duplicate-include>` check by excluding include
373377
directives that form the filename using macro.

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,31 @@ namespace PR72619 {
861861
if (0 >= s.size()) {}
862862
}
863863
}
864+
865+
namespace PR88203 {
866+
struct SS {
867+
bool empty() const;
868+
int size() const;
869+
int length(int) const;
870+
};
871+
872+
struct SU {
873+
bool empty() const;
874+
int size(int) const;
875+
int length() const;
876+
};
877+
878+
void f(const SS& s) {
879+
if (0 == s.length(1)) {}
880+
if (0 == s.size()) {}
881+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
882+
// CHECK-FIXES: {{^ }}if (s.empty()) {}{{$}}
883+
}
884+
885+
void f(const SU& s) {
886+
if (0 == s.size(1)) {}
887+
if (0 == s.length()) {}
888+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty]
889+
// CHECK-FIXES: {{^ }}if (s.empty()) {}{{$}}
890+
}
891+
}

0 commit comments

Comments
 (0)