Author: flovent Date: 2025-08-10T12:20:51+03:00 New Revision: 76d2f0fe471d761895e6a8274b731ed59ce4403a
URL: https://github.com/llvm/llvm-project/commit/76d2f0fe471d761895e6a8274b731ed59ce4403a DIFF: https://github.com/llvm/llvm-project/commit/76d2f0fe471d761895e6a8274b731ed59ce4403a.diff LOG: [clang-tidy] `readability-container-size-empty`: Correctly generating fix hints when size method is called from implicit this (#152486) Correctly generating fix hints when size method is called from implicit this. Closes #152387. --------- Co-authored-by: Baranov Victor <bar.victor.2...@gmail.com> Co-authored-by: EugeneZelenko <eugene.zele...@gmail.com> Added: Modified: clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp index ce736a8d16023..c4dc319f23c38 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -238,9 +238,12 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) { ? MemberCallObject : (Pointee ? Pointee : Result.Nodes.getNodeAs<Expr>("STLObject")); FixItHint Hint; - std::string ReplacementText = std::string( - Lexer::getSourceText(CharSourceRange::getTokenRange(E->getSourceRange()), - *Result.SourceManager, getLangOpts())); + std::string ReplacementText = + E->isImplicitCXXThis() + ? "" + : std::string(Lexer::getSourceText( + CharSourceRange::getTokenRange(E->getSourceRange()), + *Result.SourceManager, getLangOpts())); const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E); if (isBinaryOrTernary(E) || isa<UnaryOperator>(E) || (OpCallExpr && (OpCallExpr->getOperator() == OO_Star))) { @@ -251,6 +254,8 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) { // This can happen if the object is a smart pointer. Don't add anything // because a '->' is already there (PR#51776), just call the method. ReplacementText += "empty()"; + } else if (E->isImplicitCXXThis()) { + ReplacementText += "empty()"; } else if (E->getType()->isPointerType()) ReplacementText += "->empty()"; else diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8b63601882930..187aae2ec8c90 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -186,6 +186,10 @@ Changes in existing checks <clang-tidy/checks/portability/template-virtual-member-function>` check to avoid false positives on pure virtual member functions. +- Improved :doc:`readability-container-size-empty + <clang-tidy/checks/readability/container-size-empty>` check by correctly + generating fix-it hints when size method is called from implicit ``this``. + - Improved :doc:`readability-identifier-naming <clang-tidy/checks/readability/identifier-naming>` check by ignoring declarations in system headers. diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp index 2fd0b2224cb1c..b1e68672a3a9a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp @@ -895,3 +895,16 @@ namespace PR94454 { int operator""_ci() { return 0; } auto eq = 0_ci == 0; } + +namespace GH152387 { + +class foo : public std::string{ + void doit() { + if (!size()) { + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of 'size' + // CHECK-FIXES: if (empty()) { + } + } +}; + +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits