Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2 | |
| 3 | int foo(int x) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 4 | return x == x; // expected-warning {{self-comparison always evaluates to true}} |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 5 | } |
| 6 | |
| 7 | int foo2(int x) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 8 | return (x) != (((x))); // expected-warning {{self-comparison always evaluates to false}} |
| 9 | } |
| 10 | |
| 11 | void foo3(short s, short t) { |
| 12 | if (s == s) {} // expected-warning {{self-comparison always evaluates to true}} |
| 13 | if (s == t) {} // no-warning |
| 14 | } |
| 15 | |
| 16 | void foo4(void* v, void* w) { |
| 17 | if (v == v) {} // expected-warning {{self-comparison always evaluates to true}} |
| 18 | if (v == w) {} // no-warning |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Ted Kremenek | a8335a9 | 2007-10-29 17:02:56 +0000 | [diff] [blame] | 21 | int qux(int x) { |
| 22 | return x < x; // expected-warning {{self-comparison}} |
| 23 | } |
| 24 | |
| 25 | int qux2(int x) { |
| 26 | return x > x; // expected-warning {{self-comparison}} |
| 27 | } |
| 28 | |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 29 | int bar(float x) { |
| 30 | return x == x; // no-warning |
| 31 | } |
| 32 | |
| 33 | int bar2(float x) { |
| 34 | return x != x; // no-warning |
Ted Kremenek | a8335a9 | 2007-10-29 17:02:56 +0000 | [diff] [blame] | 35 | } |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 36 | |
| 37 | // Motivated by <rdar://problem/6703892>, self-comparisons of enum constants |
| 38 | // should not be warned about. These can be expanded from macros, and thus |
| 39 | // are usually deliberate. |
| 40 | int compare_enum() { |
| 41 | enum { A }; |
| 42 | return A == A; // no-warning |
| 43 | } |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 44 | |
| 45 | // Don't complain in unevaluated contexts. |
| 46 | int compare_sizeof(int x) { |
| 47 | return sizeof(x == x); // no-warning |
| 48 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 49 | |
| 50 | int array_comparisons() { |
| 51 | int array1[2]; |
| 52 | int array2[2]; |
| 53 | |
| 54 | // |
| 55 | // compare same array |
| 56 | // |
| 57 | return array1 == array1; // expected-warning{{self-comparison always evaluates to true}} |
| 58 | return array1 != array1; // expected-warning{{self-comparison always evaluates to false}} |
| 59 | return array1 < array1; // expected-warning{{self-comparison always evaluates to false}} |
| 60 | return array1 <= array1; // expected-warning{{self-comparison always evaluates to true}} |
| 61 | return array1 > array1; // expected-warning{{self-comparison always evaluates to false}} |
| 62 | return array1 >= array1; // expected-warning{{self-comparison always evaluates to true}} |
| 63 | |
| 64 | // |
| 65 | // compare differrent arrays |
| 66 | // |
| 67 | return array1 == array2; // expected-warning{{array comparison always evaluates to false}} |
| 68 | return array1 != array2; // expected-warning{{array comparison always evaluates to true}} |
| 69 | |
| 70 | // |
| 71 | // we don't know what these are going to be |
| 72 | // |
| 73 | return array1 < array2; // expected-warning{{array comparison always evaluates to a constant}} |
| 74 | return array1 <= array2; // expected-warning{{array comparison always evaluates to a constant}} |
| 75 | return array1 > array2; // expected-warning{{array comparison always evaluates to a constant}} |
| 76 | return array1 >= array2; // expected-warning{{array comparison always evaluates to a constant}} |
| 77 | |
| 78 | } |
| 79 | |