blob: 8bb82372b534adef2fce576cc6ae3fa32c86e80e [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.IdenticalExpr -w -verify %s
Jordan Rose51327f92013-11-08 01:15:39 +00002
3/* Only one expected warning per function allowed at the very end. */
4
Jordan Rose60bd88d2013-12-10 18:18:06 +00005int func(void)
6{
7 return 0;
8}
9
10int func2(void)
11{
12 return 0;
13}
14
15int funcParam(int a)
16{
17 return 0;
18}
19
Jordan Rose51327f92013-11-08 01:15:39 +000020/* '!=' operator*/
21
22/* '!=' with float */
23int checkNotEqualFloatLiteralCompare1(void) {
24 return (5.14F != 5.14F); // no warning
25}
26
27int checkNotEqualFloatLiteralCompare2(void) {
28 return (6.14F != 7.14F); // no warning
29}
30
31int checkNotEqualFloatDeclCompare1(void) {
32 float f = 7.1F;
33 float g = 7.1F;
34 return (f != g); // no warning
35}
36
37int checkNotEqualFloatDeclCompare12(void) {
38 float f = 7.1F;
39 return (f != f); // no warning
40}
41
42int checkNotEqualFloatDeclCompare3(void) {
43 float f = 7.1F;
44 return (f != 7.1F); // no warning
45}
46
47int checkNotEqualFloatDeclCompare4(void) {
48 float f = 7.1F;
49 return (7.1F != f); // no warning
50}
51
52int checkNotEqualFloatDeclCompare5(void) {
53 float f = 7.1F;
54 int t = 7;
55 return (t != f); // no warning
56}
57
58int checkNotEqualFloatDeclCompare6(void) {
59 float f = 7.1F;
60 int t = 7;
61 return (f != t); // no warning
62}
63
64
65
66int checkNotEqualCastFloatDeclCompare11(void) {
67 float f = 7.1F;
68 return ((int)f != (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
69}
70int checkNotEqualCastFloatDeclCompare12(void) {
71 float f = 7.1F;
72 return ((char)f != (int)f); // no warning
73}
74int checkNotEqualBinaryOpFloatCompare1(void) {
75 int res;
76 float f= 3.14F;
77 res = (f + 3.14F != f + 3.14F); // no warning
78 return (0);
79}
80int checkNotEqualBinaryOpFloatCompare2(void) {
81 float f = 7.1F;
82 float g = 7.1F;
83 return (f + 3.14F != g + 3.14F); // no warning
84}
85int checkNotEqualBinaryOpFloatCompare3(void) {
86 int res;
87 float f= 3.14F;
88 res = ((int)f + 3.14F != (int)f + 3.14F); // no warning
89 return (0);
90}
91int checkNotEqualBinaryOpFloatCompare4(void) {
92 int res;
93 float f= 3.14F;
94 res = ((int)f + 3.14F != (char)f + 3.14F); // no warning
95 return (0);
96}
97
98int checkNotEqualNestedBinaryOpFloatCompare1(void) {
99 int res;
100 int t= 1;
101 int u= 2;
102 float f= 3.14F;
103 res = (((int)f + (3.14F - u)*t) != ((int)f + (3.14F - u)*t)); // no warning
104 return (0);
105}
106
107int checkNotEqualNestedBinaryOpFloatCompare2(void) {
108 int res;
109 int t= 1;
110 int u= 2;
111 float f= 3.14F;
112 res = (((int)f + (u - 3.14F)*t) != ((int)f + (3.14F - u)*t)); // no warning
113 return (0);
114}
115
116int checkNotEqualNestedBinaryOpFloatCompare3(void) {
117 int res;
118 int t= 1;
119 int u= 2;
120 float f= 3.14F;
121 res = (((int)f + (u - 3.14F)*t) != ((int)f + (3.14F - u)*(f + t != f + t))); // no warning
122 return (0);
123}
124
125
126
127
128/* end '!=' with float*/
129
130/* '!=' with int*/
131
132int checkNotEqualIntLiteralCompare1(void) {
133 return (5 != 5); // expected-warning {{comparison of identical expressions always evaluates to false}}
134}
135
136int checkNotEqualIntLiteralCompare2(void) {
137 return (6 != 7); // no warning
138}
139
140int checkNotEqualIntDeclCompare1(void) {
141 int f = 7;
142 int g = 7;
143 return (f != g); // no warning
144}
145
146int checkNotEqualIntDeclCompare3(void) {
147 int f = 7;
148 return (f != 7); // no warning
149}
150
151int checkNotEqualIntDeclCompare4(void) {
152 int f = 7;
153 return (7 != f); // no warning
154}
155
156int checkNotEqualCastIntDeclCompare11(void) {
157 int f = 7;
158 return ((int)f != (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
159}
160int checkNotEqualCastIntDeclCompare12(void) {
161 int f = 7;
162 return ((char)f != (int)f); // no warning
163}
164int checkNotEqualBinaryOpIntCompare1(void) {
165 int res;
166 int t= 1;
167 int u= 2;
168 int f= 4;
169 res = (f + 4 != f + 4); // expected-warning {{comparison of identical expressions always evaluates to false}}
170 return (0);
171}
172int checkNotEqualBinaryOpIntCompare2(void) {
173 int f = 7;
174 int g = 7;
175 return (f + 4 != g + 4); // no warning
176}
177
178
179int checkNotEqualBinaryOpIntCompare3(void) {
180 int res;
181 int t= 1;
182 int u= 2;
183 int f= 4;
184 res = ((int)f + 4 != (int)f + 4); // expected-warning {{comparison of identical expressions always evaluates to false}}
185 return (0);
186}
187int checkNotEqualBinaryOpIntCompare4(void) {
188 int res;
189 int t= 1;
190 int u= 2;
191 int f= 4;
192 res = ((int)f + 4 != (char)f + 4); // no warning
193 return (0);
194}
195int checkNotEqualBinaryOpIntCompare5(void) {
196 int res;
197 int t= 1;
198 int u= 2;
199 res = (u + t != u + t); // expected-warning {{comparison of identical expressions always evaluates to false}}
200 return (0);
201}
202
203int checkNotEqualNestedBinaryOpIntCompare1(void) {
204 int res;
205 int t= 1;
206 int u= 2;
207 int f= 3;
208 res = (((int)f + (3 - u)*t) != ((int)f + (3 - u)*t)); // expected-warning {{comparison of identical expressions always evaluates to false}}
209 return (0);
210}
211
212int checkNotEqualNestedBinaryOpIntCompare2(void) {
213 int res;
214 int t= 1;
215 int u= 2;
216 int f= 3;
217 res = (((int)f + (u - 3)*t) != ((int)f + (3 - u)*t)); // no warning
218 return (0);
219}
220
221int checkNotEqualNestedBinaryOpIntCompare3(void) {
222 int res;
223 int t= 1;
224 int u= 2;
225 int f= 3;
226 res = (((int)f + (u - 3)*t) != ((int)f + (3 - u)*(t + 1 != t + 1))); // expected-warning {{comparison of identical expressions always evaluates to false}}
227 return (0);
228}
229
230/* end '!=' int */
231
232
233
234/* '!=' with int pointer */
235
236int checkNotEqualIntPointerLiteralCompare1(void) {
237 int* p = 0;
238 return (p != 0); // no warning
239}
240
241int checkNotEqualIntPointerLiteralCompare2(void) {
242 return (6 != 7); // no warning
243}
244
245int checkNotEqualIntPointerDeclCompare1(void) {
246 int k = 3;
247 int* f = &k;
248 int* g = &k;
249 return (f != g); // no warning
250}
251
252int checkNotEqualCastIntPointerDeclCompare11(void) {
253 int k = 7;
254 int* f = &k;
255 return ((int*)f != (int*)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
256}
257int checkNotEqualCastIntPointerDeclCompare12(void) {
258 int k = 7;
259 int* f = &k;
260 return ((int*)((char*)f) != (int*)f); // no warning
261}
262int checkNotEqualBinaryOpIntPointerCompare1(void) {
263 int k = 7;
264 int res;
265 int* f= &k;
266 res = (f + 4 != f + 4); // expected-warning {{comparison of identical expressions always evaluates to false}}
267 return (0);
268}
269int checkNotEqualBinaryOpIntPointerCompare2(void) {
270 int k = 7;
271 int* f = &k;
272 int* g = &k;
273 return (f + 4 != g + 4); // no warning
274}
275
276
277int checkNotEqualBinaryOpIntPointerCompare3(void) {
278 int k = 7;
279 int res;
280 int* f= &k;
281 res = ((int*)f + 4 != (int*)f + 4); // expected-warning {{comparison of identical expressions always evaluates to false}}
282 return (0);
283}
284int checkNotEqualBinaryOpIntPointerCompare4(void) {
285 int k = 7;
286 int res;
287 int* f= &k;
288 res = ((int*)f + 4 != (int*)((char*)f) + 4); // no warning
289 return (0);
290}
291
292int checkNotEqualNestedBinaryOpIntPointerCompare1(void) {
293 int res;
294 int k = 7;
295 int t= 1;
296 int* u= &k+2;
297 int* f= &k+3;
298 res = ((f + (3)*t) != (f + (3)*t)); // expected-warning {{comparison of identical expressions always evaluates to false}}
299 return (0);
300}
301
302int checkNotEqualNestedBinaryOpIntPointerCompare2(void) {
303 int res;
304 int k = 7;
305 int t= 1;
306 int* u= &k+2;
307 int* f= &k+3;
308 res = (((3)*t + f) != (f + (3)*t)); // no warning
309 return (0);
310}
311/* end '!=' int* */
312
Jordan Rose60bd88d2013-12-10 18:18:06 +0000313/* '!=' with function*/
314
315int checkNotEqualSameFunction() {
316 unsigned a = 0;
317 unsigned b = 1;
318 int res = (a+func() != a+func()); // no warning
319 return (0);
320}
321
322int checkNotEqualDifferentFunction() {
323 unsigned a = 0;
324 unsigned b = 1;
325 int res = (a+func() != a+func2()); // no warning
326 return (0);
327}
328
329int checkNotEqualSameFunctionSameParam() {
330 unsigned a = 0;
331 unsigned b = 1;
332 int res = (a+funcParam(a) != a+funcParam(a)); // no warning
333 return (0);
334}
335
336int checkNotEqualSameFunctionDifferentParam() {
337 unsigned a = 0;
338 unsigned b = 1;
339 int res = (a+funcParam(a) != a+funcParam(b)); // no warning
340 return (0);
341}
342
343/* end '!=' with function*/
344
Jordan Rose51327f92013-11-08 01:15:39 +0000345/* end '!=' */
346
347
348
349/* EQ operator */
350
351int checkEqualIntPointerDeclCompare(void) {
352 int k = 3;
353 int* f = &k;
354 int* g = &k;
355 return (f == g); // no warning
356}
357
358int checkEqualIntPointerDeclCompare0(void) {
359 int k = 3;
360 int* f = &k;
361 return (f+1 == f+1); // expected-warning {{comparison of identical expressions always evaluates to true}}
362}
363
364/* EQ with float*/
365
366int checkEqualFloatLiteralCompare1(void) {
367 return (5.14F == 5.14F); // no warning
368}
369
370int checkEqualFloatLiteralCompare2(void) {
371 return (6.14F == 7.14F); // no warning
372}
373
374int checkEqualFloatDeclCompare1(void) {
375 float f = 7.1F;
376 float g = 7.1F;
377 return (f == g); // no warning
378}
379
380int checkEqualFloatDeclCompare12(void) {
381 float f = 7.1F;
382 return (f == f); // no warning
383}
384
385
386int checkEqualFloatDeclCompare3(void) {
387 float f = 7.1F;
388 return (f == 7.1F); // no warning
389}
390
391int checkEqualFloatDeclCompare4(void) {
392 float f = 7.1F;
393 return (7.1F == f); // no warning
394}
395
396int checkEqualFloatDeclCompare5(void) {
397 float f = 7.1F;
398 int t = 7;
399 return (t == f); // no warning
400}
401
402int checkEqualFloatDeclCompare6(void) {
403 float f = 7.1F;
404 int t = 7;
405 return (f == t); // no warning
406}
407
408
409
410
411int checkEqualCastFloatDeclCompare11(void) {
412 float f = 7.1F;
413 return ((int)f == (int)f); // expected-warning {{comparison of identical expressions always evaluates to true}}
414}
415int checkEqualCastFloatDeclCompare12(void) {
416 float f = 7.1F;
417 return ((char)f == (int)f); // no warning
418}
419int checkEqualBinaryOpFloatCompare1(void) {
420 int res;
421 float f= 3.14F;
422 res = (f + 3.14F == f + 3.14F); // no warning
423 return (0);
424}
425int checkEqualBinaryOpFloatCompare2(void) {
426 float f = 7.1F;
427 float g = 7.1F;
428 return (f + 3.14F == g + 3.14F); // no warning
429}
430int checkEqualBinaryOpFloatCompare3(void) {
431 int res;
432 float f= 3.14F;
433 res = ((int)f + 3.14F == (int)f + 3.14F); // no warning
434 return (0);
435}
436int checkEqualBinaryOpFloatCompare4(void) {
437 int res;
438 float f= 3.14F;
439 res = ((int)f + 3.14F == (char)f + 3.14F); // no warning
440 return (0);
441}
442
443int checkEqualNestedBinaryOpFloatCompare1(void) {
444 int res;
445 int t= 1;
446 int u= 2;
447 float f= 3.14F;
448 res = (((int)f + (3.14F - u)*t) == ((int)f + (3.14F - u)*t)); // no warning
449 return (0);
450}
451
452int checkEqualNestedBinaryOpFloatCompare2(void) {
453 int res;
454 int t= 1;
455 int u= 2;
456 float f= 3.14F;
457 res = (((int)f + (u - 3.14F)*t) == ((int)f + (3.14F - u)*t)); // no warning
458 return (0);
459}
460
461int checkEqualNestedBinaryOpFloatCompare3(void) {
462 int res;
463 int t= 1;
464 int u= 2;
465 float f= 3.14F;
466 res = (((int)f + (u - 3.14F)*t) == ((int)f + (3.14F - u)*(f + t == f + t))); // no warning
467 return (0);
468}
469
470
471
472
473
474/* Equal with int*/
475
476int checkEqualIntLiteralCompare1(void) {
477 return (5 == 5); // expected-warning {{comparison of identical expressions always evaluates to true}}
478}
479
480int checkEqualIntLiteralCompare2(void) {
481 return (6 == 7); // no warning
482}
483
484int checkEqualIntDeclCompare1(void) {
485 int f = 7;
486 int g = 7;
487 return (f == g); // no warning
488}
489
490int checkEqualCastIntDeclCompare11(void) {
491 int f = 7;
492 return ((int)f == (int)f); // expected-warning {{comparison of identical expressions always evaluates to true}}
493}
494int checkEqualCastIntDeclCompare12(void) {
495 int f = 7;
496 return ((char)f == (int)f); // no warning
497}
498
499int checkEqualIntDeclCompare3(void) {
500 int f = 7;
501 return (f == 7); // no warning
502}
503
504int checkEqualIntDeclCompare4(void) {
505 int f = 7;
506 return (7 == f); // no warning
507}
508
509int checkEqualBinaryOpIntCompare1(void) {
510 int res;
511 int t= 1;
512 int u= 2;
513 int f= 4;
514 res = (f + 4 == f + 4); // expected-warning {{comparison of identical expressions always evaluates to true}}
515 return (0);
516}
517int checkEqualBinaryOpIntCompare2(void) {
518 int f = 7;
519 int g = 7;
520 return (f + 4 == g + 4); // no warning
521}
522
523
524int checkEqualBinaryOpIntCompare3(void) {
525 int res;
526 int t= 1;
527 int u= 2;
528 int f= 4;
529 res = ((int)f + 4 == (int)f + 4); // expected-warning {{comparison of identical expressions always evaluates to true}}
530 return (0);
531
532}
533int checkEqualBinaryOpIntCompare4(void) {
534 int res;
535 int t= 1;
536 int u= 2;
537 int f= 4;
538 res = ((int)f + 4 == (char)f + 4); // no warning
539 return (0);
540}
541int checkEqualBinaryOpIntCompare5(void) {
542 int res;
543 int t= 1;
544 int u= 2;
545 res = (u + t == u + t); // expected-warning {{comparison of identical expressions always evaluates to true}}
546 return (0);
547}
548
549int checkEqualNestedBinaryOpIntCompare1(void) {
550 int res;
551 int t= 1;
552 int u= 2;
553 int f= 3;
554 res = (((int)f + (3 - u)*t) == ((int)f + (3 - u)*t)); // expected-warning {{comparison of identical expressions always evaluates to true}}
555 return (0);
556}
557
558int checkEqualNestedBinaryOpIntCompare2(void) {
559 int res;
560 int t= 1;
561 int u= 2;
562 int f= 3;
563 res = (((int)f + (u - 3)*t) == ((int)f + (3 - u)*t)); // no warning
564 return (0);
565}
566
567int checkEqualNestedBinaryOpIntCompare3(void) {
568 int res;
569 int t= 1;
570 int u= 2;
571 int f= 3;
572 res = (((int)f + (u - 3)*t) == ((int)f + (3 - u)*(t + 1 == t + 1))); // expected-warning {{comparison of identical expressions always evaluates to true}}
573 return (0);
574}
575
Jordan Rose60bd88d2013-12-10 18:18:06 +0000576/* '==' with function*/
577
578int checkEqualSameFunction() {
579 unsigned a = 0;
580 unsigned b = 1;
581 int res = (a+func() == a+func()); // no warning
582 return (0);
583}
584
585int checkEqualDifferentFunction() {
586 unsigned a = 0;
587 unsigned b = 1;
588 int res = (a+func() == a+func2()); // no warning
589 return (0);
590}
591
592int checkEqualSameFunctionSameParam() {
593 unsigned a = 0;
594 unsigned b = 1;
595 int res = (a+funcParam(a) == a+funcParam(a)); // no warning
596 return (0);
597}
598
599int checkEqualSameFunctionDifferentParam() {
600 unsigned a = 0;
601 unsigned b = 1;
602 int res = (a+funcParam(a) == a+funcParam(b)); // no warning
603 return (0);
604}
605
606/* end '==' with function*/
Jordan Rose51327f92013-11-08 01:15:39 +0000607
608/* end EQ int */
609
610/* end EQ */
611
612
613/* LT */
614
615/* LT with float */
616
617int checkLessThanFloatLiteralCompare1(void) {
618 return (5.14F < 5.14F); // expected-warning {{comparison of identical expressions always evaluates to false}}
619}
620
621int checkLessThanFloatLiteralCompare2(void) {
622 return (6.14F < 7.14F); // no warning
623}
624
625int checkLessThanFloatDeclCompare1(void) {
626 float f = 7.1F;
627 float g = 7.1F;
628 return (f < g); // no warning
629}
630
631int checkLessThanFloatDeclCompare12(void) {
632 float f = 7.1F;
633 return (f < f); // expected-warning {{comparison of identical expressions always evaluates to false}}
634}
635
636int checkLessThanFloatDeclCompare3(void) {
637 float f = 7.1F;
638 return (f < 7.1F); // no warning
639}
640
641int checkLessThanFloatDeclCompare4(void) {
642 float f = 7.1F;
643 return (7.1F < f); // no warning
644}
645
646int checkLessThanFloatDeclCompare5(void) {
647 float f = 7.1F;
648 int t = 7;
649 return (t < f); // no warning
650}
651
652int checkLessThanFloatDeclCompare6(void) {
653 float f = 7.1F;
654 int t = 7;
655 return (f < t); // no warning
656}
657
658
659int checkLessThanCastFloatDeclCompare11(void) {
660 float f = 7.1F;
661 return ((int)f < (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
662}
663int checkLessThanCastFloatDeclCompare12(void) {
664 float f = 7.1F;
665 return ((char)f < (int)f); // no warning
666}
667int checkLessThanBinaryOpFloatCompare1(void) {
668 int res;
669 float f= 3.14F;
670 res = (f + 3.14F < f + 3.14F); // no warning
671 return (0);
672}
673int checkLessThanBinaryOpFloatCompare2(void) {
674 float f = 7.1F;
675 float g = 7.1F;
676 return (f + 3.14F < g + 3.14F); // no warning
677}
678int checkLessThanBinaryOpFloatCompare3(void) {
679 int res;
680 float f= 3.14F;
681 res = ((int)f + 3.14F < (int)f + 3.14F); // no warning
682 return (0);
683}
684int checkLessThanBinaryOpFloatCompare4(void) {
685 int res;
686 float f= 3.14F;
687 res = ((int)f + 3.14F < (char)f + 3.14F); // no warning
688 return (0);
689}
690
691int checkLessThanNestedBinaryOpFloatCompare1(void) {
692 int res;
693 int t= 1;
694 int u= 2;
695 float f= 3.14F;
696 res = (((int)f + (3.14F - u)*t) < ((int)f + (3.14F - u)*t)); // no warning
697 return (0);
698}
699
700int checkLessThanNestedBinaryOpFloatCompare2(void) {
701 int res;
702 int t= 1;
703 int u= 2;
704 float f= 3.14F;
705 res = (((int)f + (u - 3.14F)*t) < ((int)f + (3.14F - u)*t)); // no warning
706 return (0);
707}
708
709int checkLessThanNestedBinaryOpFloatCompare3(void) {
710 int res;
711 int t= 1;
712 int u= 2;
713 float f= 3.14F;
714 res = (((int)f + (u - 3.14F)*t) < ((int)f + (3.14F - u)*(f + t < f + t))); // no warning
715 return (0);
716}
717
718/* end LT with float */
719
720/* LT with int */
721
722
723int checkLessThanIntLiteralCompare1(void) {
724 return (5 < 5); // expected-warning {{comparison of identical expressions always evaluates to false}}
725}
726
727int checkLessThanIntLiteralCompare2(void) {
728 return (6 < 7); // no warning
729}
730
731int checkLessThanIntDeclCompare1(void) {
732 int f = 7;
733 int g = 7;
734 return (f < g); // no warning
735}
736
737int checkLessThanIntDeclCompare3(void) {
738 int f = 7;
739 return (f < 7); // no warning
740}
741
742int checkLessThanIntDeclCompare4(void) {
743 int f = 7;
744 return (7 < f); // no warning
745}
746
747int checkLessThanIntDeclCompare5(void) {
748 int f = 7;
749 int t = 7;
750 return (t < f); // no warning
751}
752
753int checkLessThanIntDeclCompare6(void) {
754 int f = 7;
755 int t = 7;
756 return (f < t); // no warning
757}
758
759int checkLessThanCastIntDeclCompare11(void) {
760 int f = 7;
761 return ((int)f < (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
762}
763int checkLessThanCastIntDeclCompare12(void) {
764 int f = 7;
765 return ((char)f < (int)f); // no warning
766}
767int checkLessThanBinaryOpIntCompare1(void) {
768 int res;
769 int f= 3;
770 res = (f + 3 < f + 3); // expected-warning {{comparison of identical expressions always evaluates to false}}
771 return (0);
772}
773int checkLessThanBinaryOpIntCompare2(void) {
774 int f = 7;
775 int g = 7;
776 return (f + 3 < g + 3); // no warning
777}
778int checkLessThanBinaryOpIntCompare3(void) {
779 int res;
780 int f= 3;
781 res = ((int)f + 3 < (int)f + 3); // expected-warning {{comparison of identical expressions always evaluates to false}}
782 return (0);
783}
784int checkLessThanBinaryOpIntCompare4(void) {
785 int res;
786 int f= 3;
787 res = ((int)f + 3 < (char)f + 3); // no warning
788 return (0);
789}
790
791int checkLessThanNestedBinaryOpIntCompare1(void) {
792 int res;
793 int t= 1;
794 int u= 2;
795 int f= 3;
796 res = (((int)f + (3 - u)*t) < ((int)f + (3 - u)*t)); // expected-warning {{comparison of identical expressions always evaluates to false}}
797 return (0);
798}
799
800int checkLessThanNestedBinaryOpIntCompare2(void) {
801 int res;
802 int t= 1;
803 int u= 2;
804 int f= 3;
805 res = (((int)f + (u - 3)*t) < ((int)f + (3 - u)*t)); // no warning
806 return (0);
807}
808
809int checkLessThanNestedBinaryOpIntCompare3(void) {
810 int res;
811 int t= 1;
812 int u= 2;
813 int f= 3;
814 res = (((int)f + (u - 3)*t) < ((int)f + (3 - u)*(t + u < t + u))); // expected-warning {{comparison of identical expressions always evaluates to false}}
815 return (0);
816}
817
818/* end LT with int */
819
820/* end LT */
821
822
823/* GT */
824
825/* GT with float */
826
827int checkGreaterThanFloatLiteralCompare1(void) {
828 return (5.14F > 5.14F); // expected-warning {{comparison of identical expressions always evaluates to false}}
829}
830
831int checkGreaterThanFloatLiteralCompare2(void) {
832 return (6.14F > 7.14F); // no warning
833}
834
835int checkGreaterThanFloatDeclCompare1(void) {
836 float f = 7.1F;
837 float g = 7.1F;
838
839 return (f > g); // no warning
840}
841
842int checkGreaterThanFloatDeclCompare12(void) {
843 float f = 7.1F;
844 return (f > f); // expected-warning {{comparison of identical expressions always evaluates to false}}
845}
846
847
848int checkGreaterThanFloatDeclCompare3(void) {
849 float f = 7.1F;
850 return (f > 7.1F); // no warning
851}
852
853int checkGreaterThanFloatDeclCompare4(void) {
854 float f = 7.1F;
855 return (7.1F > f); // no warning
856}
857
858int checkGreaterThanFloatDeclCompare5(void) {
859 float f = 7.1F;
860 int t = 7;
861 return (t > f); // no warning
862}
863
864int checkGreaterThanFloatDeclCompare6(void) {
865 float f = 7.1F;
866 int t = 7;
867 return (f > t); // no warning
868}
869
870int checkGreaterThanCastFloatDeclCompare11(void) {
871 float f = 7.1F;
872 return ((int)f > (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
873}
874int checkGreaterThanCastFloatDeclCompare12(void) {
875 float f = 7.1F;
876 return ((char)f > (int)f); // no warning
877}
878int checkGreaterThanBinaryOpFloatCompare1(void) {
879 int res;
880 float f= 3.14F;
881 res = (f + 3.14F > f + 3.14F); // no warning
882 return (0);
883}
884int checkGreaterThanBinaryOpFloatCompare2(void) {
885 float f = 7.1F;
886 float g = 7.1F;
887 return (f + 3.14F > g + 3.14F); // no warning
888}
889int checkGreaterThanBinaryOpFloatCompare3(void) {
890 int res;
891 float f= 3.14F;
892 res = ((int)f + 3.14F > (int)f + 3.14F); // no warning
893 return (0);
894}
895int checkGreaterThanBinaryOpFloatCompare4(void) {
896 int res;
897 float f= 3.14F;
898 res = ((int)f + 3.14F > (char)f + 3.14F); // no warning
899 return (0);
900}
901
902int checkGreaterThanNestedBinaryOpFloatCompare1(void) {
903 int res;
904 int t= 1;
905 int u= 2;
906 float f= 3.14F;
907 res = (((int)f + (3.14F - u)*t) > ((int)f + (3.14F - u)*t)); // no warning
908 return (0);
909}
910
911int checkGreaterThanNestedBinaryOpFloatCompare2(void) {
912 int res;
913 int t= 1;
914 int u= 2;
915 float f= 3.14F;
916 res = (((int)f + (u - 3.14F)*t) > ((int)f + (3.14F - u)*t)); // no warning
917 return (0);
918}
919
920int checkGreaterThanNestedBinaryOpFloatCompare3(void) {
921 int res;
922 int t= 1;
923 int u= 2;
924 float f= 3.14F;
925 res = (((int)f + (u - 3.14F)*t) > ((int)f + (3.14F - u)*(f + t > f + t))); // no warning
926 return (0);
927}
928
929/* end GT with float */
930
931/* GT with int */
932
933
934int checkGreaterThanIntLiteralCompare1(void) {
935 return (5 > 5); // expected-warning {{comparison of identical expressions always evaluates to false}}
936}
937
938int checkGreaterThanIntLiteralCompare2(void) {
939 return (6 > 7); // no warning
940}
941
942int checkGreaterThanIntDeclCompare1(void) {
943 int f = 7;
944 int g = 7;
945
946 return (f > g); // no warning
947}
948
949int checkGreaterThanIntDeclCompare3(void) {
950 int f = 7;
951 return (f > 7); // no warning
952}
953
954int checkGreaterThanIntDeclCompare4(void) {
955 int f = 7;
956 return (7 > f); // no warning
957}
958
959int checkGreaterThanCastIntDeclCompare11(void) {
960 int f = 7;
961 return ((int)f > (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
962}
963int checkGreaterThanCastIntDeclCompare12(void) {
964 int f = 7;
965 return ((char)f > (int)f); // no warning
966}
967int checkGreaterThanBinaryOpIntCompare1(void) {
968 int res;
969 int f= 3;
970 res = (f + 3 > f + 3); // expected-warning {{comparison of identical expressions always evaluates to false}}
971 return (0);
972}
973int checkGreaterThanBinaryOpIntCompare2(void) {
974 int f = 7;
975 int g = 7;
976 return (f + 3 > g + 3); // no warning
977}
978int checkGreaterThanBinaryOpIntCompare3(void) {
979 int res;
980 int f= 3;
981 res = ((int)f + 3 > (int)f + 3); // expected-warning {{comparison of identical expressions always evaluates to false}}
982 return (0);
983}
984int checkGreaterThanBinaryOpIntCompare4(void) {
985 int res;
986 int f= 3;
987 res = ((int)f + 3 > (char)f + 3); // no warning
988 return (0);
989}
990
991int checkGreaterThanNestedBinaryOpIntCompare1(void) {
992 int res;
993 int t= 1;
994 int u= 2;
995 int f= 3;
996 res = (((int)f + (3 - u)*t) > ((int)f + (3 - u)*t)); // expected-warning {{comparison of identical expressions always evaluates to false}}
997 return (0);
998}
999
1000int checkGreaterThanNestedBinaryOpIntCompare2(void) {
1001 int res;
1002 int t= 1;
1003 int u= 2;
1004 int f= 3;
1005 res = (((int)f + (u - 3)*t) > ((int)f + (3 - u)*t)); // no warning
1006 return (0);
1007}
1008
1009int checkGreaterThanNestedBinaryOpIntCompare3(void) {
1010 int res;
1011 int t= 1;
1012 int u= 2;
1013 int f= 3;
1014 res = (((int)f + (u - 3)*t) > ((int)f + (3 - u)*(t + u > t + u))); // expected-warning {{comparison of identical expressions always evaluates to false}}
1015 return (0);
1016}
1017
1018/* end GT with int */
1019
1020/* end GT */
Jordan Rose60bd88d2013-12-10 18:18:06 +00001021
1022
1023/* Checking use of identical expressions in conditional operator*/
1024
1025unsigned test_unsigned(unsigned a) {
1026 unsigned b = 1;
1027 a = a > 5 ? b : b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1028 return a;
1029}
1030
1031void test_signed() {
1032 int a = 0;
1033 a = a > 5 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1034}
1035
1036void test_bool(bool a) {
1037 a = a > 0 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1038}
1039
1040void test_float() {
1041 float a = 0;
1042 float b = 0;
1043 a = a > 5 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1044}
1045
Jordan Rose70e7e872014-02-19 17:44:11 +00001046const char *test_string() {
1047 float a = 0;
1048 return a > 5 ? "abc" : "abc"; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1049}
1050
Jordan Rose60bd88d2013-12-10 18:18:06 +00001051void test_unsigned_expr() {
1052 unsigned a = 0;
1053 unsigned b = 0;
1054 a = a > 5 ? a+b : a+b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1055}
1056
1057void test_signed_expr() {
1058 int a = 0;
1059 int b = 1;
1060 a = a > 5 ? a+b : a+b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1061}
1062
1063void test_bool_expr(bool a) {
1064 bool b = 0;
1065 a = a > 0 ? a&&b : a&&b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1066}
1067
1068void test_unsigned_expr_negative() {
1069 unsigned a = 0;
1070 unsigned b = 0;
1071 a = a > 5 ? a+b : b+a; // no warning
1072}
1073
1074void test_signed_expr_negative() {
1075 int a = 0;
1076 int b = 1;
1077 a = a > 5 ? b+a : a+b; // no warning
1078}
1079
1080void test_bool_expr_negative(bool a) {
1081 bool b = 0;
1082 a = a > 0 ? a&&b : b&&a; // no warning
1083}
1084
1085void test_float_expr_positive() {
1086 float a = 0;
1087 float b = 0;
1088 a = a > 5 ? a+b : a+b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1089}
1090
1091void test_expr_positive_func() {
1092 unsigned a = 0;
1093 unsigned b = 1;
1094 a = a > 5 ? a+func() : a+func(); // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1095}
1096
1097void test_expr_negative_func() {
1098 unsigned a = 0;
1099 unsigned b = 1;
1100 a = a > 5 ? a+func() : a+func2(); // no warning
1101}
1102
1103void test_expr_positive_funcParam() {
1104 unsigned a = 0;
1105 unsigned b = 1;
1106 a = a > 5 ? a+funcParam(b) : a+funcParam(b); // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1107}
1108
1109void test_expr_negative_funcParam() {
1110 unsigned a = 0;
1111 unsigned b = 1;
1112 a = a > 5 ? a+funcParam(a) : a+funcParam(b); // no warning
1113}
1114
1115void test_expr_positive_inc() {
1116 unsigned a = 0;
1117 unsigned b = 1;
1118 a = a > 5 ? a++ : a++; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1119}
1120
1121void test_expr_negative_inc() {
1122 unsigned a = 0;
1123 unsigned b = 1;
1124 a = a > 5 ? a++ : b++; // no warning
1125}
1126
1127void test_expr_positive_assign() {
1128 unsigned a = 0;
1129 unsigned b = 1;
1130 a = a > 5 ? a=1 : a=1; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1131}
1132
1133void test_expr_negative_assign() {
1134 unsigned a = 0;
1135 unsigned b = 1;
1136 a = a > 5 ? a=1 : a=2; // no warning
1137}
1138
1139void test_signed_nested_expr() {
1140 int a = 0;
1141 int b = 1;
1142 int c = 3;
1143 a = a > 5 ? a+b+(c+a)*(a + b*(c+a)) : a+b+(c+a)*(a + b*(c+a)); // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1144}
1145
1146void test_signed_nested_expr_negative() {
1147 int a = 0;
1148 int b = 1;
1149 int c = 3;
1150 a = a > 5 ? a+b+(c+a)*(a + b*(c+a)) : a+b+(c+a)*(a + b*(a+c)); // no warning
1151}
1152
1153void test_signed_nested_cond_expr_negative() {
1154 int a = 0;
1155 int b = 1;
1156 int c = 3;
1157 a = a > 5 ? (b > 5 ? 1 : 4) : (b > 5 ? 2 : 4); // no warning
1158}
1159
1160void test_signed_nested_cond_expr() {
1161 int a = 0;
1162 int b = 1;
1163 int c = 3;
1164 a = a > 5 ? (b > 5 ? 1 : 4) : (b > 5 ? 4 : 4); // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1165}
Jordan Rose70e7e872014-02-19 17:44:11 +00001166
1167void test_identical_branches1(bool b) {
1168 int i = 0;
1169 if (b) { // expected-warning {{true and false branches are identical}}
1170 ++i;
1171 } else {
1172 ++i;
1173 }
1174}
1175
1176void test_identical_branches2(bool b) {
1177 int i = 0;
1178 if (b) { // expected-warning {{true and false branches are identical}}
1179 ++i;
1180 } else
1181 ++i;
1182}
1183
1184void test_identical_branches3(bool b) {
1185 int i = 0;
1186 if (b) { // no warning
1187 ++i;
1188 } else {
1189 i++;
1190 }
1191}
1192
1193void test_identical_branches4(bool b) {
1194 int i = 0;
1195 if (b) { // expected-warning {{true and false branches are identical}}
1196 } else {
1197 }
1198}
1199
1200void test_identical_branches_break(bool b) {
1201 while (true) {
1202 if (b) // expected-warning {{true and false branches are identical}}
1203 break;
1204 else
1205 break;
1206 }
1207}
1208
1209void test_identical_branches_continue(bool b) {
1210 while (true) {
1211 if (b) // expected-warning {{true and false branches are identical}}
1212 continue;
1213 else
1214 continue;
1215 }
1216}
1217
1218void test_identical_branches_func(bool b) {
1219 if (b) // expected-warning {{true and false branches are identical}}
1220 func();
1221 else
1222 func();
1223}
1224
1225void test_identical_branches_func_arguments(bool b) {
1226 if (b) // no-warning
1227 funcParam(1);
1228 else
1229 funcParam(2);
1230}
1231
1232void test_identical_branches_cast1(bool b) {
1233 long v = -7;
1234 if (b) // no-warning
1235 v = (signed int) v;
1236 else
1237 v = (unsigned int) v;
1238}
1239
1240void test_identical_branches_cast2(bool b) {
1241 long v = -7;
1242 if (b) // expected-warning {{true and false branches are identical}}
1243 v = (signed int) v;
1244 else
1245 v = (signed int) v;
1246}
1247
1248int test_identical_branches_return_int(bool b) {
1249 int i = 0;
1250 if (b) { // expected-warning {{true and false branches are identical}}
1251 i++;
1252 return i;
1253 } else {
1254 i++;
1255 return i;
1256 }
1257}
1258
1259int test_identical_branches_return_func(bool b) {
1260 if (b) { // expected-warning {{true and false branches are identical}}
1261 return func();
1262 } else {
1263 return func();
1264 }
1265}
1266
1267void test_identical_branches_for(bool b) {
1268 int i;
1269 int j;
1270 if (b) { // expected-warning {{true and false branches are identical}}
1271 for (i = 0, j = 0; i < 10; i++)
1272 j += 4;
1273 } else {
1274 for (i = 0, j = 0; i < 10; i++)
1275 j += 4;
1276 }
1277}
1278
1279void test_identical_branches_while(bool b) {
1280 int i = 10;
1281 if (b) { // expected-warning {{true and false branches are identical}}
1282 while (func())
1283 i--;
1284 } else {
1285 while (func())
1286 i--;
1287 }
1288}
1289
Jordan Rose45d71a22014-02-21 00:18:31 +00001290void test_identical_branches_while_2(bool b) {
1291 int i = 10;
1292 if (b) { // no-warning
1293 while (func())
1294 i--;
1295 } else {
1296 while (func())
1297 i++;
1298 }
1299}
1300
Jordan Rose70e7e872014-02-19 17:44:11 +00001301void test_identical_branches_do_while(bool b) {
1302 int i = 10;
1303 if (b) { // expected-warning {{true and false branches are identical}}
1304 do {
1305 i--;
1306 } while (func());
1307 } else {
1308 do {
1309 i--;
1310 } while (func());
1311 }
1312}
1313
1314void test_identical_branches_if(bool b, int i) {
1315 if (b) { // expected-warning {{true and false branches are identical}}
1316 if (i < 5)
1317 i += 10;
1318 } else {
1319 if (i < 5)
1320 i += 10;
1321 }
1322}
Jordan Rose94008122014-02-19 17:44:16 +00001323
1324void test_identical_bitwise1() {
1325 int a = 5 | 5; // expected-warning {{identical expressions on both sides of bitwise operator}}
1326}
1327
1328void test_identical_bitwise2() {
1329 int a = 5;
1330 int b = a | a; // expected-warning {{identical expressions on both sides of bitwise operator}}
1331}
1332
1333void test_identical_bitwise3() {
1334 int a = 5;
1335 int b = (a | a); // expected-warning {{identical expressions on both sides of bitwise operator}}
1336}
1337
1338void test_identical_bitwise4() {
1339 int a = 4;
1340 int b = a | 4; // no-warning
1341}
1342
1343void test_identical_bitwise5() {
1344 int a = 4;
1345 int b = 4;
1346 int c = a | b; // no-warning
1347}
1348
1349void test_identical_bitwise6() {
1350 int a = 5;
1351 int b = a | 4 | a; // expected-warning {{identical expressions on both sides of bitwise operator}}
1352}
1353
1354void test_identical_bitwise7() {
1355 int a = 5;
1356 int b = func() | func(); // no-warning
1357}
1358
1359void test_identical_logical1(int a) {
1360 if (a == 4 && a == 4) // expected-warning {{identical expressions on both sides of logical operator}}
1361 ;
1362}
1363
1364void test_identical_logical2(int a) {
1365 if (a == 4 || a == 5 || a == 4) // expected-warning {{identical expressions on both sides of logical operator}}
1366 ;
1367}
1368
1369void test_identical_logical3(int a) {
1370 if (a == 4 || a == 5 || a == 6) // no-warning
1371 ;
1372}
1373
1374void test_identical_logical4(int a) {
1375 if (a == func() || a == func()) // no-warning
1376 ;
1377}
1378
1379#pragma clang diagnostic push
1380#pragma clang diagnostic ignored "-Wlogical-op-parentheses"
1381void test_identical_logical5(int x, int y) {
1382 if (x == 4 && y == 5 || x == 4 && y == 6) // no-warning
1383 ;
1384}
1385
1386void test_identical_logical6(int x, int y) {
1387 if (x == 4 && y == 5 || x == 4 && y == 5) // expected-warning {{identical expressions on both sides of logical operator}}
1388 ;
1389}
1390
1391void test_identical_logical7(int x, int y) {
1392 // FIXME: We should warn here
1393 if (x == 4 && y == 5 || x == 4)
1394 ;
1395}
1396
1397void test_identical_logical8(int x, int y) {
1398 // FIXME: We should warn here
1399 if (x == 4 || y == 5 && x == 4)
1400 ;
1401}
1402
1403void test_identical_logical9(int x, int y) {
1404 // FIXME: We should warn here
1405 if (x == 4 || x == 4 && y == 5)
1406 ;
1407}
1408#pragma clang diagnostic pop
Jordan Rosea6839aa2014-03-11 16:52:29 +00001409
1410void test_warn_chained_if_stmts_1(int x) {
1411 if (x == 1)
1412 ;
1413 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1414 ;
1415}
1416
1417void test_warn_chained_if_stmts_2(int x) {
1418 if (x == 1)
1419 ;
1420 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1421 ;
1422 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1423 ;
1424}
1425
1426void test_warn_chained_if_stmts_3(int x) {
1427 if (x == 1)
1428 ;
1429 else if (x == 2)
1430 ;
1431 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1432 ;
1433}
1434
1435void test_warn_chained_if_stmts_4(int x) {
1436 if (x == 1)
1437 ;
1438 else if (func())
1439 ;
1440 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1441 ;
1442}
1443
1444void test_warn_chained_if_stmts_5(int x) {
1445 if (x & 1)
1446 ;
1447 else if (x & 1) // expected-warning {{expression is identical to previous condition}}
1448 ;
1449}
1450
1451void test_warn_chained_if_stmts_6(int x) {
1452 if (x == 1)
1453 ;
1454 else if (x == 2)
1455 ;
1456 else if (x == 2) // expected-warning {{expression is identical to previous condition}}
1457 ;
1458 else if (x == 3)
1459 ;
1460}
1461
1462void test_warn_chained_if_stmts_7(int x) {
1463 if (x == 1)
1464 ;
1465 else if (x == 2)
1466 ;
1467 else if (x == 3)
1468 ;
1469 else if (x == 2) // expected-warning {{expression is identical to previous condition}}
1470 ;
1471 else if (x == 5)
1472 ;
1473}
1474
1475void test_warn_chained_if_stmts_8(int x) {
1476 if (x == 1)
1477 ;
1478 else if (x == 2)
1479 ;
1480 else if (x == 3)
1481 ;
1482 else if (x == 2) // expected-warning {{expression is identical to previous condition}}
1483 ;
1484 else if (x == 5)
1485 ;
1486 else if (x == 3) // expected-warning {{expression is identical to previous condition}}
1487 ;
1488 else if (x == 7)
1489 ;
1490}
1491
1492void test_nowarn_chained_if_stmts_1(int x) {
1493 if (func())
1494 ;
1495 else if (func()) // no-warning
1496 ;
1497}
1498
1499void test_nowarn_chained_if_stmts_2(int x) {
1500 if (func())
1501 ;
1502 else if (x == 1)
1503 ;
1504 else if (func()) // no-warning
1505 ;
1506}
1507
1508void test_nowarn_chained_if_stmts_3(int x) {
1509 if (x++)
1510 ;
1511 else if (x++) // no-warning
1512 ;
1513}
Jordan Roseb6100302014-08-20 16:51:18 +00001514
1515void test_warn_wchar() {
1516 const wchar_t * a = 0 ? L"Warning" : L"Warning"; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1517}
1518void test_nowarn_wchar() {
1519 const wchar_t * a = 0 ? L"No" : L"Warning";
1520}
Jordan Rosef3544e92014-08-20 16:51:26 +00001521
1522void test_nowarn_long() {
Jordan Rose50de57d2014-08-20 22:40:57 +00001523 int a = 0, b = 0;
Jordan Rosef3544e92014-08-20 16:51:26 +00001524 long c;
1525 if (0) {
1526 b -= a;
1527 c = 0;
1528 } else { // no-warning
1529 b -= a;
Jordan Rose50de57d2014-08-20 22:40:57 +00001530 c = 0LL;
Jordan Rosef3544e92014-08-20 16:51:26 +00001531 }
1532}
Daniel Marjamaki30e2a442015-08-10 07:18:29 +00001533
1534// Identical inner conditions
1535
1536void test_warn_inner_if_1(int x) {
1537 if (x == 1) {
1538 if (x == 1) // expected-warning {{conditions of the inner and outer statements are identical}}
1539 ;
1540 }
1541
1542 // FIXME: Should warn here. The warning is currently not emitted because there
1543 // is code between the conditions.
1544 if (x == 1) {
1545 int y = x;
1546 if (x == 1)
1547 ;
1548 }
1549}
1550
1551void test_nowarn_inner_if_1(int x) {
1552 // Don't warn when condition has side effects.
1553 if (x++ == 1) {
1554 if (x++ == 1)
1555 ;
1556 }
1557
1558 // Don't warn when x is changed before inner condition.
1559 if (x < 10) {
1560 x++;
1561 if (x < 10)
1562 ;
1563 }
1564}