https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/190535
Fixes https://github.com/llvm/llvm-project/issues/132419. >From cd12916f5e7a449eb77ec540e29d17a7aaaf3f0e Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Sun, 5 Apr 2026 19:05:28 +0300 Subject: [PATCH] [clang-tidy] Fix FP in readability-container-size-empty with compairing to unrelated type --- .../readability/ContainerSizeEmptyCheck.cpp | 2 +- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++ .../readability/container-size-empty.cpp | 39 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp index 2101fd2248e8a..d791cd9340766 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -209,7 +209,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) { const auto WrongComparend = anyOf(stringLiteral(hasSize(0)), userDefinedLiteral(hasLiteral(stringLiteral(hasSize(0)))), - cxxConstructExpr(argumentCountIs(0)), + cxxConstructExpr(argumentCountIs(0), hasType(ValidContainer)), cxxUnresolvedConstructExpr(argumentCountIs(0))); // Match the object being compared. const auto STLArg = diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 69dc5b9633398..565e0dfedb971 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -399,6 +399,9 @@ Changes in existing checks - Reduce verbosity by removing the note indicating source location of the ``empty`` function. + - Fixed a false positive with suggesting ``empty`` when comparing a container + to a default-constructed object of an unrelated type. + - Improved :doc:`readability-else-after-return <clang-tidy/checks/readability/else-after-return>` check: 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 2b8b3261ac765..3dac7ef857551 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 @@ -954,3 +954,42 @@ struct DestructorUser { } }; } + +namespace GH162287 { +struct Label { + virtual ~Label(); +}; + +bool operator==(std::string, const Label&); + +bool testUnrelatedType() { + std::string s{"aa"}; + return s == Label{}; +} + +bool testUnrelatedTypeParens() { + std::string s{"aa"}; + return s == Label(); +} + +bool testValidContainer() { + std::string s{"aa"}; + return s == std::string{}; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object + // CHECK-FIXES: return s.empty(); +} + +template <typename T> +bool testUnrelatedInTemplate(std::string s) { + return s == Label{}; +} +template bool testUnrelatedInTemplate<int>(std::string); + +template <typename T> +bool testDependentValidContainer(TemplatedContainer<T> c) { + return c == TemplatedContainer<T>(); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object + // CHECK-FIXES: return c.empty(); +} +template bool testDependentValidContainer<int>(TemplatedContainer<int>); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
