[clang] [clang] Add -Wbool-integral-comparison (PR #194180)

via cfe-commits cfe-commits at lists.llvm.org
Mon May 4 09:50:29 PDT 2026


================
@@ -0,0 +1,96 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wbool-integral-comparison %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wextra %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,priority -Wbool-integral-comparison -Wtautological-unsigned-zero-compare %s
+// RUN: %clang_cc1 -fsyntax-only -verify=wall -Wall %s
+
+// wall-no-diagnostics
+
+void integral_comparisons(bool b, char c, int i, unsigned u, bool other) {
+  (void)(b == c); // expected-warning {{comparison between 'bool' and integral type 'char' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+  (void)(c != b); // expected-warning {{comparison between 'bool' and integral type 'char' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+  (void)(b < i);  // expected-warning {{comparison between 'bool' and integral type 'int' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+  (void)(u >= b); // expected-warning {{comparison between 'bool' and integral type 'unsigned int' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+
+  (void)(b == other);
+  (void)(b < other);
+}
+
+void reference_operands(bool b, bool &br, int i, int &ir) {
+  (void)(br == i);  // expected-warning {{comparison between 'bool' and integral type 'int' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+  (void)(b == ir);  // expected-warning {{comparison between 'bool' and integral type 'int' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+  (void)(ir == br); // expected-warning {{comparison between 'bool' and integral type 'int' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+}
+
+void parenthesized_operands(bool b, char c) {
+  (void)((b) == (c)); // expected-warning {{comparison between 'bool' and integral type 'char' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+}
+
+void qualified_operands(const bool b, volatile char c) {
+  (void)(b == c); // expected-warning {{comparison between 'const bool' and integral type 'volatile char' is suspicious; the 'const bool' operand is converted to an integral value that can only be 0 or 1}}
+}
+
+typedef bool Bool;
+using Int = int;
+
+void alias_operands(Bool b, Int i) {
+  (void)(b == i); // expected-warning {{comparison between 'Bool' (aka 'bool') and integral type 'Int' (aka 'int') is suspicious; the 'Bool' operand is converted to an integral value that can only be 0 or 1}}
+}
+
+template <typename T> bool dependent_template_comparison(bool b, T t) {
+  return b == t;
+}
+
+void instantiate_template_comparison(bool b, int i) {
+  (void)dependent_template_comparison(b, i);
+}
+
+template <typename T> bool non_dependent_template_comparison(bool b, int i) {
+  return b == i; // expected-warning {{comparison between 'bool' and integral type 'int' is suspicious; the 'bool' operand is converted to an integral value that can only be 0 or 1}}
+}
+
+struct BitFields {
+  unsigned one_bit : 1;
----------------
Sirraide wrote:

On that note, can you add a test involving `unsigned _BitInt(1)` too?

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


More information about the cfe-commits mailing list