blob: 6299c511fe230bde00c9de4a4508a019b2efabaf [file] [log] [blame]
Richard Trieuf935b562014-04-05 05:17:01 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
2
3#define mydefine 2
4
George Burgess IVced56e62015-10-01 18:47:52 +00005enum Choices {
6 CHOICE_0 = 0,
7 CHOICE_1 = 1
8};
9
10enum Unchoices {
11 UNCHOICE_0 = 0,
12 UNCHOICE_1 = 1
13};
14
Richard Trieuf935b562014-04-05 05:17:01 +000015void f(int x) {
16 int y = 0;
17
18 // > || <
19 if (x > 2 || x < 1) { }
20 if (x > 2 || x < 2) { }
21 if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
22 if (x > 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
23 if (x > 0 || x < 0) { }
24
25 if (x > 2 || x <= 1) { }
26 if (x > 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
27 if (x > 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
28
29 if (x >= 2 || x < 1) { }
30 if (x >= 2 || x < 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
31 if (x >= 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
32
33 if (x >= 2 || x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
34 if (x >= 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
35 if (x >= 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
36 if (x >= 0 || x <= 0) { } // expected-warning {{overlapping comparisons always evaluate to true}}
37
38 // > && <
39 if (x > 2 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
40 if (x > 2 && x < 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
41 if (x > 2 && x < 3) { } // expected-warning {{overlapping comparisons always evaluate to false}}
42 if (x > 0 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
43
44 if (x > 2 && x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
45 if (x > 2 && x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
46 if (x > 2 && x <= 3) { }
47
48 if (x >= 2 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
49 if (x >= 2 && x < 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
50 if (x >= 2 && x < 3) { }
51 if (x >= 0 && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
52
53 if (x >= 2 && x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
54 if (x >= 2 && x <= 2) { }
55 if (x >= 2 && x <= 3) { }
56
57 // !=, ==, ..
58 if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
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 false}}
61 if (x == 2 && x > 3) { } // expected-warning {{overlapping comparisons always evaluate to false}}
62 if (x == 3 && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
63 if (3 == x && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
64
65 if (x == mydefine && x > 3) { }
66 if (x == (mydefine + 1) && x > 3) { }
George Burgess IVced56e62015-10-01 18:47:52 +000067
68 if (x != CHOICE_0 || x != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
69 if (x == CHOICE_0 && x == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
70
71 // Don't warn if comparing x to different types
72 if (x == CHOICE_0 && x == 1) { }
73 if (x != CHOICE_0 || x != 1) { }
74
75 // "Different types" includes different enums
76 if (x == CHOICE_0 && x == UNCHOICE_1) { }
77 if (x != CHOICE_0 || x != UNCHOICE_1) { }
78}
79
80void enums(enum Choices c) {
81 if (c != CHOICE_0 || c != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
82 if (c == CHOICE_0 && c == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
83
84 // Don't warn if comparing x to different types
85 if (c == CHOICE_0 && c == 1) { }
86 if (c != CHOICE_0 || c != 1) { }
87
88 // "Different types" includes different enums
89 if (c == CHOICE_0 && c == UNCHOICE_1) { }
90 if (c != CHOICE_0 || c != UNCHOICE_1) { }
Richard Trieuf935b562014-04-05 05:17:01 +000091}
92
Richard Trieue9fa2662014-04-15 00:57:50 +000093// Don't generate a warning here.
94void array_out_of_bounds() {
95 int x;
96 int buffer[4];
97 x = (-7 > 0) ? (buffer[-7]) : 0;
98}
Richard Trieu6a6af522017-01-04 00:46:30 +000099
100void bool_contexts(int x) {
101 if (x > 4 || x < 10) {}
102 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
103 for (;x > 4 || x < 10;) {}
104 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
105 while (x > 4 || x < 10) {}
106 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
107 do {} while (x > 4 || x < 10);
108 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
109 x = (x > 4 || x < 10) ? 1 : 2;
110 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
111 if ((void)5, x > 4 || x < 10) {}
112 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
113}
114
115void assignment(int x) {
116 int a = x > 4 || x < 10;
117 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
118 int b = x < 2 && x > 5;
119 // expected-warning@-1{{overlapping comparisons always evaluate to false}}
120
121 int c = x != 1 || x != 3;
122 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
123 int d = x == 1 && x == 2;
124 // expected-warning@-1{{overlapping comparisons always evaluate to false}}
125
126 int e = x < 1 || x != 0;
127 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
128}
129
130int returns(int x) {
131 return x > 4 || x < 10;
132 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
133 return x < 2 && x > 5;
134 // expected-warning@-1{{overlapping comparisons always evaluate to false}}
135
136 return x != 1 || x != 3;
137 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
138 return x == 1 && x == 2;
139 // expected-warning@-1{{overlapping comparisons always evaluate to false}}
140
141 return x < 1 || x != 0;
142 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
143}