[clang] [analyzer] Do not destruct fields of unions (PR #122330)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 4 02:57:11 PST 2025


================
@@ -441,3 +441,31 @@ void testLeakBecauseNTTPIsNotDeallocation() {
   void* p = ::operator new(10);
   deallocate_via_nttp<not_free>(p);
 }  // leak-warning{{Potential leak of memory pointed to by 'p'}}
+
+namespace optional_union {
+  template <typename T>
+  class unique_ptr {
+    T *q;
+  public:
+    unique_ptr() : q(new T) {}
+    ~unique_ptr() {
+      delete q;
+    }
+  };
+
+  union custom_union_t {
+    unique_ptr<int> present;
+    char notpresent;
+    custom_union_t() : present(unique_ptr<int>()) {}
+    ~custom_union_t() {};
----------------
Xazax-hun wrote:

Nit: redundant semi after the definition.

https://github.com/llvm/llvm-project/pull/122330


More information about the cfe-commits mailing list