Skip to content

Commit fb3b3f7

Browse files
committed
[clang-tidy] Fix readability-container-size-empty check for smart pointers
Fixes #51118. Reviewed By: Sockke Differential Revision: https://reviews.llvm.org/D115124
1 parent f25935a commit fb3b3f7

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,17 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
191191
std::string ReplacementText = std::string(
192192
Lexer::getSourceText(CharSourceRange::getTokenRange(E->getSourceRange()),
193193
*Result.SourceManager, getLangOpts()));
194-
if (isBinaryOrTernary(E) || isa<UnaryOperator>(E)) {
194+
const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E);
195+
if (isBinaryOrTernary(E) || isa<UnaryOperator>(E) ||
196+
(OpCallExpr && (OpCallExpr->getOperator() == OO_Star))) {
195197
ReplacementText = "(" + ReplacementText + ")";
196198
}
197-
if (E->getType()->isPointerType())
199+
if (OpCallExpr &&
200+
OpCallExpr->getOperator() == OverloadedOperatorKind::OO_Arrow) {
201+
// This can happen if the object is a smart pointer. Don't add anything
202+
// because a '->' is already there (PR#51776), just call the method.
203+
ReplacementText += "empty()";
204+
} else if (E->getType()->isPointerType())
198205
ReplacementText += "->empty()";
199206
else
200207
ReplacementText += ".empty()";

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ Changes in existing checks
173173
- Fixed nonsensical suggestion of :doc:`altera-struct-pack-align
174174
<clang-tidy/checks/altera-struct-pack-align>` check for empty structs.
175175

176+
- Fixed incorrect suggestions for :doc:`readability-container-size-empty
177+
<clang-tidy/checks/readability-container-size-empty>` when smart pointers are involved.
178+
176179
Removed checks
177180
^^^^^^^^^^^^^^
178181

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,25 @@ void instantiator() {
696696
instantiatedTemplateWithSizeCall<TypeWithSize>();
697697
instantiatedTemplateWithSizeCall<std::vector<int>>();
698698
}
699+
700+
namespace std {
701+
template <typename T>
702+
struct unique_ptr {
703+
T *operator->() const;
704+
T &operator*() const;
705+
};
706+
} // namespace std
707+
708+
bool call_through_unique_ptr(const std::unique_ptr<std::vector<int>> &ptr) {
709+
return ptr->size() > 0;
710+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
711+
// CHECK-MESSAGES: :9:8: note: method 'vector'::empty() defined here
712+
// CHECK-FIXES: {{^ }}return !ptr->empty();
713+
}
714+
715+
bool call_through_unique_ptr_deref(const std::unique_ptr<std::vector<int>> &ptr) {
716+
return (*ptr).size() > 0;
717+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used
718+
// CHECK-MESSAGES: :9:8: note: method 'vector'::empty() defined here
719+
// CHECK-FIXES: {{^ }}return !(*ptr).empty();
720+
}

0 commit comments

Comments
 (0)