[clang-tools-extra] [clang-tidy] Create bugprone-incorrect-enable-shared-from-this check (PR #102299)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 11 09:51:39 PDT 2024


================
@@ -0,0 +1,33 @@
+.. title:: clang-tidy - bugprone-incorrect-enable-shared-from-this
+
+bugprone-incorrect-enable-shared-from-this
+==========================================
+
+Checks if class/struct publicly inherits from 
+``std::enable_shared_from_this``, because otherwise when ``shared_from_this``
+is called unintended behaviour will occur.
+
+Consider the following code:
+
+.. code-block:: c++
+    #include <memory>
+
+    // private inheritance
+    class BadExample : std::enable_shared_from_this<BadExample> {
+    
+    // ``shared_from_this``` unintended behaviour
+    // libstd implementation returns uninitialized ``weak_ptr``
+        public:
+        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();
+    }
+
+Using ``libstd`` implementation, `shared_from_this`` will throw 
----------------
5chmidti wrote:

`Using the ..`

`shared_from_this` is missing one leading backtick

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


More information about the cfe-commits mailing list