brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · da510c4 Raw
60 lines · plain
1.. title:: clang-tidy - bugprone-unintended-char-ostream-output2 3bugprone-unintended-char-ostream-output4=======================================5 6Finds unintended character output from ``unsigned char`` and ``signed char`` to7an ``ostream``.8 9Normally, when ``unsigned char (uint8_t)`` or ``signed char (int8_t)`` is used,10it is more likely a number than a character. However, when it is passed11directly to ``std::ostream``'s ``operator<<``, the result is the character12output instead of the numeric value. This often contradicts the developer's13intent to print integer values.14 15.. code-block:: c++16 17  uint8_t v = 65;18  std::cout << v; // output 'A' instead of '65'19 20The check will suggest casting the value to an appropriate type to indicate the21intent, by default, it will cast to ``unsigned int`` for ``unsigned char`` and22``int`` for ``signed char``.23 24.. code-block:: c++25 26  std::cout << static_cast<unsigned int>(v); // when v is unsigned char27  std::cout << static_cast<int>(v); // when v is signed char28 29To avoid lengthy cast statements, add prefix ``+`` to the variable can30also suppress warnings because unary expression will promote the value31to an ``int``.32 33.. code-block:: c++34 35  std::cout << +v;36 37Or cast to char to explicitly indicate that output should be a character.38 39.. code-block:: c++40 41  std::cout << static_cast<char>(v);42 43Options44-------45 46.. option:: AllowedTypes47 48  A semicolon-separated list of type names that will be treated like the ``char``49  type: the check will not report variables declared with with these types or50  explicit cast expressions to these types. Note that this distinguishes type51  aliases from the original type, so specifying e.g. ``unsigned char`` here52  will not suppress reports about ``uint8_t`` even if it is defined as a53  ``typedef`` alias for ``unsigned char``.54  Default is `unsigned char;signed char`.55 56.. option:: CastTypeName57 58  When `CastTypeName` is specified, the fix-it will use `CastTypeName` as the59  cast target type. Otherwise, fix-it will automatically infer the type.60