Issue 135136
Summary [clang] Misleading warning location for -Wimplicit-int-conversion
Labels clang:frontend, clang:diagnostics
Assignees
Reporter carlosgalvezp
    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!
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to