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

    <tr>
        <th>Summary</th>
        <td>
            [FR] clang-tidy check for nullptr check when calling delete or free
        </td>
    </tr>

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

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

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

<pre>
    Plenty of people do not know that calling `free`, `std::free` or `delete` on a `nullptr` is a no-op.
If a check for `nullptr` is applied to only such a call, it should be flagged and a fix proposed to remove it.

Possible name could be "readability-redundant-nullptr-check" or it could be in "misc-".

```cpp
int* ptr{nullptr}
if (!ptr)  // check not required
{
    delete ptr;
}
```

All standard checks should be supported `!ptr`, `ptr != nullptr`, `ptr != NULL`, etc
Both extra scopes and single line if checks should be supported.

```cpp
int* ptr{nullptr}
if (!ptr)  // check not required
    delete ptr; // no scope

if (!ptr)  // check not required
{{{{
    delete ptr; // lots of scopes
}}}}
```

Assignment of nullptr after the delete should also trigger this.
Fix should of course retain the assignment.

```cpp
int* ptr{nullptr}
if (!ptr)  // check not required
{
    delete ptr;
    ptr = nullptr; // extra assignment of nullptr. should also check for 0, NULL.
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVN2OqzYQfhpzM0oEJgnhggvSiKrSUXVUqQ8w4AHc49jUHvZs3r4ykM22W63Ui-pIlhAznr_v-zwYgh4sUSWOF3G8Jjjz6HzVoOncz-ST1ql79dWQ5Tu4HiZykyFQDqxj-Gbdd-ARGTo0RtsBxCntPZE4pUL-FP8CK5HXIq83MzgfzYoM8fpvAaPFzsZM7KNJB0CwbuemvUjrX3pA6EbqvkG_Bv_j6jQZTQrYgbPmDmHuxhiBxsQeNEMY3WwUtAS9wWEgBWgVIPT6FSbvJhfWcE8390KgOZYVaf3VhaBbQ2DxRtA9kggpPaHCVhvN950nNVuFlndbX7ulWSFlnFXzM1DbGHvTodsJKbciEarldNMk0lpbFrKGOF5xeQxaXKOnByHPQmbRIksAIRshmw2aSIenP2ftScWsxUWkNQDAivSSML8snuv7qmsTtTEQGK1Cr9aE4R1qYZ4m55kULLxmK_YbwRN7EDIT-RWevHz0_fr7ly-bg7gTaX1xPAK9skcInZsoLKQEbQdDYLQl0P0nrfyv6H3A7XHburXZtfh_ZuTtfFLCOA7xpa2gbIS9nY_MLe_3RpZj0DYyYM_kgUd61NgQRBMcsNfDsLh1iDA2-vXhd31Uqw8Enhi1XVLgW4kfJNloW6T0TmNPwFYN4b_hsP_b3M8dssgwKnL_8UEkqspVmZeYUJUVeS6PMj2dk7HKD2V56s9Zhvmpz1XRHvpTr9pjmndYln2Z6Eqm8pDJNM_Ox4Ms9urUE7ZHLMoyP7eHQhxSuqE2e2Nebnvnh0SHMFOVZeejLBKDLZmwLGIpO4N22LFWdyFlXMy-ilG7dh6COKRGBw7PPKzZLCu8-U0cr_AMfjf0Qxur5ftI9m1rb4A7D3FLJ7M31cg8hbi5F5AHzePc7jt3E7KJVbfPbvLuD-pYyGYZJQjZbNO8VPKvAAAA___Lc_qC">