<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/120924>120924</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] Check request: never capture `shared_from_this` by a field
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-tidy
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          denzor200
      </td>
    </tr>
</table>

<pre>
    
```
#include <functional>
#include <memory>

struct A : std::enable_shared_from_this<A> {
   std::function<void()> captured;
   
   void init() {
    captured = 
        [self=shared_from_this()]() { // INCORRECT
        };
 }
};

int main()
{
    auto a = std::make_shared<A>();
 a->init();
}
```
The example above contains circular reference, so `A` will never be deleted, that's a memory leak 100%.
One of the possible fix is to use `weak_from_this` instead. Suppose a check to discover the bug like this will be simple
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxkU0tvqzwQ_TXDZpTI2OS1YEHzkL7NV6m3-8rgIfjG2Lm2SZv7669MSBOpUQRYnnPmnHnIEPTREpWweIHFLpND7JwvFdm_znPGstqpawmsgiWb_qwCLrRtzKAIQWzbwTZROysNiP2P255656_TDatC9EMTsUIQFYaoQFQgKrKyNvQROulJfbTe9R-x0wHEtgKxR1i9AKsQH4B7ThDbi9MK-Br4JoU28hwHTwrEBLk9UxBqq-Mt8sH4DUAQuyl4_MHiJZBpQex-qLolW-weXAj8APyA__2_fX1722_fn4lWu0lM-mLV9xlYpW3EXmo7UabLuy45RIdyVPXtupene5Gm0tyNjyg5A7F_mJyy3JI-de-9I6Qv2Z8NoazdhbBxNkptAzbaN4ORHj215Mk2BHyLwSEsRw781MagpQt5rAkVGYqkUkzsZAS-Cijx1nE0JE-YMwZ8MQdWvVpC12LsCM8uBF0bwlZ_oQ4YHQ6BUo5PkqenQi8ZahsiSTXHX8P57AKhxKaj5pRASofGJSmJtB6OaPSJMCFvOmvCoJPNTJVCbcRGZlTmK1Fscl4sN1lXLnNJTb6iNa2LdsMLtS4EtaSIcrYmWWe65IwXOeeCMbYq1nNep6MkxvK6Vqs1FIx6qc3cmEs_d_6Y6RAGKnPONrzIjKzJhHG5OG-MtMdZ1OoKnKdl82VCzerhGKBgRocYHjxRRzOu5RNsscPt6N7Tn4FCTEt068Y0xqmIP-Z1ybC-osRWk1HZ4E3ZxXgOaabGsT3q2A31vHE98EPKP71mZ-9-UxOBH0ZTAfhh8nUp-b8AAAD__44dR80">