self-comparison.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. int foo(int x) {
  3. return x == x; // expected-warning {{self-comparison always evaluates to true}}
  4. }
  5. int foo2(int x) {
  6. return (x) != (((x))); // expected-warning {{self-comparison always evaluates to false}}
  7. }
  8. void foo3(short s, short t) {
  9. if (s == s) {} // expected-warning {{self-comparison always evaluates to true}}
  10. if (s == t) {} // no-warning
  11. }
  12. void foo4(void* v, void* w) {
  13. if (v == v) {} // expected-warning {{self-comparison always evaluates to true}}
  14. if (v == w) {} // no-warning
  15. }
  16. int qux(int x) {
  17. return x < x; // expected-warning {{self-comparison}}
  18. }
  19. int qux2(int x) {
  20. return x > x; // expected-warning {{self-comparison}}
  21. }
  22. int bar(float x) {
  23. return x == x; // no-warning
  24. }
  25. int bar2(float x) {
  26. return x != x; // no-warning
  27. }
  28. #define IS_THE_ANSWER(x) (x == 42)
  29. int macro_comparison() {
  30. return IS_THE_ANSWER(42);
  31. }
  32. // Don't complain in unevaluated contexts.
  33. int compare_sizeof(int x) {
  34. return sizeof(x == x); // no-warning
  35. }
  36. int array_comparisons() {
  37. int array1[2];
  38. int array2[2];
  39. //
  40. // compare same array
  41. //
  42. return array1 == array1; // expected-warning{{self-comparison always evaluates to true}}
  43. return array1 != array1; // expected-warning{{self-comparison always evaluates to false}}
  44. return array1 < array1; // expected-warning{{self-comparison always evaluates to false}}
  45. return array1 <= array1; // expected-warning{{self-comparison always evaluates to true}}
  46. return array1 > array1; // expected-warning{{self-comparison always evaluates to false}}
  47. return array1 >= array1; // expected-warning{{self-comparison always evaluates to true}}
  48. //
  49. // compare differrent arrays
  50. //
  51. return array1 == array2; // expected-warning{{array comparison always evaluates to false}}
  52. return array1 != array2; // expected-warning{{array comparison always evaluates to true}}
  53. //
  54. // we don't know what these are going to be
  55. //
  56. return array1 < array2; // expected-warning{{array comparison always evaluates to a constant}}
  57. return array1 <= array2; // expected-warning{{array comparison always evaluates to a constant}}
  58. return array1 > array2; // expected-warning{{array comparison always evaluates to a constant}}
  59. return array1 >= array2; // expected-warning{{array comparison always evaluates to a constant}}
  60. }
  61. // Don't issue a warning when either the left or right side of the comparison
  62. // results from a macro expansion. <rdar://problem/8435950>
  63. #define R8435950_A i
  64. #define R8435950_B i
  65. int R8435950(int i) {
  66. if (R8435950_A == R8435950_B) // no-warning
  67. return 0;
  68. return 1;
  69. }