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

Carlos Galvez via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 23:18:23 PST 2025


================
@@ -0,0 +1,69 @@
+// RUN: %check_clang_tidy %s bugprone-unintended-char-ostream-output %t
+
+namespace std {
+
+template <class _CharT, class _Traits = void> class basic_ostream {
+public:
+  basic_ostream &operator<<(int);
+  basic_ostream &operator<<(unsigned int);
+};
+
+template <class CharT, class Traits>
+basic_ostream<CharT, Traits> &operator<<(basic_ostream<CharT, Traits> &, CharT);
+template <class CharT, class Traits>
+basic_ostream<CharT, Traits> &operator<<(basic_ostream<CharT, Traits> &, char);
+template <class _Traits>
+basic_ostream<char, _Traits> &operator<<(basic_ostream<char, _Traits> &, char);
+template <class _Traits>
+basic_ostream<char, _Traits> &operator<<(basic_ostream<char, _Traits> &,
+                                          signed char);
+template <class _Traits>
+basic_ostream<char, _Traits> &operator<<(basic_ostream<char, _Traits> &,
+                                          unsigned char);
+
+using ostream = basic_ostream<char>;
+
+} // namespace std
+
+class A : public std::ostream {};
+
+void origin_ostream(std::ostream &os) {
+  unsigned char unsigned_value = 9;
+  os << unsigned_value;
+  // CHECK-MESSAGES: [[@LINE-1]]:6: warning: ('unsigned char' passed to
+  // 'operator<<' outputs as character instead of integer
+
+  signed char signed_value = 9;
+  os << signed_value;
+  // CHECK-MESSAGES: [[@LINE-1]]:6: warning: ('signed char' passed to
+  // 'operator<<' outputs as character instead of integer
+
+  char char_value = 9;
+  os << char_value;
+}
+
+void based_on_ostream(A &os) {
+  unsigned char unsigned_value = 9;
+  os << unsigned_value;
+  // CHECK-MESSAGES: [[@LINE-1]]:6: warning: ('unsigned char' passed to
----------------
carlosgalvezp wrote:

Remove the opening parenthesis

    ('unsigned char'
    ^

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


More information about the cfe-commits mailing list