1 // RUN: %check_clang_tidy %s misc-redundant-expression -check-suffix=IDENTEXPR %t 2 3 /* Only one expected warning per function allowed at the very end. */ 4 5 int func(void) 6 { 7 return 0; 8 } 9 10 int func2(void) 11 { 12 return 0; 13 } 14 15 int funcParam(int a) 16 { 17 return 0; 18 } 19 20 /* '!=' operator*/ 21 22 /* '!=' with float */ 23 int checkNotEqualFloatLiteralCompare1(void) { 24 return (5.14F != 5.14F); // no warning 25 } 26 27 int checkNotEqualFloatLiteralCompare2(void) { 28 return (6.14F != 7.14F); // no warning 29 } 30 31 int checkNotEqualFloatDeclCompare1(void) { 32 float f = 7.1F; 33 float g = 7.1F; 34 return (f != g); // no warning 35 } 36 37 int checkNotEqualFloatDeclCompare12(void) { 38 float f = 7.1F; 39 return (f != f); // no warning 40 } 41 42 int checkNotEqualFloatDeclCompare3(void) { 43 float f = 7.1F; 44 return (f != 7.1F); // no warning 45 } 46 47 int checkNotEqualFloatDeclCompare4(void) { 48 float f = 7.1F; 49 return (7.1F != f); // no warning 50 } 51 52 int checkNotEqualFloatDeclCompare5(void) { 53 float f = 7.1F; 54 int t = 7; 55 return (t != f); // no warning 56 } 57 58 int checkNotEqualFloatDeclCompare6(void) { 59 float f = 7.1F; 60 int t = 7; 61 return (f != t); // no warning 62 } 63 64 65 66 int checkNotEqualCastFloatDeclCompare11(void) { 67 float f = 7.1F; 68 return ((int)f != (int)f); 69 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 70 } 71 int checkNotEqualCastFloatDeclCompare12(void) { 72 float f = 7.1F; 73 return ((char)f != (int)f); // no warning 74 } 75 int checkNotEqualBinaryOpFloatCompare1(void) { 76 int res; 77 float f= 3.14F; 78 res = (f + 3.14F != f + 3.14F); // no warning 79 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:20: warning: both sides of operator are equivalent [misc-redundant-expression] 80 return (0); 81 } 82 int checkNotEqualBinaryOpFloatCompare2(void) { 83 float f = 7.1F; 84 float g = 7.1F; 85 return (f + 3.14F != g + 3.14F); // no warning 86 } 87 int checkNotEqualBinaryOpFloatCompare3(void) { 88 int res; 89 float f= 3.14F; 90 res = ((int)f + 3.14F != (int)f + 3.14F); // no warning 91 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:25: warning: both sides of operator are equivalent [misc-redundant-expression] 92 return (0); 93 } 94 int checkNotEqualBinaryOpFloatCompare4(void) { 95 int res; 96 float f= 3.14F; 97 res = ((int)f + 3.14F != (char)f + 3.14F); // no warning 98 return (0); 99 } 100 101 int checkNotEqualNestedBinaryOpFloatCompare1(void) { 102 int res; 103 int t= 1; 104 int u= 2; 105 float f= 3.14F; 106 res = (((int)f + (3.14F - u)*t) != ((int)f + (3.14F - u)*t)); // no warning 107 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:35: warning: both sides of operator are equivalent [misc-redundant-expression] 108 return (0); 109 } 110 111 int checkNotEqualNestedBinaryOpFloatCompare2(void) { 112 int res; 113 int t= 1; 114 int u= 2; 115 float f= 3.14F; 116 res = (((int)f + (u - 3.14F)*t) != ((int)f + (3.14F - u)*t)); // no warning 117 return (0); 118 } 119 120 int checkNotEqualNestedBinaryOpFloatCompare3(void) { 121 int res; 122 int t= 1; 123 int u= 2; 124 float f= 3.14F; 125 res = (((int)f + (u - 3.14F)*t) != ((int)f + (3.14F - u)*(f + t != f + t))); // no warning 126 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:67: warning: both sides of operator are equivalent [misc-redundant-expression] 127 return (0); 128 } 129 130 131 /* end '!=' with float*/ 132 133 /* '!=' with int*/ 134 135 int checkNotEqualIntLiteralCompare1(void) { 136 return (5 != 5); 137 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 138 } 139 140 int checkNotEqualIntLiteralCompare2(void) { 141 return (6 != 7); // no warning 142 } 143 144 int checkNotEqualIntDeclCompare1(void) { 145 int f = 7; 146 int g = 7; 147 return (f != g); // no warning 148 } 149 150 int checkNotEqualIntDeclCompare3(void) { 151 int f = 7; 152 return (f != 7); // no warning 153 } 154 155 int checkNotEqualIntDeclCompare4(void) { 156 int f = 7; 157 return (7 != f); // no warning 158 } 159 160 int checkNotEqualCastIntDeclCompare11(void) { 161 int f = 7; 162 return ((int)f != (int)f); 163 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 164 } 165 int checkNotEqualCastIntDeclCompare12(void) { 166 int f = 7; 167 return ((char)f != (int)f); // no warning 168 } 169 int checkNotEqualBinaryOpIntCompare1(void) { 170 int res; 171 int t= 1; 172 int u= 2; 173 int f= 4; 174 res = (f + 4 != f + 4); 175 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:16: warning: both sides of operator are equivalent [misc-redundant-expression] 176 return (0); 177 } 178 int checkNotEqualBinaryOpIntCompare2(void) { 179 int f = 7; 180 int g = 7; 181 return (f + 4 != g + 4); // no warning 182 } 183 184 185 int checkNotEqualBinaryOpIntCompare3(void) { 186 int res; 187 int t= 1; 188 int u= 2; 189 int f= 4; 190 res = ((int)f + 4 != (int)f + 4); 191 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:21: warning: both sides of operator are equivalent [misc-redundant-expression] 192 return (0); 193 } 194 int checkNotEqualBinaryOpIntCompare4(void) { 195 int res; 196 int t= 1; 197 int u= 2; 198 int f= 4; 199 res = ((int)f + 4 != (char)f + 4); // no warning 200 return (0); 201 } 202 int checkNotEqualBinaryOpIntCompare5(void) { 203 int res; 204 int t= 1; 205 int u= 2; 206 res = (u + t != u + t); 207 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:16: warning: both sides of operator are equivalent [misc-redundant-expression] 208 return (0); 209 } 210 211 int checkNotEqualNestedBinaryOpIntCompare1(void) { 212 int res; 213 int t= 1; 214 int u= 2; 215 int f= 3; 216 res = (((int)f + (3 - u)*t) != ((int)f + (3 - u)*t)); 217 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:31: warning: both sides of operator are equivalent [misc-redundant-expression] 218 return (0); 219 } 220 221 int checkNotEqualNestedBinaryOpIntCompare2(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)); // no warning 227 return (0); 228 } 229 230 int checkNotEqualNestedBinaryOpIntCompare3(void) { 231 int res; 232 int t= 1; 233 int u= 2; 234 int f= 3; 235 res = (((int)f + (u - 3)*t) != ((int)f + (3 - u)*(t + 1 != t + 1))); 236 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:59: warning: both sides of operator are equivalent [misc-redundant-expression] 237 return (0); 238 } 239 240 /* end '!=' int */ 241 242 /* '!=' with int pointer */ 243 244 int checkNotEqualIntPointerLiteralCompare1(void) { 245 int* p = 0; 246 return (p != 0); // no warning 247 } 248 249 int checkNotEqualIntPointerLiteralCompare2(void) { 250 return (6 != 7); // no warning 251 } 252 253 int checkNotEqualIntPointerDeclCompare1(void) { 254 int k = 3; 255 int* f = &k; 256 int* g = &k; 257 return (f != g); // no warning 258 } 259 260 int checkNotEqualCastIntPointerDeclCompare11(void) { 261 int k = 7; 262 int* f = &k; 263 return ((int*)f != (int*)f); 264 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:19: warning: both sides of operator are equivalent [misc-redundant-expression] 265 } 266 int checkNotEqualCastIntPointerDeclCompare12(void) { 267 int k = 7; 268 int* f = &k; 269 return ((int*)((char*)f) != (int*)f); // no warning 270 } 271 int checkNotEqualBinaryOpIntPointerCompare1(void) { 272 int k = 7; 273 int res; 274 int* f= &k; 275 res = (f + 4 != f + 4); 276 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:16: warning: both sides of operator are equivalent [misc-redundant-expression] 277 return (0); 278 } 279 int checkNotEqualBinaryOpIntPointerCompare2(void) { 280 int k = 7; 281 int* f = &k; 282 int* g = &k; 283 return (f + 4 != g + 4); // no warning 284 } 285 286 287 int checkNotEqualBinaryOpIntPointerCompare3(void) { 288 int k = 7; 289 int res; 290 int* f= &k; 291 res = ((int*)f + 4 != (int*)f + 4); 292 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:22: warning: both sides of operator are equivalent [misc-redundant-expression] 293 return (0); 294 } 295 int checkNotEqualBinaryOpIntPointerCompare4(void) { 296 int k = 7; 297 int res; 298 int* f= &k; 299 res = ((int*)f + 4 != (int*)((char*)f) + 4); // no warning 300 return (0); 301 } 302 303 int checkNotEqualNestedBinaryOpIntPointerCompare1(void) { 304 int res; 305 int k = 7; 306 int t= 1; 307 int* u= &k+2; 308 int* f= &k+3; 309 res = ((f + (3)*t) != (f + (3)*t)); 310 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:22: warning: both sides of operator are equivalent [misc-redundant-expression] 311 return (0); 312 } 313 314 int checkNotEqualNestedBinaryOpIntPointerCompare2(void) { 315 int res; 316 int k = 7; 317 int t= 1; 318 int* u= &k+2; 319 int* f= &k+3; 320 res = (((3)*t + f) != (f + (3)*t)); // no warning 321 return (0); 322 } 323 /* end '!=' int* */ 324 325 /* '!=' with function*/ 326 327 int checkNotEqualSameFunction() { 328 unsigned a = 0; 329 unsigned b = 1; 330 int res = (a+func() != a+func()); // no warning 331 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:23: warning: both sides of operator are equivalent [misc-redundant-expression] 332 return (0); 333 } 334 335 int checkNotEqualDifferentFunction() { 336 unsigned a = 0; 337 unsigned b = 1; 338 int res = (a+func() != a+func2()); // no warning 339 return (0); 340 } 341 342 int checkNotEqualSameFunctionSameParam() { 343 unsigned a = 0; 344 unsigned b = 1; 345 int res = (a+funcParam(a) != a+funcParam(a)); // no warning 346 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:29: warning: both sides of operator are equivalent [misc-redundant-expression] 347 return (0); 348 } 349 350 int checkNotEqualSameFunctionDifferentParam() { 351 unsigned a = 0; 352 unsigned b = 1; 353 int res = (a+funcParam(a) != a+funcParam(b)); // no warning 354 return (0); 355 } 356 357 /* end '!=' with function*/ 358 359 /* end '!=' */ 360 361 362 363 /* EQ operator */ 364 365 int checkEqualIntPointerDeclCompare(void) { 366 int k = 3; 367 int* f = &k; 368 int* g = &k; 369 return (f == g); // no warning 370 } 371 372 int checkEqualIntPointerDeclCompare0(void) { 373 int k = 3; 374 int* f = &k; 375 return (f+1 == f+1); 376 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:15: warning: both sides of operator are equivalent [misc-redundant-expression] 377 } 378 379 /* EQ with float*/ 380 381 int checkEqualFloatLiteralCompare1(void) { 382 return (5.14F == 5.14F); // no warning 383 } 384 385 int checkEqualFloatLiteralCompare2(void) { 386 return (6.14F == 7.14F); // no warning 387 } 388 389 int checkEqualFloatDeclCompare1(void) { 390 float f = 7.1F; 391 float g = 7.1F; 392 return (f == g); // no warning 393 } 394 395 int checkEqualFloatDeclCompare12(void) { 396 float f = 7.1F; 397 return (f == f); // no warning 398 } 399 400 401 int checkEqualFloatDeclCompare3(void) { 402 float f = 7.1F; 403 return (f == 7.1F); // no warning 404 } 405 406 int checkEqualFloatDeclCompare4(void) { 407 float f = 7.1F; 408 return (7.1F == f); // no warning 409 } 410 411 int checkEqualFloatDeclCompare5(void) { 412 float f = 7.1F; 413 int t = 7; 414 return (t == f); // no warning 415 } 416 417 int checkEqualFloatDeclCompare6(void) { 418 float f = 7.1F; 419 int t = 7; 420 return (f == t); // no warning 421 } 422 423 424 425 426 int checkEqualCastFloatDeclCompare11(void) { 427 float f = 7.1F; 428 return ((int)f == (int)f); 429 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 430 } 431 int checkEqualCastFloatDeclCompare12(void) { 432 float f = 7.1F; 433 return ((char)f == (int)f); // no warning 434 } 435 int checkEqualBinaryOpFloatCompare1(void) { 436 int res; 437 float f= 3.14F; 438 res = (f + 3.14F == f + 3.14F); 439 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:20: warning: both sides of operator are equivalent [misc-redundant-expression] 440 return (0); 441 } 442 int checkEqualBinaryOpFloatCompare2(void) { 443 float f = 7.1F; 444 float g = 7.1F; 445 return (f + 3.14F == g + 3.14F); // no warning 446 } 447 int checkEqualBinaryOpFloatCompare3(void) { 448 int res; 449 float f= 3.14F; 450 res = ((int)f + 3.14F == (int)f + 3.14F); 451 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:25: warning: both sides of operator are equivalent [misc-redundant-expression] 452 return (0); 453 } 454 int checkEqualBinaryOpFloatCompare4(void) { 455 int res; 456 float f= 3.14F; 457 res = ((int)f + 3.14F == (char)f + 3.14F); // no warning 458 return (0); 459 } 460 461 int checkEqualNestedBinaryOpFloatCompare1(void) { 462 int res; 463 int t= 1; 464 int u= 2; 465 float f= 3.14F; 466 res = (((int)f + (3.14F - u)*t) == ((int)f + (3.14F - u)*t)); 467 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:35: warning: both sides of operator are equivalent [misc-redundant-expression] 468 return (0); 469 } 470 471 int checkEqualNestedBinaryOpFloatCompare2(void) { 472 int res; 473 int t= 1; 474 int u= 2; 475 float f= 3.14F; 476 res = (((int)f + (u - 3.14F)*t) == ((int)f + (3.14F - u)*t)); // no warning 477 return (0); 478 } 479 480 int checkEqualNestedBinaryOpFloatCompare3(void) { 481 int res; 482 int t= 1; 483 int u= 2; 484 float f= 3.14F; 485 res = (((int)f + (u - 3.14F)*t) == ((int)f + (3.14F - u)*(f + t == f + t))); 486 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:67: warning: both sides of operator are equivalent [misc-redundant-expression] 487 return (0); 488 } 489 490 /* Equal with int*/ 491 492 int checkEqualIntLiteralCompare1(void) { 493 return (5 == 5); 494 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 495 } 496 497 int checkEqualIntLiteralCompare2(void) { 498 return (6 == 7); // no warning 499 } 500 501 int checkEqualIntDeclCompare1(void) { 502 int f = 7; 503 int g = 7; 504 return (f == g); // no warning 505 } 506 507 int checkEqualCastIntDeclCompare11(void) { 508 int f = 7; 509 return ((int)f == (int)f); 510 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 511 } 512 int checkEqualCastIntDeclCompare12(void) { 513 int f = 7; 514 return ((char)f == (int)f); // no warning 515 } 516 517 int checkEqualIntDeclCompare3(void) { 518 int f = 7; 519 return (f == 7); // no warning 520 } 521 522 int checkEqualIntDeclCompare4(void) { 523 int f = 7; 524 return (7 == f); // no warning 525 } 526 527 int checkEqualBinaryOpIntCompare1(void) { 528 int res; 529 int t= 1; 530 int u= 2; 531 int f= 4; 532 res = (f + 4 == f + 4); 533 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:16: warning: both sides of operator are equivalent [misc-redundant-expression] 534 return (0); 535 } 536 int checkEqualBinaryOpIntCompare2(void) { 537 int f = 7; 538 int g = 7; 539 return (f + 4 == g + 4); // no warning 540 } 541 542 543 int checkEqualBinaryOpIntCompare3(void) { 544 int res; 545 int t= 1; 546 int u= 2; 547 int f= 4; 548 res = ((int)f + 4 == (int)f + 4); 549 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:21: warning: both sides of operator are equivalent [misc-redundant-expression] 550 return (0); 551 552 } 553 int checkEqualBinaryOpIntCompare4(void) { 554 int res; 555 int t= 1; 556 int u= 2; 557 int f= 4; 558 res = ((int)f + 4 == (char)f + 4); // no warning 559 return (0); 560 } 561 int checkEqualBinaryOpIntCompare5(void) { 562 int res; 563 int t= 1; 564 int u= 2; 565 res = (u + t == u + t); 566 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:16: warning: both sides of operator are equivalent [misc-redundant-expression] 567 return (0); 568 } 569 570 int checkEqualNestedBinaryOpIntCompare1(void) { 571 int res; 572 int t= 1; 573 int u= 2; 574 int f= 3; 575 res = (((int)f + (3 - u)*t) == ((int)f + (3 - u)*t)); 576 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:31: warning: both sides of operator are equivalent [misc-redundant-expression] 577 return (0); 578 } 579 580 int checkEqualNestedBinaryOpIntCompare2(void) { 581 int res; 582 int t= 1; 583 int u= 2; 584 int f= 3; 585 res = (((int)f + (u - 3)*t) == ((int)f + (3 - u)*t)); // no warning 586 return (0); 587 } 588 589 int checkEqualNestedBinaryOpIntCompare3(void) { 590 int res; 591 int t= 1; 592 int u= 2; 593 int f= 3; 594 res = (((int)f + (u - 3)*t) == ((int)f + (3 - u)*(t + 1 == t + 1))); 595 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:59: warning: both sides of operator are equivalent [misc-redundant-expression] 596 return (0); 597 } 598 599 /* '==' with function*/ 600 601 int checkEqualSameFunction() { 602 unsigned a = 0; 603 unsigned b = 1; 604 int res = (a+func() == a+func()); 605 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:23: warning: both sides of operator are equivalent [misc-redundant-expression] 606 return (0); 607 } 608 609 int checkEqualDifferentFunction() { 610 unsigned a = 0; 611 unsigned b = 1; 612 int res = (a+func() == a+func2()); // no warning 613 return (0); 614 } 615 616 int checkEqualSameFunctionSameParam() { 617 unsigned a = 0; 618 unsigned b = 1; 619 int res = (a+funcParam(a) == a+funcParam(a)); 620 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:29: warning: both sides of operator are equivalent [misc-redundant-expression] 621 return (0); 622 } 623 624 int checkEqualSameFunctionDifferentParam() { 625 unsigned a = 0; 626 unsigned b = 1; 627 int res = (a+funcParam(a) == a+funcParam(b)); // no warning 628 return (0); 629 } 630 631 /* end '==' with function*/ 632 633 /* end EQ int */ 634 635 /* end EQ */ 636 637 638 /* LT */ 639 640 /* LT with float */ 641 642 int checkLessThanFloatLiteralCompare1(void) { 643 return (5.14F < 5.14F); 644 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:17: warning: both sides of operator are equivalent [misc-redundant-expression] 645 } 646 647 int checkLessThanFloatLiteralCompare2(void) { 648 return (6.14F < 7.14F); // no warning 649 } 650 651 int checkLessThanFloatDeclCompare1(void) { 652 float f = 7.1F; 653 float g = 7.1F; 654 return (f < g); // no warning 655 } 656 657 int checkLessThanFloatDeclCompare12(void) { 658 float f = 7.1F; 659 return (f < f); 660 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 661 } 662 663 int checkLessThanFloatDeclCompare3(void) { 664 float f = 7.1F; 665 return (f < 7.1F); // no warning 666 } 667 668 int checkLessThanFloatDeclCompare4(void) { 669 float f = 7.1F; 670 return (7.1F < f); // no warning 671 } 672 673 int checkLessThanFloatDeclCompare5(void) { 674 float f = 7.1F; 675 int t = 7; 676 return (t < f); // no warning 677 } 678 679 int checkLessThanFloatDeclCompare6(void) { 680 float f = 7.1F; 681 int t = 7; 682 return (f < t); // no warning 683 } 684 685 686 int checkLessThanCastFloatDeclCompare11(void) { 687 float f = 7.1F; 688 return ((int)f < (int)f); 689 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 690 } 691 int checkLessThanCastFloatDeclCompare12(void) { 692 float f = 7.1F; 693 return ((char)f < (int)f); // no warning 694 } 695 int checkLessThanBinaryOpFloatCompare1(void) { 696 int res; 697 float f= 3.14F; 698 res = (f + 3.14F < f + 3.14F); 699 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:20: warning: both sides of operator are equivalent [misc-redundant-expression] 700 return (0); 701 } 702 int checkLessThanBinaryOpFloatCompare2(void) { 703 float f = 7.1F; 704 float g = 7.1F; 705 return (f + 3.14F < g + 3.14F); // no warning 706 } 707 int checkLessThanBinaryOpFloatCompare3(void) { 708 int res; 709 float f= 3.14F; 710 res = ((int)f + 3.14F < (int)f + 3.14F); 711 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:25: warning: both sides of operator are equivalent [misc-redundant-expression] 712 return (0); 713 } 714 int checkLessThanBinaryOpFloatCompare4(void) { 715 int res; 716 float f= 3.14F; 717 res = ((int)f + 3.14F < (char)f + 3.14F); // no warning 718 return (0); 719 } 720 721 int checkLessThanNestedBinaryOpFloatCompare1(void) { 722 int res; 723 int t= 1; 724 int u= 2; 725 float f= 3.14F; 726 res = (((int)f + (3.14F - u)*t) < ((int)f + (3.14F - u)*t)); 727 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:35: warning: both sides of operator are equivalent [misc-redundant-expression] 728 return (0); 729 } 730 731 int checkLessThanNestedBinaryOpFloatCompare2(void) { 732 int res; 733 int t= 1; 734 int u= 2; 735 float f= 3.14F; 736 res = (((int)f + (u - 3.14F)*t) < ((int)f + (3.14F - u)*t)); // no warning 737 return (0); 738 } 739 740 int checkLessThanNestedBinaryOpFloatCompare3(void) { 741 int res; 742 int t= 1; 743 int u= 2; 744 float f= 3.14F; 745 res = (((int)f + (u - 3.14F)*t) < ((int)f + (3.14F - u)*(f + t < f + t))); 746 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:66: warning: both sides of operator are equivalent [misc-redundant-expression] 747 return (0); 748 } 749 750 /* end LT with float */ 751 752 /* LT with int */ 753 754 755 int checkLessThanIntLiteralCompare1(void) { 756 return (5 < 5); 757 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 758 } 759 760 int checkLessThanIntLiteralCompare2(void) { 761 return (6 < 7); // no warning 762 } 763 764 int checkLessThanIntDeclCompare1(void) { 765 int f = 7; 766 int g = 7; 767 return (f < g); // no warning 768 } 769 770 int checkLessThanIntDeclCompare3(void) { 771 int f = 7; 772 return (f < 7); // no warning 773 } 774 775 int checkLessThanIntDeclCompare4(void) { 776 int f = 7; 777 return (7 < f); // no warning 778 } 779 780 int checkLessThanIntDeclCompare5(void) { 781 int f = 7; 782 int t = 7; 783 return (t < f); // no warning 784 } 785 786 int checkLessThanIntDeclCompare6(void) { 787 int f = 7; 788 int t = 7; 789 return (f < t); // no warning 790 } 791 792 int checkLessThanCastIntDeclCompare11(void) { 793 int f = 7; 794 return ((int)f < (int)f); 795 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 796 } 797 int checkLessThanCastIntDeclCompare12(void) { 798 int f = 7; 799 return ((char)f < (int)f); // no warning 800 } 801 int checkLessThanBinaryOpIntCompare1(void) { 802 int res; 803 int f= 3; 804 res = (f + 3 < f + 3); 805 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:16: warning: both sides of operator are equivalent [misc-redundant-expression] 806 return (0); 807 } 808 int checkLessThanBinaryOpIntCompare2(void) { 809 int f = 7; 810 int g = 7; 811 return (f + 3 < g + 3); // no warning 812 } 813 int checkLessThanBinaryOpIntCompare3(void) { 814 int res; 815 int f= 3; 816 res = ((int)f + 3 < (int)f + 3); 817 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:21: warning: both sides of operator are equivalent [misc-redundant-expression] 818 return (0); 819 } 820 int checkLessThanBinaryOpIntCompare4(void) { 821 int res; 822 int f= 3; 823 res = ((int)f + 3 < (char)f + 3); // no warning 824 return (0); 825 } 826 827 int checkLessThanNestedBinaryOpIntCompare1(void) { 828 int res; 829 int t= 1; 830 int u= 2; 831 int f= 3; 832 res = (((int)f + (3 - u)*t) < ((int)f + (3 - u)*t)); 833 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:31: warning: both sides of operator are equivalent [misc-redundant-expression] 834 return (0); 835 } 836 837 int checkLessThanNestedBinaryOpIntCompare2(void) { 838 int res; 839 int t= 1; 840 int u= 2; 841 int f= 3; 842 res = (((int)f + (u - 3)*t) < ((int)f + (3 - u)*t)); // no warning 843 return (0); 844 } 845 846 int checkLessThanNestedBinaryOpIntCompare3(void) { 847 int res; 848 int t= 1; 849 int u= 2; 850 int f= 3; 851 res = (((int)f + (u - 3)*t) < ((int)f + (3 - u)*(t + u < t + u))); 852 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:58: warning: both sides of operator are equivalent [misc-redundant-expression] 853 return (0); 854 } 855 856 /* end LT with int */ 857 858 /* end LT */ 859 860 861 /* GT */ 862 863 /* GT with float */ 864 865 int checkGreaterThanFloatLiteralCompare1(void) { 866 return (5.14F > 5.14F); 867 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:17: warning: both sides of operator are equivalent [misc-redundant-expression] 868 } 869 870 int checkGreaterThanFloatLiteralCompare2(void) { 871 return (6.14F > 7.14F); // no warning 872 } 873 874 int checkGreaterThanFloatDeclCompare1(void) { 875 float f = 7.1F; 876 float g = 7.1F; 877 878 return (f > g); // no warning 879 } 880 881 int checkGreaterThanFloatDeclCompare12(void) { 882 float f = 7.1F; 883 return (f > f); 884 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 885 } 886 887 888 int checkGreaterThanFloatDeclCompare3(void) { 889 float f = 7.1F; 890 return (f > 7.1F); // no warning 891 } 892 893 int checkGreaterThanFloatDeclCompare4(void) { 894 float f = 7.1F; 895 return (7.1F > f); // no warning 896 } 897 898 int checkGreaterThanFloatDeclCompare5(void) { 899 float f = 7.1F; 900 int t = 7; 901 return (t > f); // no warning 902 } 903 904 int checkGreaterThanFloatDeclCompare6(void) { 905 float f = 7.1F; 906 int t = 7; 907 return (f > t); // no warning 908 } 909 910 int checkGreaterThanCastFloatDeclCompare11(void) { 911 float f = 7.1F; 912 return ((int)f > (int)f); 913 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 914 } 915 int checkGreaterThanCastFloatDeclCompare12(void) { 916 float f = 7.1F; 917 return ((char)f > (int)f); // no warning 918 } 919 int checkGreaterThanBinaryOpFloatCompare1(void) { 920 int res; 921 float f= 3.14F; 922 res = (f + 3.14F > f + 3.14F); 923 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:20: warning: both sides of operator are equivalent [misc-redundant-expression] 924 return (0); 925 } 926 int checkGreaterThanBinaryOpFloatCompare2(void) { 927 float f = 7.1F; 928 float g = 7.1F; 929 return (f + 3.14F > g + 3.14F); // no warning 930 } 931 int checkGreaterThanBinaryOpFloatCompare3(void) { 932 int res; 933 float f= 3.14F; 934 res = ((int)f + 3.14F > (int)f + 3.14F); 935 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:25: warning: both sides of operator are equivalent [misc-redundant-expression] 936 return (0); 937 } 938 int checkGreaterThanBinaryOpFloatCompare4(void) { 939 int res; 940 float f= 3.14F; 941 res = ((int)f + 3.14F > (char)f + 3.14F); // no warning 942 return (0); 943 } 944 945 int checkGreaterThanNestedBinaryOpFloatCompare1(void) { 946 int res; 947 int t= 1; 948 int u= 2; 949 float f= 3.14F; 950 res = (((int)f + (3.14F - u)*t) > ((int)f + (3.14F - u)*t)); 951 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:35: warning: both sides of operator are equivalent [misc-redundant-expression] 952 return (0); 953 } 954 955 int checkGreaterThanNestedBinaryOpFloatCompare2(void) { 956 int res; 957 int t= 1; 958 int u= 2; 959 float f= 3.14F; 960 res = (((int)f + (u - 3.14F)*t) > ((int)f + (3.14F - u)*t)); // no warning 961 return (0); 962 } 963 964 int checkGreaterThanNestedBinaryOpFloatCompare3(void) { 965 int res; 966 int t= 1; 967 int u= 2; 968 float f= 3.14F; 969 res = (((int)f + (u - 3.14F)*t) > ((int)f + (3.14F - u)*(f + t > f + t))); // no warning 970 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:66: warning: both sides of operator are equivalent [misc-redundant-expression] 971 return (0); 972 } 973 974 /* end GT with float */ 975 976 /* GT with int */ 977 978 979 int checkGreaterThanIntLiteralCompare1(void) { 980 return (5 > 5); 981 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 982 } 983 984 int checkGreaterThanIntLiteralCompare2(void) { 985 return (6 > 7); // no warning 986 } 987 988 int checkGreaterThanIntDeclCompare1(void) { 989 int f = 7; 990 int g = 7; 991 992 return (f > g); // no warning 993 } 994 995 int checkGreaterThanIntDeclCompare3(void) { 996 int f = 7; 997 return (f > 7); // no warning 998 } 999 1000 int checkGreaterThanIntDeclCompare4(void) { 1001 int f = 7; 1002 return (7 > f); // no warning 1003 } 1004 1005 int checkGreaterThanCastIntDeclCompare11(void) { 1006 int f = 7; 1007 return ((int)f > (int)f); 1008 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 1009 } 1010 int checkGreaterThanCastIntDeclCompare12(void) { 1011 int f = 7; 1012 return ((char)f > (int)f); // no warning 1013 } 1014 int checkGreaterThanBinaryOpIntCompare1(void) { 1015 int res; 1016 int f= 3; 1017 res = (f + 3 > f + 3); 1018 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:16: warning: both sides of operator are equivalent [misc-redundant-expression] 1019 return (0); 1020 } 1021 int checkGreaterThanBinaryOpIntCompare2(void) { 1022 int f = 7; 1023 int g = 7; 1024 return (f + 3 > g + 3); // no warning 1025 } 1026 int checkGreaterThanBinaryOpIntCompare3(void) { 1027 int res; 1028 int f= 3; 1029 res = ((int)f + 3 > (int)f + 3); 1030 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:21: warning: both sides of operator are equivalent [misc-redundant-expression] 1031 return (0); 1032 } 1033 int checkGreaterThanBinaryOpIntCompare4(void) { 1034 int res; 1035 int f= 3; 1036 res = ((int)f + 3 > (char)f + 3); // no warning 1037 return (0); 1038 } 1039 1040 int checkGreaterThanNestedBinaryOpIntCompare1(void) { 1041 int res; 1042 int t= 1; 1043 int u= 2; 1044 int f= 3; 1045 res = (((int)f + (3 - u)*t) > ((int)f + (3 - u)*t)); 1046 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:31: warning: both sides of operator are equivalent [misc-redundant-expression] 1047 return (0); 1048 } 1049 1050 int checkGreaterThanNestedBinaryOpIntCompare2(void) { 1051 int res; 1052 int t= 1; 1053 int u= 2; 1054 int f= 3; 1055 res = (((int)f + (u - 3)*t) > ((int)f + (3 - u)*t)); // no warning 1056 return (0); 1057 } 1058 1059 int checkGreaterThanNestedBinaryOpIntCompare3(void) { 1060 int res; 1061 int t= 1; 1062 int u= 2; 1063 int f= 3; 1064 res = (((int)f + (u - 3)*t) > ((int)f + (3 - u)*(t + u > t + u))); 1065 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:58: warning: both sides of operator are equivalent [misc-redundant-expression] 1066 return (0); 1067 } 1068 1069 /* end GT with int */ 1070 1071 /* end GT */ 1072 1073 1074 /* Checking use of identical expressions in conditional operator*/ 1075 1076 unsigned test_unsigned(unsigned a) { 1077 unsigned b = 1; 1078 a = a > 5 ? b : b; 1079 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:17: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1080 return a; 1081 } 1082 1083 void test_signed() { 1084 int a = 0; 1085 a = a > 5 ? a : a; 1086 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:17: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1087 } 1088 1089 void test_bool(bool a) { 1090 a = a > 0 ? a : a; 1091 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:17: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1092 } 1093 1094 void test_float() { 1095 float a = 0; 1096 float b = 0; 1097 a = a > 5 ? a : a; 1098 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:17: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1099 } 1100 1101 const char *test_string() { 1102 float a = 0; 1103 return a > 5 ? "abc" : "abc"; 1104 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:24: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1105 } 1106 1107 void test_unsigned_expr() { 1108 unsigned a = 0; 1109 unsigned b = 0; 1110 a = a > 5 ? a+b : a+b; 1111 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:19: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1112 } 1113 1114 void test_signed_expr() { 1115 int a = 0; 1116 int b = 1; 1117 a = a > 5 ? a+b : a+b; 1118 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:19: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1119 } 1120 1121 void test_bool_expr(bool a) { 1122 bool b = 0; 1123 a = a > 0 ? a&&b : a&&b; 1124 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:20: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1125 } 1126 1127 void test_unsigned_expr_negative() { 1128 unsigned a = 0; 1129 unsigned b = 0; 1130 a = a > 5 ? a+b : b+a; // no warning 1131 } 1132 1133 void test_signed_expr_negative() { 1134 int a = 0; 1135 int b = 1; 1136 a = a > 5 ? b+a : a+b; // no warning 1137 } 1138 1139 void test_bool_expr_negative(bool a) { 1140 bool b = 0; 1141 a = a > 0 ? a&&b : b&&a; // no warning 1142 } 1143 1144 void test_float_expr_positive() { 1145 float a = 0; 1146 float b = 0; 1147 a = a > 5 ? a+b : a+b; 1148 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:19: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1149 } 1150 1151 void test_expr_positive_func() { 1152 unsigned a = 0; 1153 unsigned b = 1; 1154 a = a > 5 ? a+func() : a+func(); 1155 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:24: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1156 } 1157 1158 void test_expr_negative_func() { 1159 unsigned a = 0; 1160 unsigned b = 1; 1161 a = a > 5 ? a+func() : a+func2(); // no warning 1162 } 1163 1164 void test_expr_positive_funcParam() { 1165 unsigned a = 0; 1166 unsigned b = 1; 1167 a = a > 5 ? a+funcParam(b) : a+funcParam(b); 1168 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:30: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1169 } 1170 1171 void test_expr_negative_funcParam() { 1172 unsigned a = 0; 1173 unsigned b = 1; 1174 a = a > 5 ? a+funcParam(a) : a+funcParam(b); // no warning 1175 } 1176 1177 void test_expr_negative_inc() { 1178 unsigned a = 0; 1179 unsigned b = 1; 1180 a = a > 5 ? a++ : b++; // no warning 1181 } 1182 1183 void test_expr_negative_assign() { 1184 unsigned a = 0; 1185 unsigned b = 1; 1186 a = a > 5 ? a=1 : a=2; // no warning 1187 } 1188 1189 void test_signed_nested_expr() { 1190 int a = 0; 1191 int b = 1; 1192 int c = 3; 1193 a = a > 5 ? a+b+(c+a)*(a + b*(c+a)) : a+b+(c+a)*(a + b*(c+a)); 1194 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:39: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1195 } 1196 1197 void test_signed_nested_expr_negative() { 1198 int a = 0; 1199 int b = 1; 1200 int c = 3; 1201 a = a > 5 ? a+b+(c+a)*(a + b*(c+a)) : a+b+(c+a)*(a + b*(a+c)); // no warning 1202 } 1203 1204 void test_signed_nested_cond_expr_negative() { 1205 int a = 0; 1206 int b = 1; 1207 int c = 3; 1208 a = a > 5 ? (b > 5 ? 1 : 4) : (b > 5 ? 2 : 4); // no warning 1209 } 1210 1211 void test_signed_nested_cond_expr() { 1212 int a = 0; 1213 int b = 1; 1214 int c = 3; 1215 a = a > 5 ? (b > 5 ? 1 : 4) : (b > 5 ? 4 : 4); 1216 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:44: warning: 'true' and 'false' expressions are equivalent [misc-redundant-expression] 1217 } 1218 1219 void test_identical_bitwise1() { 1220 int a = 5 | 5; 1221 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 1222 } 1223 1224 void test_identical_bitwise2() { 1225 int a = 5; 1226 int b = a | a; 1227 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:13: warning: both sides of operator are equivalent [misc-redundant-expression] 1228 } 1229 1230 void test_identical_bitwise3() { 1231 int a = 5; 1232 int b = (a | a); 1233 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:14: warning: both sides of operator are equivalent [misc-redundant-expression] 1234 } 1235 1236 void test_identical_bitwise4() { 1237 int a = 4; 1238 int b = a | 4; // no-warning 1239 } 1240 1241 void test_identical_bitwise5() { 1242 int a = 4; 1243 int b = 4; 1244 int c = a | b; // no-warning 1245 } 1246 1247 void test_identical_bitwise6() { 1248 int a = 5; 1249 int b = a | 4 | a; 1250 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:17: warning: operator has equivalent nested operands [misc-redundant-expression] 1251 } 1252 1253 void test_identical_bitwise7() { 1254 int a = 5; 1255 int b = func() | func(); 1256 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:18: warning: both sides of operator are equivalent [misc-redundant-expression] 1257 } 1258 1259 void test_identical_logical1(int a) { 1260 if (a == 4 && a == 4) 1261 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:14: warning: both sides of operator are equivalent [misc-redundant-expression] 1262 ; 1263 } 1264 1265 void test_identical_logical2(int a) { 1266 if (a == 4 || a == 5 || a == 4) 1267 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:24: warning: operator has equivalent nested operands [misc-redundant-expression] 1268 ; 1269 } 1270 1271 void test_identical_logical3(int a) { 1272 if (a == 4 || a == 5 || a == 6) // no-warning 1273 ; 1274 } 1275 1276 void test_identical_logical4(int a) { 1277 if (a == func() || a == func()) 1278 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:19: warning: both sides of operator are equivalent [misc-redundant-expression] 1279 ; 1280 } 1281 1282 #pragma clang diagnostic push 1283 #pragma clang diagnostic ignored "-Wlogical-op-parentheses" 1284 void test_identical_logical5(int x, int y) { 1285 if (x == 4 && y == 5 || x == 4 && y == 6) // no-warning 1286 ; 1287 } 1288 1289 void test_identical_logical6(int x, int y) { 1290 if (x == 4 && y == 5 || x == 4 && y == 5) 1291 // CHECK-MESSAGES-IDENTEXPR: :[[@LINE-1]]:24: warning: both sides of operator are equivalent [misc-redundant-expression] 1292 ; 1293 } 1294 1295 void test_identical_logical7(int x, int y) { 1296 // FIXME: We should warn here 1297 if (x == 4 && y == 5 || x == 4) 1298 ; 1299 } 1300 1301 void test_identical_logical8(int x, int y) { 1302 // FIXME: We should warn here 1303 if (x == 4 || y == 5 && x == 4) 1304 ; 1305 } 1306 1307 void test_identical_logical9(int x, int y) { 1308 // FIXME: We should warn here 1309 if (x == 4 || x == 4 && y == 5) 1310 ; 1311 } 1312 #pragma clang diagnostic pop 1313