File tree Expand file tree Collapse file tree 3 files changed +25
-3
lines changed Expand file tree Collapse file tree 3 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -238,9 +238,12 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
238238 ? MemberCallObject
239239 : (Pointee ? Pointee : Result.Nodes .getNodeAs <Expr>(" STLObject" ));
240240 FixItHint Hint;
241- std::string ReplacementText = std::string (
242- Lexer::getSourceText (CharSourceRange::getTokenRange (E->getSourceRange ()),
243- *Result.SourceManager , getLangOpts ()));
241+ std::string ReplacementText =
242+ E->isImplicitCXXThis ()
243+ ? " "
244+ : std::string (Lexer::getSourceText (
245+ CharSourceRange::getTokenRange (E->getSourceRange ()),
246+ *Result.SourceManager , getLangOpts ()));
244247 const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E);
245248 if (isBinaryOrTernary (E) || isa<UnaryOperator>(E) ||
246249 (OpCallExpr && (OpCallExpr->getOperator () == OO_Star))) {
@@ -251,6 +254,8 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
251254 // This can happen if the object is a smart pointer. Don't add anything
252255 // because a '->' is already there (PR#51776), just call the method.
253256 ReplacementText += " empty()" ;
257+ } else if (E->isImplicitCXXThis ()) {
258+ ReplacementText += " empty()" ;
254259 } else if (E->getType ()->isPointerType ())
255260 ReplacementText += " ->empty()" ;
256261 else
Original file line number Diff line number Diff line change @@ -186,6 +186,10 @@ Changes in existing checks
186186 <clang-tidy/checks/portability/template-virtual-member-function>` check to
187187 avoid false positives on pure virtual member functions.
188188
189+ - Improved :doc: `readability-container-size-empty
190+ <clang-tidy/checks/readability/container-size-empty>` check by correctly
191+ generating fix-it hints when size method is called from implicit ``this ``.
192+
189193- Improved :doc: `readability-identifier-naming
190194 <clang-tidy/checks/readability/identifier-naming>` check by ignoring
191195 declarations in system headers.
Original file line number Diff line number Diff line change @@ -895,3 +895,16 @@ namespace PR94454 {
895895 int operator " " _ci() { return 0 ; }
896896 auto eq = 0_ci == 0 ;
897897}
898+
899+ namespace GH152387 {
900+
901+ class foo : public std ::string{
902+ void doit () {
903+ if (!size ()) {
904+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of 'size'
905+ // CHECK-FIXES: if (empty()) {
906+ }
907+ }
908+ };
909+
910+ }
You can’t perform that action at this time.
0 commit comments