Open
Description
This issue is identified in #112751.
// case1
namespace std {
template<typename T>
struct unique_ptr {
T &operator*();
T *get() const [[clang::lifetimebound]];
};
} // namespace std
struct X {
X(std::unique_ptr<int> up) :
pointer(up.get()), owner(std::move(up)) {}
int *pointer;
std::unique_ptr<int> owner;
};
When we add the clang::lifetimebound
annotation to unique_ptr::get()
, clang emits a dangling-field warning for the pointer(up.get())
member initializer. This warning is a false positive in this context, as the owner
member is moved as part of the initialization, retaining ownership.
Another example occurs in designated-initializer cases:
// case2
struct X {
int *pointer;
std::unique_ptr<int> owner;
};
X func(std::unique_ptr<int> up) {
return {
.pointer = up.get(),
.owner = std::move(up)
};
}
Fixing these false positives is hard because it would require tracking dependencies between expressions, which is beyond the capabilities of the current statement-local analysis.