| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare -Wno-enum-compare %s |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis -Wno-enum-compare %s |
| 3 |
|
| 4 |
#define mydefine 2 |
| 5 |
|
| 6 |
enum Choices { |
| 7 |
CHOICE_0 = 0, |
| 8 |
CHOICE_1 = 1 |
| 9 |
}; |
| 10 |
|
| 11 |
enum Unchoices { |
| 12 |
UNCHOICE_0 = 0, |
| 13 |
UNCHOICE_1 = 1 |
| 14 |
}; |
| 15 |
|
| 16 |
void f(int x) { |
| 17 |
int y = 0; |
| 18 |
|
| 19 |
// > || < |
| 20 |
if (x > 2 || x < 1) { } |
| 21 |
if (x > 2 || x < 2) { } |
| 22 |
if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 23 |
if (x > 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 24 |
if (x > 0 || x < 0) { } |
| 25 |
|
| 26 |
if (x > 2 || x <= 1) { } |
| 27 |
if (x > 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 28 |
if (x > 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 29 |
|
| 30 |
if (x >= 2 || x < 1) { } |
| 31 |
if (x >= 2 || x < 2) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 32 |
if (x >= 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 33 |
|
| 34 |
if (x >= 2 || x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 35 |
if (x >= 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 36 |
if (x >= 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 37 |
if (x >= 0 || x <= 0) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 38 |
|
| 39 |
// > && < |
| 40 |
if (x > 2 && x < 1) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 41 |
if (x > 2 && x < 2) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 42 |
if (x > 2 && x < 3) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 43 |
if (x > 0 && x < 1) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 44 |
|
| 45 |
if (x > 2 && x <= 1) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 46 |
if (x > 2 && x <= 2) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 47 |
if (x > 2 && x <= 3) { } |
| 48 |
|
| 49 |
if (x >= 2 && x < 1) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 50 |
if (x >= 2 && x < 2) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 51 |
if (x >= 2 && x < 3) { } |
| 52 |
if (x >= 0 && x < 0) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 53 |
|
| 54 |
if (x >= 2 && x <= 1) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 55 |
if (x >= 2 && x <= 2) { } |
| 56 |
if (x >= 2 && x <= 3) { } |
| 57 |
|
| 58 |
// !=, ==, .. |
| 59 |
if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 60 |
if (x != 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 61 |
if (x == 2 && x == 3) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 62 |
if (x == 2 && x > 3) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 63 |
if (x == 3 && x < 0) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 64 |
if (3 == x && x < 0) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 65 |
|
| 66 |
if (x == mydefine && x > 3) { } |
| 67 |
if (x == (mydefine + 1) && x > 3) { } |
| 68 |
|
| 69 |
if (x != CHOICE_0 || x != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 70 |
if (x == CHOICE_0 && x == CHOICE_1) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 71 |
|
| 72 |
// Don't warn if comparing x to different types |
| 73 |
if (x == CHOICE_0 && x == 1) { } |
| 74 |
if (x != CHOICE_0 || x != 1) { } |
| 75 |
|
| 76 |
// "Different types" includes different enums |
| 77 |
if (x == CHOICE_0 && x == UNCHOICE_1) { } |
| 78 |
if (x != CHOICE_0 || x != UNCHOICE_1) { } |
| 79 |
} |
| 80 |
|
| 81 |
void enums(enum Choices c) { |
| 82 |
if (c != CHOICE_0 || c != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}} |
| 83 |
if (c == CHOICE_0 && c == CHOICE_1) { } // expected-warning {{non-overlapping comparisons always evaluate to false}} |
| 84 |
|
| 85 |
// Don't warn if comparing x to different types |
| 86 |
if (c == CHOICE_0 && c == 1) { } |
| 87 |
if (c != CHOICE_0 || c != 1) { } |
| 88 |
|
| 89 |
// "Different types" includes different enums |
| 90 |
if (c == CHOICE_0 && c == UNCHOICE_1) { } |
| 91 |
if (c != CHOICE_0 || c != UNCHOICE_1) { } |
| 92 |
} |
| 93 |
|
| 94 |
// Don't generate a warning here. |
| 95 |
void array_out_of_bounds(void) { |
| 96 |
int x; |
| 97 |
int buffer[4]; |
| 98 |
x = (-7 > 0) ? (buffer[-7]) : 0; |
| 99 |
} |
| 100 |
|
| 101 |
void bool_contexts(int x) { |
| 102 |
if (x > 4 || x < 10) {} |
| 103 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 104 |
for (;x > 4 || x < 10;) {} |
| 105 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 106 |
while (x > 4 || x < 10) {} |
| 107 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 108 |
do {} while (x > 4 || x < 10); |
| 109 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 110 |
x = (x > 4 || x < 10) ? 1 : 2; |
| 111 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 112 |
if ((void)5, x > 4 || x < 10) {} |
| 113 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 114 |
} |
| 115 |
|
| 116 |
void assignment(int x) { |
| 117 |
int a = x > 4 || x < 10; |
| 118 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 119 |
int b = x < 2 && x > 5; |
| 120 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 121 |
|
| 122 |
int c = x != 1 || x != 3; |
| 123 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 124 |
int d = x == 1 && x == 2; |
| 125 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 126 |
|
| 127 |
int e = x < 1 || x != 0; |
| 128 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 129 |
} |
| 130 |
|
| 131 |
int returns(int x) { |
| 132 |
return x > 4 || x < 10; |
| 133 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 134 |
return x < 2 && x > 5; |
| 135 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 136 |
|
| 137 |
return x != 1 || x != 3; |
| 138 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 139 |
return x == 1 && x == 2; |
| 140 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 141 |
|
| 142 |
return x < 1 || x != 0; |
| 143 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 144 |
} |
| 145 |
|
| 146 |
int integer_conversion(unsigned x, int y) { |
| 147 |
return x > 4 || x < 10; |
| 148 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 149 |
return y > 4u || y < 10u; |
| 150 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 151 |
} |
| 152 |
|
| 153 |
int negative_compare(int x) { |
| 154 |
return x > -1 || x < 1; |
| 155 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 156 |
} |
| 157 |
|
| 158 |
int no_warning(unsigned x) { |
| 159 |
return x >= 0 || x == 1; |
| 160 |
// no warning since "x >= 0" is caught by a different tautological warning. |
| 161 |
} |
| 162 |
|
| 163 |
struct A { |
| 164 |
int x; |
| 165 |
int y; |
| 166 |
}; |
| 167 |
|
| 168 |
int struct_test(struct A a) { |
| 169 |
return a.x > 5 && a.y < 1; // no warning, different variables |
| 170 |
|
| 171 |
return a.x > 5 && a.x < 1; |
| 172 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 173 |
return a.y == 1 || a.y != 1; |
| 174 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 175 |
} |
| 176 |
|
| 177 |
void char_tests(char c) { |
| 178 |
if (c > 'a' || c < 'z') {} |
| 179 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 180 |
if (c > 'z' && c < 'a') {} |
| 181 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 182 |
if (c == 'a' && c == 'z') {} |
| 183 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 184 |
if (c != 'a' || c != 'z') {} |
| 185 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 186 |
} |
| 187 |
|
| 188 |
void float_tests(float f) { |
| 189 |
if (f > 1.0 || f < 2.0) {} |
| 190 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 191 |
if (f > 2.0 && f < 1.0) {} |
| 192 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 193 |
if (f == 1.0 && f == 2.0) {} |
| 194 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 195 |
if (f != 1.0 || f != 2.0) {} |
| 196 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 197 |
} |
| 198 |
|
| 199 |
void double_tests(double d) { |
| 200 |
if (d > 3.5 || d < 4.5) {} |
| 201 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 202 |
if (d > 4.5 && d < 3.5) {} |
| 203 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 204 |
if (d == 3.5 && d == 4.5) {} |
| 205 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 206 |
if (d != 3.5 || d != 4.5) {} |
| 207 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 208 |
} |
| 209 |
|
| 210 |
void mixed_float_double_tests(float f, double d) { |
| 211 |
if (f > 1.0 || d < 2.0) {} |
| 212 |
// no warning, different types |
| 213 |
if (f > 2.0 && d < 1.0) {} |
| 214 |
// no warning, different types |
| 215 |
if (f == 1.0 && d == 2.0) {} |
| 216 |
// no warning, different types |
| 217 |
if (f != 1.0 || d != 2.0) {} |
| 218 |
// no warning, different types |
| 219 |
} |
| 220 |
|
| 221 |
void float_edge_cases(float f) { |
| 222 |
if (f > 0.0 || f < 0.0) {} |
| 223 |
// no-warning |
| 224 |
if (f > 0.0 && f < 0.0) {} |
| 225 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}}} |
| 226 |
if (f == 0.0 && f == -0.0) {} |
| 227 |
// no-warning |
| 228 |
if (f != 0.0 || f != -0.0) {} |
| 229 |
// no warning |
| 230 |
} |
| 231 |
|
| 232 |
void double_edge_cases(double d) { |
| 233 |
if (d > 0.0 || d < 0.0) {} |
| 234 |
// no-warning |
| 235 |
if (d > 0.0 && d < 0.0) {} |
| 236 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 237 |
if (d == 0.0 && d == -0.0) {} |
| 238 |
// no warning |
| 239 |
if (d != 0.0 || d != -0.0) {} |
| 240 |
// no warning |
| 241 |
} |
| 242 |
|
| 243 |
void float_int_literal_tests(float f) { |
| 244 |
if (f > 1 || f < 2) {} |
| 245 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 246 |
if (f > 2 && f < 1) {} |
| 247 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 248 |
if (f == 1 && f == 2) {} |
| 249 |
// expected-warning@-1{{non-overlapping comparisons always evaluate to false}} |
| 250 |
if (f != 1 || f != 2) {} |
| 251 |
// expected-warning@-1{{overlapping comparisons always evaluate to true}} |
| 252 |
} |