#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 (so, the resulting lambda is passed into std::function directly), then the warning is not triggered.
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 (so, the resulting lambda is passed into std::function directly), then the warning is not triggered.
Clang-tidy version is 18.1
Minimal reproduction example:
As described, when calling
combine
two times in this scenario, if the temporary variable is present incombine
implementation, then clang-tidy triggers warning "potential memory leak" which points to implementation of constructor ofstd::function
. If the temporary variable is not present (so, the resulting lambda is passed into std::function directly), then the warning is not triggered.Clang-tidy version is 18.1