xref: /llvm-project/clang/test/SemaCXX/warn-bitwise-compare.cpp (revision fd874e5fb119e1d9f427a299ffa5bbabaeba9455)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-bitwise-compare %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused %s
3 
test(int x)4 void test(int x) {
5   bool b1 = (8 & x) == 3;
6   // expected-warning@-1 {{bitwise comparison always evaluates to false}}
7   bool b2 = x | 5;
8   // expected-warning@-1 {{bitwise or with non-zero value always evaluates to true}}
9   bool b3 = (x | 5);
10   // expected-warning@-1 {{bitwise or with non-zero value always evaluates to true}}
11   bool b4 = !!(x | 5);
12   // expected-warning@-1 {{bitwise or with non-zero value always evaluates to true}}
13 }
14 
15 template <int I, class T>  // silence
foo(int x)16 void foo(int x) {
17     bool b1 = (x & sizeof(T)) == 8;
18     bool b2 = (x & I) == 8;
19     bool b3 = (x & 4) == 8; // expected-warning {{bitwise comparison always evaluates to false}}
20 }
21 
run(int x)22 void run(int x) {
23     foo<4, int>(8); // expected-note {{in instantiation of function template specialization 'foo<4, int>' requested here}}
24 }
25