================ @@ -0,0 +1,45 @@ +.. title:: clang-tidy - bugprone-unintended-char-ostream-output + +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. + +.. code-block:: c++ + + 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``. + +.. code-block:: c++ + + 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. ---------------- carlosgalvezp wrote:
Would be good to explain that this triggers an implicit promotion to `int` (even if the variable is `unsigned`). https://github.com/llvm/llvm-project/pull/127720 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits