https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/152486
>From d5b8bebfb79ac67ecf144c9668081469c843e16f Mon Sep 17 00:00:00 2001 From: flovent <flb...@protonmail.com> Date: Thu, 7 Aug 2025 19:25:15 +0800 Subject: [PATCH 1/5] [clang-tidy] `readability-container-size-empty`: Correctly generate fixit when `size` method's base is `this` --- .../readability/ContainerSizeEmptyCheck.cpp | 11 ++++++++--- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../checkers/readability/container-size-empty.cpp | 13 +++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) 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 2dc5c73073cf8..a0c03754e8fbf 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -182,6 +182,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/portability/container-size-empty>` check by correctly + generating fix hints when size method is inherited from parent class. + - 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..462db5acbaade 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 PR152387 { + +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()) { + } + } +}; + +} >From 019ca4ba07ff8b23d22c9da5758201e53bb39f67 Mon Sep 17 00:00:00 2001 From: flovent <flb...@protonmail.com> Date: Thu, 7 Aug 2025 20:27:39 +0800 Subject: [PATCH 2/5] [NFC] fix release note --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index a0c03754e8fbf..de3e4062081c6 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -183,7 +183,7 @@ Changes in existing checks avoid false positives on pure virtual member functions. - Improved :doc:`readability-container-size-empty - <clang-tidy/checks/portability/container-size-empty>` check by correctly + <clang-tidy/checks/readability/container-size-empty>` check by correctly generating fix hints when size method is inherited from parent class. - Improved :doc:`readability-identifier-naming >From 3d90d15dae1c9c62046674509ba18432d9d09910 Mon Sep 17 00:00:00 2001 From: flovent <flb...@protonmail.com> Date: Thu, 7 Aug 2025 20:47:16 +0800 Subject: [PATCH 3/5] [NFC] improve doc --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index de3e4062081c6..2e021ebca1daf 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -184,7 +184,7 @@ Changes in existing checks - Improved :doc:`readability-container-size-empty <clang-tidy/checks/readability/container-size-empty>` check by correctly - generating fix hints when size method is inherited from parent class. + generating fix hints when size method is called from implicit this. - Improved :doc:`readability-identifier-naming <clang-tidy/checks/readability/identifier-naming>` check by ignoring >From 65ff2ffd372a82015941160fa103edeb1ea03957 Mon Sep 17 00:00:00 2001 From: flovent <flb...@protonmail.com> Date: Fri, 8 Aug 2025 15:07:25 +0800 Subject: [PATCH 4/5] apply suggestion Co-authored-by: Baranov Victor <bar.victor.2...@gmail.com> --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 2e021ebca1daf..20c6b0d1ac600 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -184,7 +184,7 @@ Changes in existing checks - Improved :doc:`readability-container-size-empty <clang-tidy/checks/readability/container-size-empty>` check by correctly - generating fix hints when size method is called from implicit this. + generating fix hints when size method is called from implicit ``this``. - Improved :doc:`readability-identifier-naming <clang-tidy/checks/readability/identifier-naming>` check by ignoring >From 0fd8a7dd9223d05bdde78d5101ccc51593e727ff Mon Sep 17 00:00:00 2001 From: flovent <flb...@protonmail.com> Date: Fri, 8 Aug 2025 15:07:53 +0800 Subject: [PATCH 5/5] apply suggestion Co-authored-by: Baranov Victor <bar.victor.2...@gmail.com> --- .../clang-tidy/checkers/readability/container-size-empty.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 462db5acbaade..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 @@ -896,7 +896,7 @@ namespace PR94454 { auto eq = 0_ci == 0; } -namespace PR152387 { +namespace GH152387 { class foo : public std::string{ void doit() { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits