blob: e1e86d1a08cd0e78d98475e060b9d6f235abefb9 [file] [log] [blame]
Richard Trieuf935b562014-04-05 05:17:01 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
Weverything9740f9f2019-11-12 13:46:03 -08002// RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis %s
Richard Trieuf935b562014-04-05 05:17:01 +00003
4#define mydefine 2
5
George Burgess IVced56e62015-10-01 18:47:52 +00006enum Choices {
7 CHOICE_0 = 0,
8 CHOICE_1 = 1
9};
10
11enum Unchoices {
12 UNCHOICE_0 = 0,
13 UNCHOICE_1 = 1
14};
15
Richard Trieuf935b562014-04-05 05:17:01 +000016void 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 {{overlapping comparisons always evaluate to false}}
41 if (x > 2 && x < 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
42 if (x > 2 && x < 3) { } // expected-warning {{overlapping comparisons always evaluate to false}}
43 if (x > 0 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
44
45 if (x > 2 && x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
46 if (x > 2 && x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
47 if (x > 2 && x <= 3) { }
48
49 if (x >= 2 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
50 if (x >= 2 && x < 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
51 if (x >= 2 && x < 3) { }
52 if (x >= 0 && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
53
54 if (x >= 2 && x <= 1) { } // expected-warning {{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 {{overlapping comparisons always evaluate to false}}
62 if (x == 2 && x > 3) { } // expected-warning {{overlapping comparisons always evaluate to false}}
63 if (x == 3 && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
64 if (3 == x && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
65
66 if (x == mydefine && x > 3) { }
67 if (x == (mydefine + 1) && x > 3) { }
George Burgess IVced56e62015-10-01 18:47:52 +000068
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 {{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
81void 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 {{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) { }
Richard Trieuf935b562014-04-05 05:17:01 +000092}
93
Richard Trieue9fa2662014-04-15 00:57:50 +000094// Don't generate a warning here.
95void array_out_of_bounds() {
96 int x;
97 int buffer[4];
98 x = (-7 > 0) ? (buffer[-7]) : 0;
99}
Richard Trieu6a6af522017-01-04 00:46:30 +0000100
101void 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
116void 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{{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{{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
131int 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{{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{{overlapping comparisons always evaluate to false}}
141
142 return x < 1 || x != 0;
143 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
144}
Richard Trieu6541c792019-09-21 02:37:10 +0000145
146int 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
153int negative_compare(int x) {
154 return x > -1 || x < 1;
155 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
156}
157
158int no_warning(unsigned x) {
159 return x >= 0 || x == 1;
160 // no warning since "x >= 0" is caught by a different tautological warning.
161}
Richard Trieu4c05de82019-09-21 03:02:26 +0000162
163struct A {
164 int x;
165 int y;
166};
167
168int 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{{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}