[clang-cl] Ignore /Wv and /Wv:17 flags
[llvm-project.git] / clang / test / Analysis / identical-expressions.cpp
blob8bb82372b534adef2fce576cc6ae3fa32c86e80e
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.IdenticalExpr -w -verify %s
3 /* Only one expected warning per function allowed at the very end. */
5 int func(void)
7 return 0;
10 int func2(void)
12 return 0;
15 int funcParam(int a)
17 return 0;
20 /* '!=' operator*/
22 /* '!=' with float */
23 int checkNotEqualFloatLiteralCompare1(void) {
24 return (5.14F != 5.14F); // no warning
27 int checkNotEqualFloatLiteralCompare2(void) {
28 return (6.14F != 7.14F); // no warning
31 int checkNotEqualFloatDeclCompare1(void) {
32 float f = 7.1F;
33 float g = 7.1F;
34 return (f != g); // no warning
37 int checkNotEqualFloatDeclCompare12(void) {
38 float f = 7.1F;
39 return (f != f); // no warning
42 int checkNotEqualFloatDeclCompare3(void) {
43 float f = 7.1F;
44 return (f != 7.1F); // no warning
47 int checkNotEqualFloatDeclCompare4(void) {
48 float f = 7.1F;
49 return (7.1F != f); // no warning
52 int checkNotEqualFloatDeclCompare5(void) {
53 float f = 7.1F;
54 int t = 7;
55 return (t != f); // no warning
58 int checkNotEqualFloatDeclCompare6(void) {
59 float f = 7.1F;
60 int t = 7;
61 return (f != t); // no warning
66 int checkNotEqualCastFloatDeclCompare11(void) {
67 float f = 7.1F;
68 return ((int)f != (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
70 int checkNotEqualCastFloatDeclCompare12(void) {
71 float f = 7.1F;
72 return ((char)f != (int)f); // no warning
74 int checkNotEqualBinaryOpFloatCompare1(void) {
75 int res;
76 float f= 3.14F;
77 res = (f + 3.14F != f + 3.14F); // no warning
78 return (0);
80 int checkNotEqualBinaryOpFloatCompare2(void) {
81 float f = 7.1F;
82 float g = 7.1F;
83 return (f + 3.14F != g + 3.14F); // no warning
85 int 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);
91 int 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);
98 int 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);
107 int 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);
116 int 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);
128 /* end '!=' with float*/
130 /* '!=' with int*/
132 int checkNotEqualIntLiteralCompare1(void) {
133 return (5 != 5); // expected-warning {{comparison of identical expressions always evaluates to false}}
136 int checkNotEqualIntLiteralCompare2(void) {
137 return (6 != 7); // no warning
140 int checkNotEqualIntDeclCompare1(void) {
141 int f = 7;
142 int g = 7;
143 return (f != g); // no warning
146 int checkNotEqualIntDeclCompare3(void) {
147 int f = 7;
148 return (f != 7); // no warning
151 int checkNotEqualIntDeclCompare4(void) {
152 int f = 7;
153 return (7 != f); // no warning
156 int checkNotEqualCastIntDeclCompare11(void) {
157 int f = 7;
158 return ((int)f != (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
160 int checkNotEqualCastIntDeclCompare12(void) {
161 int f = 7;
162 return ((char)f != (int)f); // no warning
164 int 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);
172 int checkNotEqualBinaryOpIntCompare2(void) {
173 int f = 7;
174 int g = 7;
175 return (f + 4 != g + 4); // no warning
179 int 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);
187 int 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);
195 int 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);
203 int 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);
212 int 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);
221 int 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);
230 /* end '!=' int */
234 /* '!=' with int pointer */
236 int checkNotEqualIntPointerLiteralCompare1(void) {
237 int* p = 0;
238 return (p != 0); // no warning
241 int checkNotEqualIntPointerLiteralCompare2(void) {
242 return (6 != 7); // no warning
245 int checkNotEqualIntPointerDeclCompare1(void) {
246 int k = 3;
247 int* f = &k;
248 int* g = &k;
249 return (f != g); // no warning
252 int 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}}
257 int checkNotEqualCastIntPointerDeclCompare12(void) {
258 int k = 7;
259 int* f = &k;
260 return ((int*)((char*)f) != (int*)f); // no warning
262 int 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);
269 int checkNotEqualBinaryOpIntPointerCompare2(void) {
270 int k = 7;
271 int* f = &k;
272 int* g = &k;
273 return (f + 4 != g + 4); // no warning
277 int 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);
284 int 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);
292 int 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);
302 int 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);
311 /* end '!=' int* */
313 /* '!=' with function*/
315 int checkNotEqualSameFunction() {
316 unsigned a = 0;
317 unsigned b = 1;
318 int res = (a+func() != a+func()); // no warning
319 return (0);
322 int checkNotEqualDifferentFunction() {
323 unsigned a = 0;
324 unsigned b = 1;
325 int res = (a+func() != a+func2()); // no warning
326 return (0);
329 int checkNotEqualSameFunctionSameParam() {
330 unsigned a = 0;
331 unsigned b = 1;
332 int res = (a+funcParam(a) != a+funcParam(a)); // no warning
333 return (0);
336 int checkNotEqualSameFunctionDifferentParam() {
337 unsigned a = 0;
338 unsigned b = 1;
339 int res = (a+funcParam(a) != a+funcParam(b)); // no warning
340 return (0);
343 /* end '!=' with function*/
345 /* end '!=' */
349 /* EQ operator */
351 int checkEqualIntPointerDeclCompare(void) {
352 int k = 3;
353 int* f = &k;
354 int* g = &k;
355 return (f == g); // no warning
358 int 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}}
364 /* EQ with float*/
366 int checkEqualFloatLiteralCompare1(void) {
367 return (5.14F == 5.14F); // no warning
370 int checkEqualFloatLiteralCompare2(void) {
371 return (6.14F == 7.14F); // no warning
374 int checkEqualFloatDeclCompare1(void) {
375 float f = 7.1F;
376 float g = 7.1F;
377 return (f == g); // no warning
380 int checkEqualFloatDeclCompare12(void) {
381 float f = 7.1F;
382 return (f == f); // no warning
386 int checkEqualFloatDeclCompare3(void) {
387 float f = 7.1F;
388 return (f == 7.1F); // no warning
391 int checkEqualFloatDeclCompare4(void) {
392 float f = 7.1F;
393 return (7.1F == f); // no warning
396 int checkEqualFloatDeclCompare5(void) {
397 float f = 7.1F;
398 int t = 7;
399 return (t == f); // no warning
402 int checkEqualFloatDeclCompare6(void) {
403 float f = 7.1F;
404 int t = 7;
405 return (f == t); // no warning
411 int checkEqualCastFloatDeclCompare11(void) {
412 float f = 7.1F;
413 return ((int)f == (int)f); // expected-warning {{comparison of identical expressions always evaluates to true}}
415 int checkEqualCastFloatDeclCompare12(void) {
416 float f = 7.1F;
417 return ((char)f == (int)f); // no warning
419 int checkEqualBinaryOpFloatCompare1(void) {
420 int res;
421 float f= 3.14F;
422 res = (f + 3.14F == f + 3.14F); // no warning
423 return (0);
425 int checkEqualBinaryOpFloatCompare2(void) {
426 float f = 7.1F;
427 float g = 7.1F;
428 return (f + 3.14F == g + 3.14F); // no warning
430 int 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);
436 int 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);
443 int 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);
452 int 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);
461 int 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);
474 /* Equal with int*/
476 int checkEqualIntLiteralCompare1(void) {
477 return (5 == 5); // expected-warning {{comparison of identical expressions always evaluates to true}}
480 int checkEqualIntLiteralCompare2(void) {
481 return (6 == 7); // no warning
484 int checkEqualIntDeclCompare1(void) {
485 int f = 7;
486 int g = 7;
487 return (f == g); // no warning
490 int checkEqualCastIntDeclCompare11(void) {
491 int f = 7;
492 return ((int)f == (int)f); // expected-warning {{comparison of identical expressions always evaluates to true}}
494 int checkEqualCastIntDeclCompare12(void) {
495 int f = 7;
496 return ((char)f == (int)f); // no warning
499 int checkEqualIntDeclCompare3(void) {
500 int f = 7;
501 return (f == 7); // no warning
504 int checkEqualIntDeclCompare4(void) {
505 int f = 7;
506 return (7 == f); // no warning
509 int 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);
517 int checkEqualBinaryOpIntCompare2(void) {
518 int f = 7;
519 int g = 7;
520 return (f + 4 == g + 4); // no warning
524 int 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);
533 int 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);
541 int 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);
549 int 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);
558 int 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);
567 int 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);
576 /* '==' with function*/
578 int checkEqualSameFunction() {
579 unsigned a = 0;
580 unsigned b = 1;
581 int res = (a+func() == a+func()); // no warning
582 return (0);
585 int checkEqualDifferentFunction() {
586 unsigned a = 0;
587 unsigned b = 1;
588 int res = (a+func() == a+func2()); // no warning
589 return (0);
592 int checkEqualSameFunctionSameParam() {
593 unsigned a = 0;
594 unsigned b = 1;
595 int res = (a+funcParam(a) == a+funcParam(a)); // no warning
596 return (0);
599 int checkEqualSameFunctionDifferentParam() {
600 unsigned a = 0;
601 unsigned b = 1;
602 int res = (a+funcParam(a) == a+funcParam(b)); // no warning
603 return (0);
606 /* end '==' with function*/
608 /* end EQ int */
610 /* end EQ */
613 /* LT */
615 /* LT with float */
617 int checkLessThanFloatLiteralCompare1(void) {
618 return (5.14F < 5.14F); // expected-warning {{comparison of identical expressions always evaluates to false}}
621 int checkLessThanFloatLiteralCompare2(void) {
622 return (6.14F < 7.14F); // no warning
625 int checkLessThanFloatDeclCompare1(void) {
626 float f = 7.1F;
627 float g = 7.1F;
628 return (f < g); // no warning
631 int checkLessThanFloatDeclCompare12(void) {
632 float f = 7.1F;
633 return (f < f); // expected-warning {{comparison of identical expressions always evaluates to false}}
636 int checkLessThanFloatDeclCompare3(void) {
637 float f = 7.1F;
638 return (f < 7.1F); // no warning
641 int checkLessThanFloatDeclCompare4(void) {
642 float f = 7.1F;
643 return (7.1F < f); // no warning
646 int checkLessThanFloatDeclCompare5(void) {
647 float f = 7.1F;
648 int t = 7;
649 return (t < f); // no warning
652 int checkLessThanFloatDeclCompare6(void) {
653 float f = 7.1F;
654 int t = 7;
655 return (f < t); // no warning
659 int checkLessThanCastFloatDeclCompare11(void) {
660 float f = 7.1F;
661 return ((int)f < (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
663 int checkLessThanCastFloatDeclCompare12(void) {
664 float f = 7.1F;
665 return ((char)f < (int)f); // no warning
667 int checkLessThanBinaryOpFloatCompare1(void) {
668 int res;
669 float f= 3.14F;
670 res = (f + 3.14F < f + 3.14F); // no warning
671 return (0);
673 int checkLessThanBinaryOpFloatCompare2(void) {
674 float f = 7.1F;
675 float g = 7.1F;
676 return (f + 3.14F < g + 3.14F); // no warning
678 int 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);
684 int 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);
691 int 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);
700 int 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);
709 int 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);
718 /* end LT with float */
720 /* LT with int */
723 int checkLessThanIntLiteralCompare1(void) {
724 return (5 < 5); // expected-warning {{comparison of identical expressions always evaluates to false}}
727 int checkLessThanIntLiteralCompare2(void) {
728 return (6 < 7); // no warning
731 int checkLessThanIntDeclCompare1(void) {
732 int f = 7;
733 int g = 7;
734 return (f < g); // no warning
737 int checkLessThanIntDeclCompare3(void) {
738 int f = 7;
739 return (f < 7); // no warning
742 int checkLessThanIntDeclCompare4(void) {
743 int f = 7;
744 return (7 < f); // no warning
747 int checkLessThanIntDeclCompare5(void) {
748 int f = 7;
749 int t = 7;
750 return (t < f); // no warning
753 int checkLessThanIntDeclCompare6(void) {
754 int f = 7;
755 int t = 7;
756 return (f < t); // no warning
759 int checkLessThanCastIntDeclCompare11(void) {
760 int f = 7;
761 return ((int)f < (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
763 int checkLessThanCastIntDeclCompare12(void) {
764 int f = 7;
765 return ((char)f < (int)f); // no warning
767 int 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);
773 int checkLessThanBinaryOpIntCompare2(void) {
774 int f = 7;
775 int g = 7;
776 return (f + 3 < g + 3); // no warning
778 int 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);
784 int checkLessThanBinaryOpIntCompare4(void) {
785 int res;
786 int f= 3;
787 res = ((int)f + 3 < (char)f + 3); // no warning
788 return (0);
791 int 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);
800 int 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);
809 int 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);
818 /* end LT with int */
820 /* end LT */
823 /* GT */
825 /* GT with float */
827 int checkGreaterThanFloatLiteralCompare1(void) {
828 return (5.14F > 5.14F); // expected-warning {{comparison of identical expressions always evaluates to false}}
831 int checkGreaterThanFloatLiteralCompare2(void) {
832 return (6.14F > 7.14F); // no warning
835 int checkGreaterThanFloatDeclCompare1(void) {
836 float f = 7.1F;
837 float g = 7.1F;
839 return (f > g); // no warning
842 int checkGreaterThanFloatDeclCompare12(void) {
843 float f = 7.1F;
844 return (f > f); // expected-warning {{comparison of identical expressions always evaluates to false}}
848 int checkGreaterThanFloatDeclCompare3(void) {
849 float f = 7.1F;
850 return (f > 7.1F); // no warning
853 int checkGreaterThanFloatDeclCompare4(void) {
854 float f = 7.1F;
855 return (7.1F > f); // no warning
858 int checkGreaterThanFloatDeclCompare5(void) {
859 float f = 7.1F;
860 int t = 7;
861 return (t > f); // no warning
864 int checkGreaterThanFloatDeclCompare6(void) {
865 float f = 7.1F;
866 int t = 7;
867 return (f > t); // no warning
870 int checkGreaterThanCastFloatDeclCompare11(void) {
871 float f = 7.1F;
872 return ((int)f > (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
874 int checkGreaterThanCastFloatDeclCompare12(void) {
875 float f = 7.1F;
876 return ((char)f > (int)f); // no warning
878 int checkGreaterThanBinaryOpFloatCompare1(void) {
879 int res;
880 float f= 3.14F;
881 res = (f + 3.14F > f + 3.14F); // no warning
882 return (0);
884 int checkGreaterThanBinaryOpFloatCompare2(void) {
885 float f = 7.1F;
886 float g = 7.1F;
887 return (f + 3.14F > g + 3.14F); // no warning
889 int 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);
895 int 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);
902 int 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);
911 int 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);
920 int 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);
929 /* end GT with float */
931 /* GT with int */
934 int checkGreaterThanIntLiteralCompare1(void) {
935 return (5 > 5); // expected-warning {{comparison of identical expressions always evaluates to false}}
938 int checkGreaterThanIntLiteralCompare2(void) {
939 return (6 > 7); // no warning
942 int checkGreaterThanIntDeclCompare1(void) {
943 int f = 7;
944 int g = 7;
946 return (f > g); // no warning
949 int checkGreaterThanIntDeclCompare3(void) {
950 int f = 7;
951 return (f > 7); // no warning
954 int checkGreaterThanIntDeclCompare4(void) {
955 int f = 7;
956 return (7 > f); // no warning
959 int checkGreaterThanCastIntDeclCompare11(void) {
960 int f = 7;
961 return ((int)f > (int)f); // expected-warning {{comparison of identical expressions always evaluates to false}}
963 int checkGreaterThanCastIntDeclCompare12(void) {
964 int f = 7;
965 return ((char)f > (int)f); // no warning
967 int 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);
973 int checkGreaterThanBinaryOpIntCompare2(void) {
974 int f = 7;
975 int g = 7;
976 return (f + 3 > g + 3); // no warning
978 int 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);
984 int checkGreaterThanBinaryOpIntCompare4(void) {
985 int res;
986 int f= 3;
987 res = ((int)f + 3 > (char)f + 3); // no warning
988 return (0);
991 int 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);
1000 int 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);
1009 int 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);
1018 /* end GT with int */
1020 /* end GT */
1023 /* Checking use of identical expressions in conditional operator*/
1025 unsigned 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;
1031 void test_signed() {
1032 int a = 0;
1033 a = a > 5 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1036 void test_bool(bool a) {
1037 a = a > 0 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1040 void 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}}
1046 const char *test_string() {
1047 float a = 0;
1048 return a > 5 ? "abc" : "abc"; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1051 void 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}}
1057 void 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}}
1063 void 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}}
1068 void test_unsigned_expr_negative() {
1069 unsigned a = 0;
1070 unsigned b = 0;
1071 a = a > 5 ? a+b : b+a; // no warning
1074 void test_signed_expr_negative() {
1075 int a = 0;
1076 int b = 1;
1077 a = a > 5 ? b+a : a+b; // no warning
1080 void test_bool_expr_negative(bool a) {
1081 bool b = 0;
1082 a = a > 0 ? a&&b : b&&a; // no warning
1085 void 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}}
1091 void 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}}
1097 void test_expr_negative_func() {
1098 unsigned a = 0;
1099 unsigned b = 1;
1100 a = a > 5 ? a+func() : a+func2(); // no warning
1103 void 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}}
1109 void test_expr_negative_funcParam() {
1110 unsigned a = 0;
1111 unsigned b = 1;
1112 a = a > 5 ? a+funcParam(a) : a+funcParam(b); // no warning
1115 void 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}}
1121 void test_expr_negative_inc() {
1122 unsigned a = 0;
1123 unsigned b = 1;
1124 a = a > 5 ? a++ : b++; // no warning
1127 void 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}}
1133 void test_expr_negative_assign() {
1134 unsigned a = 0;
1135 unsigned b = 1;
1136 a = a > 5 ? a=1 : a=2; // no warning
1139 void 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}}
1146 void 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
1153 void 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
1160 void 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}}
1167 void 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;
1176 void 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;
1184 void test_identical_branches3(bool b) {
1185 int i = 0;
1186 if (b) { // no warning
1187 ++i;
1188 } else {
1189 i++;
1193 void test_identical_branches4(bool b) {
1194 int i = 0;
1195 if (b) { // expected-warning {{true and false branches are identical}}
1196 } else {
1200 void 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;
1209 void 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;
1218 void test_identical_branches_func(bool b) {
1219 if (b) // expected-warning {{true and false branches are identical}}
1220 func();
1221 else
1222 func();
1225 void test_identical_branches_func_arguments(bool b) {
1226 if (b) // no-warning
1227 funcParam(1);
1228 else
1229 funcParam(2);
1232 void 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;
1240 void 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;
1248 int 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;
1259 int 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();
1267 void 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;
1279 void 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--;
1290 void 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++;
1301 void 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());
1314 void 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;
1324 void test_identical_bitwise1() {
1325 int a = 5 | 5; // expected-warning {{identical expressions on both sides of bitwise operator}}
1328 void test_identical_bitwise2() {
1329 int a = 5;
1330 int b = a | a; // expected-warning {{identical expressions on both sides of bitwise operator}}
1333 void test_identical_bitwise3() {
1334 int a = 5;
1335 int b = (a | a); // expected-warning {{identical expressions on both sides of bitwise operator}}
1338 void test_identical_bitwise4() {
1339 int a = 4;
1340 int b = a | 4; // no-warning
1343 void test_identical_bitwise5() {
1344 int a = 4;
1345 int b = 4;
1346 int c = a | b; // no-warning
1349 void test_identical_bitwise6() {
1350 int a = 5;
1351 int b = a | 4 | a; // expected-warning {{identical expressions on both sides of bitwise operator}}
1354 void test_identical_bitwise7() {
1355 int a = 5;
1356 int b = func() | func(); // no-warning
1359 void test_identical_logical1(int a) {
1360 if (a == 4 && a == 4) // expected-warning {{identical expressions on both sides of logical operator}}
1364 void test_identical_logical2(int a) {
1365 if (a == 4 || a == 5 || a == 4) // expected-warning {{identical expressions on both sides of logical operator}}
1369 void test_identical_logical3(int a) {
1370 if (a == 4 || a == 5 || a == 6) // no-warning
1374 void test_identical_logical4(int a) {
1375 if (a == func() || a == func()) // no-warning
1379 #pragma clang diagnostic push
1380 #pragma clang diagnostic ignored "-Wlogical-op-parentheses"
1381 void test_identical_logical5(int x, int y) {
1382 if (x == 4 && y == 5 || x == 4 && y == 6) // no-warning
1386 void 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}}
1391 void test_identical_logical7(int x, int y) {
1392 // FIXME: We should warn here
1393 if (x == 4 && y == 5 || x == 4)
1397 void test_identical_logical8(int x, int y) {
1398 // FIXME: We should warn here
1399 if (x == 4 || y == 5 && x == 4)
1403 void test_identical_logical9(int x, int y) {
1404 // FIXME: We should warn here
1405 if (x == 4 || x == 4 && y == 5)
1408 #pragma clang diagnostic pop
1410 void test_warn_chained_if_stmts_1(int x) {
1411 if (x == 1)
1413 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1417 void test_warn_chained_if_stmts_2(int x) {
1418 if (x == 1)
1420 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1422 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1426 void test_warn_chained_if_stmts_3(int x) {
1427 if (x == 1)
1429 else if (x == 2)
1431 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1435 void test_warn_chained_if_stmts_4(int x) {
1436 if (x == 1)
1438 else if (func())
1440 else if (x == 1) // expected-warning {{expression is identical to previous condition}}
1444 void test_warn_chained_if_stmts_5(int x) {
1445 if (x & 1)
1447 else if (x & 1) // expected-warning {{expression is identical to previous condition}}
1451 void test_warn_chained_if_stmts_6(int x) {
1452 if (x == 1)
1454 else if (x == 2)
1456 else if (x == 2) // expected-warning {{expression is identical to previous condition}}
1458 else if (x == 3)
1462 void test_warn_chained_if_stmts_7(int x) {
1463 if (x == 1)
1465 else if (x == 2)
1467 else if (x == 3)
1469 else if (x == 2) // expected-warning {{expression is identical to previous condition}}
1471 else if (x == 5)
1475 void test_warn_chained_if_stmts_8(int x) {
1476 if (x == 1)
1478 else if (x == 2)
1480 else if (x == 3)
1482 else if (x == 2) // expected-warning {{expression is identical to previous condition}}
1484 else if (x == 5)
1486 else if (x == 3) // expected-warning {{expression is identical to previous condition}}
1488 else if (x == 7)
1492 void test_nowarn_chained_if_stmts_1(int x) {
1493 if (func())
1495 else if (func()) // no-warning
1499 void test_nowarn_chained_if_stmts_2(int x) {
1500 if (func())
1502 else if (x == 1)
1504 else if (func()) // no-warning
1508 void test_nowarn_chained_if_stmts_3(int x) {
1509 if (x++)
1511 else if (x++) // no-warning
1515 void test_warn_wchar() {
1516 const wchar_t * a = 0 ? L"Warning" : L"Warning"; // expected-warning {{identical expressions on both sides of ':' in conditional expression}}
1518 void test_nowarn_wchar() {
1519 const wchar_t * a = 0 ? L"No" : L"Warning";
1522 void test_nowarn_long() {
1523 int a = 0, b = 0;
1524 long c;
1525 if (0) {
1526 b -= a;
1527 c = 0;
1528 } else { // no-warning
1529 b -= a;
1530 c = 0LL;
1534 // Identical inner conditions
1536 void 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}}
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)
1551 void test_nowarn_inner_if_1(int x) {
1552 // Don't warn when condition has side effects.
1553 if (x++ == 1) {
1554 if (x++ == 1)
1558 // Don't warn when x is changed before inner condition.
1559 if (x < 10) {
1560 x++;
1561 if (x < 10)