[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / bool-compare.cpp
blobfe47633bb23b6f34c8d73e4526b8ace216c3f449
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
4 void f(int x, int y, int z) {
6 bool a,b;
8 if(b > true) {} // expected-warning {{comparison of true with expression of type 'bool' is always false}}
9 if(b < true) {} // no warning
10 if(b >= true) {} // no warning
11 if(b <= true) {} // expected-warning {{comparison of true with expression of type 'bool' is always true}}
12 if(b == true) {} // no warning
13 if(b != true) {} // no warning
15 if(b > false) {} // no warning
16 if(b < false) {} // expected-warning {{comparison of false with expression of type 'bool' is always false}}
17 if(b >= false) {} // expected-warning {{comparison of false with expression of type 'bool' is always true}}
18 if(b <= false) {} // no warning
19 if(b == false) {} // no warning
20 if(b != false) {} // no warning
22 if(b > 1U){} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
24 if (a > b) {} // no warning
25 if (a < b) {} // no warning
26 if (a >= b) {} // no warning
27 if (a <= b) {} // no warning
28 if (a == b) {} // no warning
29 if (a != b) {} // no warning
31 if (a > 0) {} // no warning
32 if (a > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
33 if (a > 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
35 if (a >= 0) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
36 if (a >= 1) {} // no warning
37 if (a >= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
38 if (a >= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
40 if (a <= 0) {} // no warning
41 if (a <= 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
42 if (a <= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
43 if (a <= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
45 if (!a > 0) {} // no warning
46 if (!a > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
47 if (!a > 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
48 if (!a > y) {} // no warning
49 if (!a > b) {} // no warning
50 if (!a > -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
52 if (!a < 0) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
53 if (!a < 1) {} // no warning
54 if (!a < 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
55 if (!a < y) {} // no warning
56 if (!a < b) {} // no warning
57 if (!a < -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
59 if (!a >= 0) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
60 if (!a >= 1) {} // no warning
61 if (!a >= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
62 if (!a >= y) {} // no warning
63 if (!a >= b) {} // no warning
64 if (!a >= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
66 if (!a <= 0) {} // no warning
67 if (!a <= 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
68 if (!a <= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
69 if (!a <= y) {} // no warning
70 if (!a <= b) {} // no warning
71 if (!a <= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
73 if ((a||b) > 0) {} // no warning
74 if ((a||b) > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
75 if ((a||b) > 4) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
76 if ((a||b) > -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
78 if ((a&&b) > 0) {} // no warning
79 if ((a&&b) > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
80 if ((a&&b) > 4) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
82 if ((a<y) > 0) {} // no warning
83 if ((a<y) > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
84 if ((a<y) > 4) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
85 if ((a<y) > z) {} // no warning
86 if ((a<y) > -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
88 if ((a<y) == 0) {} // no warning
89 if ((a<y) == 1) {} // no warning
90 if ((a<y) == 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
91 if ((a<y) == z) {} // no warning
92 if ((a<y) == -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
94 if ((a<y) != 0) {} // no warning
95 if ((a<y) != 1) {} // no warning
96 if ((a<y) != 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
97 if ((a<y) != z) {} // no warning
98 if ((a<y) != -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
100 if ((a<y) == z) {} // no warning
101 if (a>y<z) {} // no warning
102 if ((a<y) > z) {} // no warning
103 if((a<y)>(z<y)) {} // no warning
104 if((a<y)==(z<y)){} // no warning
105 if((a<y)!=(z<y)){} // no warning
106 if((z==x)<(y==z)){} // no warning
107 if((a<y)!=((z==x)<(y==z))){} // no warning
110 if (0 > !a) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
111 if (1 > !a) {} // no warning
112 if (2 > !a) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
113 if (y > !a) {} // no warning
114 if (-1 > !a) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
116 if (0 < !a) {} // no warning
117 if (1 < !a) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
118 if (2 < !a) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
119 if (y < !a) {} // no warning
120 if (-1 < !a) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
123 if (0 >= !a) {} // no warning
124 if (1 >= !a) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
125 if (2 >= !a) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
126 if (y >= !a) {} // no warning
127 if (-1 >= !a) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
129 if (0 <= !a) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
130 if (1 <= !a) {} // no warning
131 if (2 <= !a) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
132 if (y <= !a) {} //
133 if (-1 <= !a) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
135 if (0 > (a||b)) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
136 if (1 > (a||b)) {} // no warning
137 if (4 > (a||b)) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
139 if (0 > (a&&b)) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
140 if (1 > (a&&b)) {} // no warning
141 if (4 > (a&&b)) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
143 if (0 > (a<y)) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
144 if (1 > (a<y)) {} // no warning
145 if (4 > (a<y)) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
146 if (z > (a<y)) {} //
147 if (-1 > (a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
149 if (0 == (a<y)) {} // no warning
150 if (1 == (a<y)) {} // no warning
151 if (2 == (a<y)) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
152 if (z == (a<y)) {} // no warning
153 if (-1 == (a<y)){} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
155 if (0 !=(a<y)) {} // no warning
156 if (1 !=(a<y)) {} // no warning
157 if (2 !=(a<y)) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
158 if (z !=(a<y)) {} // no warning
159 if (-1 !=(a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
161 if (z ==(a<y)) {} // no warning
162 if (z<a>y) {} // no warning
163 if (z > (a<y)) {} // no warning
164 if((z<y)>(a<y)) {} // no warning
165 if((z<y)==(a<y)){} // no warning
166 if((z<y)!=(a<y)){} // no warning
167 if((y==z)<(z==x)){} // no warning
168 if(((z==x)<(y==z))!=(a<y)){} // no warning
170 if(((z==x)<(-1==z))!=(a<y)){} // no warning
171 if(((z==x)<(z==-1))!=(a<y)){} // no warning
172 if(((z==x)<-1)!=(a<y)){} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
173 if(((z==x)< 2)!=(a<y)){} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
174 if(((z==x)<(z>2))!=(a<y)){} // no warning
179 template<typename T, typename U, typename V> struct X6 {
180 U f(T t, U u, V v) {
181 // IfStmt
182 if (t > 0)
183 return u;
184 else {
185 if (t < 0)
186 return v; // expected-error{{cannot initialize return object of type}}
188 bool r;
189 // FIXME: We should warn here, DiagRuntimeBehavior does currently not detect this.
190 if(r<0){}
192 if (T x = t) {
193 t = x;
195 return v; // expected-error{{cannot initialize return object of type}}
199 struct ConvertibleToInt {
200 operator int() const;
203 template struct X6<ConvertibleToInt, float, char>;
204 template struct X6<bool, int, int*>; // expected-note{{instantiation}}