File tree Expand file tree Collapse file tree 3 files changed +34
-2
lines changed Expand file tree Collapse file tree 3 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -191,10 +191,17 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
191
191
std::string ReplacementText = std::string (
192
192
Lexer::getSourceText (CharSourceRange::getTokenRange (E->getSourceRange ()),
193
193
*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))) {
195
197
ReplacementText = " (" + ReplacementText + " )" ;
196
198
}
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 ())
198
205
ReplacementText += " ->empty()" ;
199
206
else
200
207
ReplacementText += " .empty()" ;
Original file line number Diff line number Diff line change @@ -173,6 +173,9 @@ Changes in existing checks
173
173
- Fixed nonsensical suggestion of :doc: `altera-struct-pack-align
174
174
<clang-tidy/checks/altera-struct-pack-align>` check for empty structs.
175
175
176
+ - Fixed incorrect suggestions for :doc: `readability-container-size-empty
177
+ <clang-tidy/checks/readability-container-size-empty>` when smart pointers are involved.
178
+
176
179
Removed checks
177
180
^^^^^^^^^^^^^^
178
181
Original file line number Diff line number Diff line change @@ -696,3 +696,25 @@ void instantiator() {
696
696
instantiatedTemplateWithSizeCall<TypeWithSize>();
697
697
instantiatedTemplateWithSizeCall<std::vector<int >>();
698
698
}
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
+ }
You can’t perform that action at this time.
0 commit comments