I'm getting a clang-tidy warning that reads narrowing conversion from 'int' to 'float' when I convert from a uint8_t to a float, which to my understanding is not a narrowing conversion since float can represent every integer that a uint8_t can.
Code example:
uint8_t testVar = 8;
float test2 = 2.0f * testVar;
clang-tidy flags the second line of that with the warning cppcoreguidelines-narrowing-conversions: narrowing conversion from 'int' to 'float'. In my IDE, the squiggle shows up under the testVar.
According to the reference, this warning should be flagged if we convert from an integer to a narrower floating-point (e.g. uint64_t to float), but to the best of my knowledge, float is not narrower than uint8_t.
Am I fundamentally misunderstanding these data types, or is something else going on here?
I'm on LLVM version 11.0.0 if that matters.
uint8_tis implicitly converted tounsignedbefore it is then converted tofloat. Obviously clang-tidy is not being very smart.ualready stood forunsigned. My IDE tells me thatuint8_tis just an alias forunsigned char.uint8_tin an expression it is implicitly converted tounsigned int. This is called integer promotionfloatis signed which is always smaller than the range ofunsignedtypes. 2.floatcovers smaller range thanint. Although uint8_t ultimately boils down to something equivalent ofunsigned charbut as stated before, clang-tidy is not going that deep.