warn-overlap.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
  2. #define mydefine 2
  3. void f(int x) {
  4. int y = 0;
  5. // > || <
  6. if (x > 2 || x < 1) { }
  7. if (x > 2 || x < 2) { }
  8. if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  9. if (x > 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  10. if (x > 0 || x < 0) { }
  11. if (x > 2 || x <= 1) { }
  12. if (x > 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  13. if (x > 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  14. if (x >= 2 || x < 1) { }
  15. if (x >= 2 || x < 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  16. if (x >= 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  17. if (x >= 2 || x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  18. if (x >= 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  19. if (x >= 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  20. if (x >= 0 || x <= 0) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  21. // > && <
  22. if (x > 2 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  23. if (x > 2 && x < 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  24. if (x > 2 && x < 3) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  25. if (x > 0 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  26. if (x > 2 && x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  27. if (x > 2 && x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  28. if (x > 2 && x <= 3) { }
  29. if (x >= 2 && x < 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  30. if (x >= 2 && x < 2) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  31. if (x >= 2 && x < 3) { }
  32. if (x >= 0 && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  33. if (x >= 2 && x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  34. if (x >= 2 && x <= 2) { }
  35. if (x >= 2 && x <= 3) { }
  36. // !=, ==, ..
  37. if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  38. if (x != 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
  39. if (x == 2 && x == 3) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  40. if (x == 2 && x > 3) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  41. if (x == 3 && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  42. if (3 == x && x < 0) { } // expected-warning {{overlapping comparisons always evaluate to false}}
  43. if (x == mydefine && x > 3) { }
  44. if (x == (mydefine + 1) && x > 3) { }
  45. }
  46. // Don't generate a warning here.
  47. void array_out_of_bounds() {
  48. int x;
  49. int buffer[4];
  50. x = (-7 > 0) ? (buffer[-7]) : 0;
  51. }