<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/145506>145506</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missing warning if variable is used in else clause of if (type_t var = ...)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Alcaro
</td>
</tr>
</table>
<pre>
```c++
int* get_something();
int* get_something_else();
int square() {
if (int* ptr1 = get_something())
return ptr1[0] * ptr1[0];
else if (int* ptr2 = get_something_else())
return ptr1[0] * ptr2[0]; // whoops, typoed!
else
return -1;
}
```
Expected: Compiler warning about how ptr1 is always null on the commented line. (Simplifying the example further returns an unused-variable warning for ptr2; that's not what I'm asking for.)
Actual: No output, just pretends ptr1[0] is 0 and optimizes accordingly. (Ideally, ptr1 would only be in scope in its then-clause, not throughout the entire if-statement, but that's not how the C++ spec looks.)
https://godbolt.org/z/118vGjE43
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx0VMFyozgQ_Rpx6bILhDH2gYOTjLfmsHvZD0gJ1IAyQmLVrXg8X78lIJPZycalMiC13uv31C1FZAaH2IjqQVRPmYo8-tBcbKeCz1qv74045uvohHxII78Yx0JeYEB-Jj8hj8YNQp6EPIvys_VntIS_BQH9E1XYZkHUaRoAwPQg5GlDmTkUIMqn_6WT57QlIMfglkhRPeSieoK3ndv3SpmwUxofCORHgl_zXVm23-dk8p0MhLwKeYXb6P1MQj4C32ePWsgiQS3YHyB3xZqmqJ_S_5vvIr98-T5jx6hFeYFHP83GYoCbCs64AVTrI8Pob6tVhkDZm7oTuGgteAc8InR-mtAxarDG4T7J_9tMszX9PWGkEPyuptki9DHwiGHLikA5iC4S6t2rCka1Fn9S9z6swssH4FGxkDWB8wy3UTF8FbKeQNG3LXS_Ginyy6XjqGxS85cHH3mOnDx6icQwB2R0mv7jsCHIQTkNfmYzmR9IoLrOB23cYO-LnK8albX3hLP4cPPRavDO3qFFMA6o8_PyYpiSYLfrrIrpkB-XnHkMPg5jMnOxw7EJqVR2xIoxuZci22X5F6nJ-BT_uLYH0IwdWO-_0bvekXkmUV7Wqhi8br3lvQ-DkNcfQl6L4vT6x8uXQ5npptTn8qwybIq6yutjXRzzbGwOrUaUqu-74lz1OdbnSlVS5ce6aPXxWGWmkbms8qM8FLKQ5Wkvu6qsTt0pV0rXtTyLQ46TMnZv7euUuDNDFLEpDlWVHzOrWrS03ANSOrzBsiqkTNdCaNKmXRsHEofcGmJ6h2HDFps_DVE657fSMD38LBdDkOoneb-03-o7-H5rRL7P-MwpfunD_T45l8Vgm9-MMzzGdt_5Schr4t8euzn4F-xYyOuSNSVLV1mvjfw3AAD__32wiRk">