llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Baranov Victor (vbvictor)

<details>
<summary>Changes</summary>


Fixes https://github.com/llvm/llvm-project/issues/132419.

---
Full diff: https://github.com/llvm/llvm-project/pull/190535.diff


3 Files Affected:

- (modified) 
clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp (+1-1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp 
(+39) 


``````````diff
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>);
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/190535
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to