<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/66732>66732</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect "variable is uninitialized when used here" when LHS of compound assignment is initialized by RHS (C++17)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mvduin
</td>
</tr>
</table>
<pre>
Since C++17 the right side of a compound-assignment expression (including any side effects) is sequenced before the left side is evaluated (and therefore before the assignment), which means the following code should be well-defined when compiled with `-std=c++17` or later:
```c++
// returns x + y + carry from (x + y)
unsigned foo( unsigned x, unsigned y ) {
unsigned z;
z += __builtin_uadd_overflow( x, y, &z );
return z;
}
```
However clang (with `-O2 -Wall -std=c++17`) emits the following warning:
```
<source>:4:5: warning: variable 'z' is uninitialized when used here [-Wuninitialized]
z += __builtin_uadd_overflow( x, y, &z );
^
<source>:3:15: note: initialize the variable 'z' to silence this warning
unsigned z;
^
= 0
```
The issue is not specific to `__builtin_uadd_overflow`, replacing the assignment statement by the equivalent `z += (z = x + y) < x;` results in the same warning.
It appears that the generated code is still correct, but obviously I'd get rather nervous when the compiler emits a warning that implies it thinks the code has undefined behaviour.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVUtv6zYT_TXjzcAGTcaytdAiiW-QAB_wAU2BuwwocWSxpUhfPuw4v74g_Ux6e7tpEEgWyXmcM2eGMgS9sUQNLB5gsZ7IFAfnm3GnkraT1qlD86ptR_gI_AH4w3yJcSD0ejNEDFoRuh4ldm7cumTV9OhvJBuR3reeQtDOIvCVtp1JStsNSns4WlLfUxcD8Bp1wEA_EtmOFLbUO08ljqH-FEYHpJ00SUZS2Z-0Kp_wx7M3JtcMgNfAH3E_6G7AkaQN5UDvjHH7nEnnFGEYXDI5KO7JmKmiXltSuB_IFlja5C8dB4SKTUNUINbdmQyoGDqPRkbyI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