================ @@ -0,0 +1,34 @@ +.. title:: clang-tidy - bugprone-incorrect-enable-shared-from-this + +bugprone-incorrect-enable-shared-from-this +======================================= + +Checks if class/struct publicly derives from ``std::enable_shared_from_this``, +because otherwise when ``shared_from_this`` is called it will throw +``std::bad_weak_ptr``. + +Consider the following code: + +.. code-block:: c++ + #include <memory> + + class BadExample : std::enable_shared_from_this<BadExample> { + // warning: inheritance from std::enable_shared_from_this + // should be public inheritance, + // otherwise the internal weak_ptr won't be initialized + // [bugprone-incorrect-enable-shared-from-this] ---------------- 5chmidti wrote:
The diagnostics of a check are normally not copy-pasted to the documentation. Maybe: > privately inheriting from `std::enable_shared_from_this` results in an > uninitialized `weak_ptr` when calling `shared_from_this` Or add smaller comments: ``` #include <memory> // private inheritance class BadExample : std::enable_shared_from_this<BadExample> { public: // `shared_from_this` returns uninitialized `weak_ptr` BadExample* foo() { return shared_from_this().get(); } void bar() { return; } }; void using_not_public() { auto bad_example = std::make_shared<BadExample>(); auto* b_ex = bad_example->foo(); b_ex->bar(); } ``` https://github.com/llvm/llvm-project/pull/102299 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits