xref: /aosp_15_r20/external/clang/test/SemaCXX/bool-compare.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li 
f(int x,int y,int z)4*67e74705SXin Li void f(int x, int y, int z) {
5*67e74705SXin Li 
6*67e74705SXin Li   bool a,b;
7*67e74705SXin Li 
8*67e74705SXin Li   if(b > true)    {} // expected-warning {{comparison of true with expression of type 'bool' is always false}}
9*67e74705SXin Li   if(b < true)    {} // no warning
10*67e74705SXin Li   if(b >= true)   {} // no warning
11*67e74705SXin Li   if(b <= true)   {} // expected-warning {{comparison of true with expression of type 'bool' is always true}}
12*67e74705SXin Li   if(b == true)   {} // no warning
13*67e74705SXin Li   if(b != true)   {} // no warning
14*67e74705SXin Li 
15*67e74705SXin Li   if(b > false)   {} // no warning
16*67e74705SXin Li   if(b < false)   {} // expected-warning {{comparison of false with expression of type 'bool' is always false}}
17*67e74705SXin Li   if(b >= false)  {} // expected-warning {{comparison of false with expression of type 'bool' is always true}}
18*67e74705SXin Li   if(b <= false)  {} // no warning
19*67e74705SXin Li   if(b == false)  {} // no warning
20*67e74705SXin Li   if(b != false)  {} // no warning
21*67e74705SXin Li 
22*67e74705SXin Li   if(b > 1U){} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
23*67e74705SXin Li 
24*67e74705SXin Li   if (a > b)      {} // no warning
25*67e74705SXin Li   if (a < b)      {} // no warning
26*67e74705SXin Li   if (a >= b)     {} // no warning
27*67e74705SXin Li   if (a <= b)     {} // no warning
28*67e74705SXin Li   if (a == b)     {} // no warning
29*67e74705SXin Li   if (a != b)     {} // no warning
30*67e74705SXin Li 
31*67e74705SXin Li   if (a > 0) {} // no warning
32*67e74705SXin Li   if (a > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
33*67e74705SXin Li   if (a > 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
34*67e74705SXin Li 
35*67e74705SXin Li   if (a >= 0) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
36*67e74705SXin Li   if (a >= 1) {} // no warning
37*67e74705SXin Li   if (a >= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
38*67e74705SXin Li   if (a >= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
39*67e74705SXin Li 
40*67e74705SXin Li   if (a <= 0) {} // no warning
41*67e74705SXin Li   if (a <= 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
42*67e74705SXin Li   if (a <= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
43*67e74705SXin Li   if (a <= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
44*67e74705SXin Li 
45*67e74705SXin Li   if (!a > 0)     {} // no warning
46*67e74705SXin Li   if (!a > 1)     {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
47*67e74705SXin Li   if (!a > 2)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
48*67e74705SXin Li   if (!a > y)     {} // no warning
49*67e74705SXin Li   if (!a > b)     {} // no warning
50*67e74705SXin Li   if (!a > -1)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
51*67e74705SXin Li 
52*67e74705SXin Li   if (!a < 0)     {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
53*67e74705SXin Li   if (!a < 1)     {} // no warning
54*67e74705SXin Li   if (!a < 2)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
55*67e74705SXin Li   if (!a < y)     {} // no warning
56*67e74705SXin Li   if (!a < b)     {} // no warning
57*67e74705SXin Li   if (!a < -1)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
58*67e74705SXin Li 
59*67e74705SXin Li   if (!a >= 0)    {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
60*67e74705SXin Li   if (!a >= 1)    {} // no warning
61*67e74705SXin Li   if (!a >= 2)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
62*67e74705SXin Li   if (!a >= y)    {} // no warning
63*67e74705SXin Li   if (!a >= b)    {} // no warning
64*67e74705SXin Li   if (!a >= -1)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
65*67e74705SXin Li 
66*67e74705SXin Li   if (!a <= 0)    {} // no warning
67*67e74705SXin Li   if (!a <= 1)    {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
68*67e74705SXin Li   if (!a <= 2)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
69*67e74705SXin Li   if (!a <= y)    {} // no warning
70*67e74705SXin Li   if (!a <= b)    {} // no warning
71*67e74705SXin Li   if (!a <= -1)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
72*67e74705SXin Li 
73*67e74705SXin Li   if ((a||b) > 0) {} // no warning
74*67e74705SXin Li   if ((a||b) > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
75*67e74705SXin Li   if ((a||b) > 4) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
76*67e74705SXin Li   if ((a||b) > -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
77*67e74705SXin Li 
78*67e74705SXin Li   if ((a&&b) > 0) {} // no warning
79*67e74705SXin Li   if ((a&&b) > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
80*67e74705SXin Li   if ((a&&b) > 4) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
81*67e74705SXin Li 
82*67e74705SXin Li   if ((a<y) > 0)  {} // no warning
83*67e74705SXin Li   if ((a<y) > 1)  {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
84*67e74705SXin Li   if ((a<y) > 4)  {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
85*67e74705SXin Li   if ((a<y) > z)  {} // no warning
86*67e74705SXin Li   if ((a<y) > -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
87*67e74705SXin Li 
88*67e74705SXin Li   if ((a<y) == 0) {} // no warning
89*67e74705SXin Li   if ((a<y) == 1) {} // no warning
90*67e74705SXin Li   if ((a<y) == 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
91*67e74705SXin Li   if ((a<y) == z) {} // no warning
92*67e74705SXin Li   if ((a<y) == -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
93*67e74705SXin Li 
94*67e74705SXin Li   if ((a<y) != 0) {} // no warning
95*67e74705SXin Li   if ((a<y) != 1) {} // no warning
96*67e74705SXin Li   if ((a<y) != 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
97*67e74705SXin Li   if ((a<y) != z) {} // no warning
98*67e74705SXin Li   if ((a<y) != -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
99*67e74705SXin Li 
100*67e74705SXin Li   if ((a<y) == z) {} // no warning
101*67e74705SXin Li   if (a>y<z)      {} // no warning
102*67e74705SXin Li   if ((a<y) > z)  {} // no warning
103*67e74705SXin Li   if((a<y)>(z<y)) {} // no warning
104*67e74705SXin Li   if((a<y)==(z<y)){} // no warning
105*67e74705SXin Li   if((a<y)!=(z<y)){} // no warning
106*67e74705SXin Li   if((z==x)<(y==z)){}  // no warning
107*67e74705SXin Li   if((a<y)!=((z==x)<(y==z))){} // no warning
108*67e74705SXin Li 
109*67e74705SXin Li 
110*67e74705SXin Li   if (0 > !a)     {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
111*67e74705SXin Li   if (1 > !a)     {} // no warning
112*67e74705SXin Li   if (2 > !a)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
113*67e74705SXin Li   if (y > !a)     {} // no warning
114*67e74705SXin Li   if (-1 > !a)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
115*67e74705SXin Li 
116*67e74705SXin Li   if (0 < !a)     {} // no warning
117*67e74705SXin Li   if (1 < !a)     {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
118*67e74705SXin Li   if (2 < !a)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
119*67e74705SXin Li   if (y < !a)     {} // no warning
120*67e74705SXin Li   if (-1 < !a)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
121*67e74705SXin Li 
122*67e74705SXin Li 
123*67e74705SXin Li   if (0 >= !a)    {} // no warning
124*67e74705SXin Li   if (1 >= !a)    {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
125*67e74705SXin Li   if (2 >= !a)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
126*67e74705SXin Li   if (y >= !a)    {} // no warning
127*67e74705SXin Li   if (-1 >= !a)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
128*67e74705SXin Li 
129*67e74705SXin Li   if (0 <= !a)    {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
130*67e74705SXin Li   if (1 <= !a)    {} // no warning
131*67e74705SXin Li   if (2 <= !a)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
132*67e74705SXin Li   if (y <= !a)    {} //
133*67e74705SXin Li   if (-1 <= !a)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
134*67e74705SXin Li 
135*67e74705SXin Li   if (0 > (a||b)) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
136*67e74705SXin Li   if (1 > (a||b)) {} // no warning
137*67e74705SXin Li   if (4 > (a||b)) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
138*67e74705SXin Li 
139*67e74705SXin Li   if (0 > (a&&b)) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
140*67e74705SXin Li   if (1 > (a&&b)) {} // no warning
141*67e74705SXin Li   if (4 > (a&&b)) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
142*67e74705SXin Li 
143*67e74705SXin Li   if (0 > (a<y))  {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
144*67e74705SXin Li   if (1 > (a<y))  {} // no warning
145*67e74705SXin Li   if (4 > (a<y))  {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
146*67e74705SXin Li   if (z > (a<y))  {} //
147*67e74705SXin Li   if (-1 > (a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
148*67e74705SXin Li 
149*67e74705SXin Li   if (0 == (a<y)) {} // no warning
150*67e74705SXin Li   if (1 == (a<y)) {} // no warning
151*67e74705SXin Li   if (2 == (a<y)) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
152*67e74705SXin Li   if (z == (a<y)) {} // no warning
153*67e74705SXin Li   if (-1 == (a<y)){} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
154*67e74705SXin Li 
155*67e74705SXin Li   if (0 !=(a<y))  {} // no warning
156*67e74705SXin Li   if (1 !=(a<y))  {} // no warning
157*67e74705SXin Li   if (2 !=(a<y))  {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
158*67e74705SXin Li   if (z !=(a<y))  {} // no warning
159*67e74705SXin Li   if (-1 !=(a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
160*67e74705SXin Li 
161*67e74705SXin Li   if (z ==(a<y))  {} // no warning
162*67e74705SXin Li   if (z<a>y)      {} // no warning
163*67e74705SXin Li   if (z > (a<y))  {} // no warning
164*67e74705SXin Li   if((z<y)>(a<y)) {} // no warning
165*67e74705SXin Li   if((z<y)==(a<y)){} // no warning
166*67e74705SXin Li   if((z<y)!=(a<y)){} // no warning
167*67e74705SXin Li   if((y==z)<(z==x)){} // no warning
168*67e74705SXin Li   if(((z==x)<(y==z))!=(a<y)){}  // no warning
169*67e74705SXin Li 
170*67e74705SXin Li   if(((z==x)<(-1==z))!=(a<y)){} // no warning
171*67e74705SXin Li   if(((z==x)<(z==-1))!=(a<y)){} // no warning
172*67e74705SXin Li   if(((z==x)<-1)!=(a<y)){} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
173*67e74705SXin Li   if(((z==x)< 2)!=(a<y)){} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
174*67e74705SXin Li   if(((z==x)<(z>2))!=(a<y)){} // no warning
175*67e74705SXin Li 
176*67e74705SXin Li }
177*67e74705SXin Li 
178*67e74705SXin Li 
179*67e74705SXin Li template<typename T, typename U, typename V> struct X6 {
fX6180*67e74705SXin Li   U f(T t, U u, V v) {
181*67e74705SXin Li     // IfStmt
182*67e74705SXin Li     if (t > 0)
183*67e74705SXin Li       return u;
184*67e74705SXin Li     else {
185*67e74705SXin Li       if (t < 0)
186*67e74705SXin Li         return v; // expected-error{{cannot initialize return object of type}}
187*67e74705SXin Li     }
188*67e74705SXin Li     bool r;
189*67e74705SXin Li     // FIXME: We should warn here, DiagRuntimeBehavior does currently not detect this.
190*67e74705SXin Li     if(r<0){}
191*67e74705SXin Li 
192*67e74705SXin Li     if (T x = t) {
193*67e74705SXin Li       t = x;
194*67e74705SXin Li     }
195*67e74705SXin Li     return v; // expected-error{{cannot initialize return object of type}}
196*67e74705SXin Li   }
197*67e74705SXin Li };
198*67e74705SXin Li 
199*67e74705SXin Li struct ConvertibleToInt {
200*67e74705SXin Li   operator int() const;
201*67e74705SXin Li };
202*67e74705SXin Li 
203*67e74705SXin Li template struct X6<ConvertibleToInt, float, char>;
204*67e74705SXin Li template struct X6<bool, int, int*>; // expected-note{{instantiation}}
205*67e74705SXin Li 
206*67e74705SXin Li 
207*67e74705SXin Li 
208