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

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] false positive report of possible memory leak when capturing std::function in lambda multiple times
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-tidy
      </td>
    </tr>

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

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

<pre>
    Minimal reproduction example:

```
#include <functional>

std::function<void()> combine(const std::function<void()>& lhs, const std::function<void()>& rhs)
{
    auto thisTemporaryCausesWarning = [lhs, rhs] (){};
    return { std::move(thisTemporaryCausesWarning) };

int main()
{
 std::function<void()> f;
    f = combine(f, nullptr);
    f = combine(f, nullptr); // this call triggers warning
}
```

As described, when calling `combine` two times in this scenario, if the temporary variable is present in `combine` implementation, then clang-tidy triggers warning "potential memory leak" which points to implementation of constructor of `std::function`. If the temporary variable is not present, then the warning is not triggered.

Clang-tidy version is 18.1
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVMuO4zYQ_Brq0hiDIm1ZOujgxxrIIbcAOVNSy-qEIgWy5Vn_fUBZHm92kEwCEDAMdVVXFQs0MdLVIdZidxS7c2ZmHnyob8aa8D1rfHevfyVHo7EQcAq-m1sm7wC_m3GyKPRByOUUcj3yIJQm19q5QxD61M9ugRgr9LfHcOQuAfXh-U3o081TJ1QpVCX0N2j92JBDocrWu8jwJUKoAuwQhTrB_0CEhKiSqP1RyAMAgJnZAw8Uf8Nx8sGE-8nMEePvJjhyVxD6DGJ3XHclgt0ZVtL9UezPQj-pAvIcHIj98aVm9Lfk6p8XCFXBB4uQB3IMoyG3rviQ-nWG_UtJv8h-hdon7W62duKwTP_3QRDqItRliQhaYy1woOsVQ4T31UHSeP65E_JwiNBhbAM12CXa9wHdwrDEWsjn1kICv3tgGjECucem2KIzgXwCUg88IPAzP7iZQKaxCBRhChjRcQL-nZNSX0d0bJa01CmROGitcdc3pu7-yQgIpSbP6JiMhRFHH-5g0fwplIL3gdoBJk-OI7D_iR58_-hhmFv2If0Vhfx8Z4XcwC__Zsd5flr6kJymnxLXkVU6dptH1qeXqxuGmBRRhLzc5FlX667Slcmwzveq2FaqLPNsqHuURu6bDreFLvedLKQx2G8rXW7bXZnvMqqVVFtZ5UWuVa7kRuoKc1N0fYXttsFGbCWOhuzG2tu48eGaUYwz1rksS6Uzaxq0cXlolHrFLpRKD0-oE-qtma9RbKWlyPHFw8R2eaJ-gO3O0BsbESYfiemG6YHygVPWk4-RUoI_XNqzbxPPISX36TJSZawZm87AOFumyeKjhNkcbD0wTzEBlvpfiYe52bR-FOqSZK4_b1Pwf2DLQl0W71Goy2r_Vqu_AgAA__8qfL5g">