<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/135136>135136</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] Misleading warning location for -Wimplicit-int-conversion
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:frontend,
clang:diagnostics
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
carlosgalvezp
</td>
</tr>
</table>
<pre>
Consider this example:
```cpp
#include <cstdint>
std::uint16_t compute(std::uint16_t a, std::uint16_t b)
{
return ((a & 0xFF) << 3) | (b & 0x0F);
}
```
Enabling `-Wimplicit-int-conversion`, Clang prints:
```
source>:5:30: warning: implicit conversion loses integer precision: 'int' to 'std::uint16_t' (aka 'unsigned short') [-Wimplicit-int-conversion]
5 | return ((a & 0xFF) << 3) | (b & 0x0F);
| ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
```
[Godbolt](https://godbolt.org/z/qf6nYnK9M)
This is very confusing, because the caret `^` is pointing at the `|` sign, and that's not where the problem lies. Therefore it's quite misleading and people may spend a lot of time figuring out what's going on.
The implicit conversion is happening because the result of these operations is `int`, and the return type of the function is `std::uint16_t`. What the user needs to do to fix the warning is to cast the result to `std::uint16_t`. But this is not at all obvious from the warning message, especially if beginner users aren't familiar with the rules of integer promotion/usual arithmetic conversions.
Could this warning be improved to more clearly highlight/pinpoint what the problem is?
Thanks!
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVU2TozYQ_TXypWtcsjDYPvhge8Y5pPa2VVs5pQQ00FkhsfrwjPeQ355qwBlPnLktRQFSt153v34SOgRqLeJe5EeRPy90ip3z-0p740KrzQV_DovS1df9ydlANXqIHQXAN90PBkV2EHK8Cznd1TDwUGVkK5NqBJGdqhBrslFkL5NziDUvzA6JbFwVf0aoXD-kiEJtH21aqBM8TpdC7RhucxTyAADgMSZvQaitUFsNQhUg385noXacg8hOkI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