[clang] [analyzer] Add cplusplus.DestroyedAsWrongType checker (PR #237801)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 12:01:21 PST 2026


mark-zilber wrote:

 
> However, I think it would be nice if the diagnostic message would indicate that the destructor is called indirectly, through a standard library utility. The current message is fitting for `std::destroy_at` and `std::default_delete`, but the message could be surprising if it was coming from the destructor of a standard type (e.g. a `vector` or similar container). Try to add a testcase where the placement new reinitializes some part of a `std` object and compose a message that also fits this situation.


Reports deferred out of std now name the function that made the call, like "Destructor of 'B' is called by 'std::holder<B>::~holder' on storage that holds an object of type 'A'". Added member_of_std_object, where placement new reuses  a member of a std object



> > explicit non-virtual base class destructor call on a derived object is reported, although [class.dtor]/16 allows it.
> 
> What was the obstacle to following the standard in this case? 

Basically, there was no obstacle. I had treated the explicit call like delete, which needs a virtual destructor. Now any base class of the placed type is accepted  and the implicit destructor at the end of the scope still requires the exact type, so not restoring the original object is still reported. However, now calling the destructor through the name of the old variable (b.~Base() after new (&b) Derived) is not reported, but that is a use of an object whose lifetime has ended, which is out of scope for this checker


https://github.com/llvm/llvm-project/pull/237801


More information about the cfe-commits mailing list