<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/119415>119415</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            incorrect modeling of std::optional in cplusplus.NewDelete leads to incorrect warnings
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:static analyzer
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          vtjnash
      </td>
    </tr>
</table>

<pre>
    The clang-analyzer incorrectly models the default `~` function for a `union` object (it calls `~` on some of the options, in contradiction to the C++ standard and actual implementation which does not call `~` on any fields). This results in declaration of double-frees (or lost memory) which don't actually occur at runtime for sample code like this.
```c++
$ cat dtor-test.cpp 
#include <iostream>
#include <optional>

class C {
  int *once;
public:
  C(int) {
    std::cout << "C Ctor" << std::endl;
    once = new int[3];
  }
  C(C&&) = delete;
 ~C() {
    std::cout << "C Dtor" << std::endl;
    delete[] once;
 }
};

int main() {
  std::optional<C> S{1};
  return 0;
}
```
```
$ clang++ -fPIE -std=c++20 dtor-test.cpp  && ./a.out # demo that clang++ runs correctly
C Ctor
C Dtor
$ g++ -fPIE -std=c++20 dtor-test.cpp  && ./a.out # demo that g++ runs correctly
C Ctor
C Dtor
$ clang++ --analyze -analyzer-output=text -std=c++20 dtor-test.cpp # demo that clang-analyzer is wrong
dtor-test.cpp:14:5: warning: Attempt to free released memory [cplusplus.NewDelete]
   14 |     delete[] once;
      |     ^
```
from https://github.com/JuliaLang/julia/pull/56130#issuecomment-2518550050
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVd2O6jYQfprhZgRybMLPRS5CWKRWVVWp5wWMPYC3jo38s1t6cZ69chLYs7vV6alUFMCxPd988y9jNGdH1EC9g3o_kzldfGhe0rOT8TI7en1rvlwIlZXuPJdO2ttfFNA45UMglewNe6_JRkwXQk0nmW1CWLGvsGJ4yk4l4x2efEBZt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