Clang Project

clang_source_code/test/SemaCXX/bool-compare.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3
4void f(int x, int y, int z) {
5
6  bool a,b;
7
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
14
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
21
22  if(b > 1U){} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
23
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
30
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}}
34
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}}
39
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}}
44
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}}
51
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}}
58
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}}
65
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}}
72
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}}
77
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}}
81
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}}
87
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}}
93
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}}
99
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
108
109
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}}
115
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}}
121
122
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}}
128
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}}
134
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}}
138
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}}
142
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}}
148
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}}
154
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}}
160
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
169
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
175
176}
177
178
179template<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}}
187    }
188    bool r;
189    // FIXME: We should warn here, DiagRuntimeBehavior does currently not detect this.
190    if(r<0){}
191
192    if (T x = t) {
193      t = x;
194    }
195    return v; // expected-error{{cannot initialize return object of type}}
196  }
197};
198
199struct ConvertibleToInt {
200  operator int() const;
201};
202
203template struct X6<ConvertibleToInt, float, char>;
204template struct X6<bool, int, int*>; // expected-note{{instantiation}}
205
206
207
208