Skip to content

[clang-tidy] Check number of arguments to size/length in readability-container-size-empty #93724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand All @@ -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)),
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {}{{$}}
}
}
Loading