[clang-tools-extra] [clang-tidy] Add new check bugprone-unintended-char-ostream-output (PR #127720)

Oliver Stöneberg via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 17 07:30:43 PDT 2025


firewave wrote:

Some thoughts based on the warnings I am seeing in actual code.

---

Another way to fix this could be using `std::to_string()`.

---

I am seeing this with an enum type which might be valid, working code but have not looked into it yet.

---

The fix-it is problematic if it is a templated type and should probably be omitted in that case:
```
#include <cstdint>
#include <sstream>

template<typename T>
void func(const T& data)
{
    std::ostringstream ostr;
    ostr << data;
}

void f()
{
    func((char)0);
    func((int8_t)0);
    func("");
}
```

```
<source>:8:10: warning: 'signed char' passed to 'operator<<' outputs as character instead of integer. cast to 'unsigned int' to print numeric value or cast to 'char' to print as character [bugprone-unintended-char-ostream-output]
    8 |     ostr << data;
      |          ^  ~~~~
      |             static_cast<int>(data)
```
https://godbolt.org/z/93axK1E4M

https://github.com/llvm/llvm-project/pull/127720


More information about the cfe-commits mailing list