bugprone-unintended-char-ostream-output¶
Finds unintended character output from unsigned char
and signed char
to an
ostream
.
Normally, when unsigned char (uint8_t)
or signed char (int8_t)
is used, it
is more likely a number than a character. However, when it is passed directly to
std::ostream
’s operator<<
, the result is the character output instead
of the numeric value. This often contradicts the developer’s intent to print
integer values.
uint8_t v = 65;
std::cout << v; // output 'A' instead of '65'
The check will suggest casting the value to an appropriate type to indicate the
intent, by default, it will cast to unsigned int
for unsigned char
and
int
for signed char
.
std::cout << static_cast<unsigned int>(v); // when v is unsigned char
std::cout << static_cast<int>(v); // when v is signed char
To avoid lengthy cast statements, add prefix +
to the variable can also
suppress warnings because unary expression will promote the value to an int
.
std::cout << +v;
Or cast to char to explicitly indicate that output should be a character.
std::cout << static_cast<char>(v);
- CastTypeName¶
When CastTypeName is specified, the fix-it will use CastTypeName as the cast target type. Otherwise, fix-it will automatically infer the type.