-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Open
Labels
Description
When using clang-tidy with the clang-analyzer-cplusplus.NewDeleteLeaks
check enabled with aggregate initialization of two unique_ptr
member variables, a false positive memory leak warning is output.
Reproducer
The code used to reproduce this issue.
#include <memory>
struct Foo {
std::unique_ptr<int> a;
std::unique_ptr<int> b;
};
void test_f() {
Foo foo = {std::make_unique<int>(1), std::make_unique<int>(2)};
}
Steps to reproduce
Run clang tidy with the code above with the clang-analyzer-cplusplus.NewDeleteLeaks
check enabled, i.e.:
clang-tidy test.cpp --checks="-*,clang-analyzer-cplusplus.NewDeleteLeaks" -- --std=c++20
Actual output:
1 warning generated.
test.cpp:11:1: warning: Potential leak of memory pointed to by field '_M_head_impl' [clang-analyzer-cplusplus.NewDeleteLeaks]
11 | }
| ^
test.cpp:9:14: note: Calling 'make_unique<int, int>'
9 | Foo foo = {std::make_unique<int>(1), std::make_unique<int>(2)};
| ^~~~~~~~~~~~~~~~~~~~~~~~
unique_ptr.h:1077:30: note: Memory is allocated
1077 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:9:14: note: Returned allocated memory
9 | Foo foo = {std::make_unique<int>(1), std::make_unique<int>(2)};
| ^~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:11:1: note: Potential leak of memory pointed to by field '_M_head_impl'
11 | }
| ^
Expected result
I would expect no warning to be output, since through all possible code paths, the memory owned by the unique_ptr
should be deleted.
I have tested this on Ubuntu 25.04:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 25.04
Release: 25.04
Codename: plucky
With clang-tidy version 20.1.2 installed through apt
on my ubuntu system:
$ clang-tidy --version
Ubuntu LLVM version 20.1.2
Optimized build.
Also see a reproducer on Godbolt: https://godbolt.org/z/GcMqrb3q1