| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++2a |
| 2 |
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++2a -fexperimental-new-constant-interpreter |
| 3 |
|
| 4 |
int foo(int x) { |
| 5 |
return x == x; // expected-warning {{self-comparison always evaluates to true}} |
| 6 |
} |
| 7 |
|
| 8 |
struct X { |
| 9 |
bool operator==(const X &x) const; |
| 10 |
}; |
| 11 |
|
| 12 |
struct A { |
| 13 |
int x; |
| 14 |
X x2; |
| 15 |
int a[3]; |
| 16 |
int b[3]; |
| 17 |
bool f() { return x == x; } // expected-warning {{self-comparison always evaluates to true}} |
| 18 |
bool g() { return x2 == x2; } // no-warning |
| 19 |
bool h() { return a == b; } // expected-warning {{array comparison always evaluates to false}} expected-warning {{deprecated}} |
| 20 |
bool i() { |
| 21 |
int c[3]; |
| 22 |
return a == c; // expected-warning {{array comparison always evaluates to false}} expected-warning {{deprecated}} |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
namespace NA { extern "C" int x[3]; } |
| 27 |
namespace NB { extern "C" int x[3]; } |
| 28 |
bool k = NA::x == NB::x; // expected-warning {{self-comparison always evaluates to true}} expected-warning {{deprecated}} |
| 29 |
|
| 30 |
template<typename T> struct Y { static inline int n; }; |
| 31 |
bool f() { |
| 32 |
return |
| 33 |
Y<int>::n == Y<int>::n || // expected-warning {{self-comparison always evaluates to true}} |
| 34 |
Y<void>::n == Y<int>::n; |
| 35 |
} |
| 36 |
template<typename T, typename U> |
| 37 |
bool g() { |
| 38 |
// FIXME: Ideally we'd produce a self-comparison warning on the first of these. |
| 39 |
return |
| 40 |
Y<T>::n == Y<T>::n || |
| 41 |
Y<T>::n == Y<U>::n; |
| 42 |
} |
| 43 |
template bool g<int, int>(); // should not produce any warnings |
| 44 |
|
| 45 |
namespace member_tests { |
| 46 |
struct B { |
| 47 |
int field; |
| 48 |
static int static_field; |
| 49 |
int test(B b) { |
| 50 |
return field == field; // expected-warning {{self-comparison always evaluates to true}} |
| 51 |
return static_field == static_field; // expected-warning {{self-comparison always evaluates to true}} |
| 52 |
return static_field == b.static_field; // expected-warning {{self-comparison always evaluates to true}} |
| 53 |
return B::static_field == this->static_field; // expected-warning {{self-comparison always evaluates to true}} |
| 54 |
return this == this; // expected-warning {{self-comparison always evaluates to true}} |
| 55 |
|
| 56 |
return field == b.field; |
| 57 |
return this->field == b.field; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
enum { |
| 62 |
I0, |
| 63 |
I1, |
| 64 |
I2, |
| 65 |
}; |
| 66 |
|
| 67 |
struct S { |
| 68 |
int field; |
| 69 |
static int static_field; |
| 70 |
int array[4]; |
| 71 |
}; |
| 72 |
|
| 73 |
struct T { |
| 74 |
int field; |
| 75 |
static int static_field; |
| 76 |
int array[4]; |
| 77 |
S s; |
| 78 |
}; |
| 79 |
|
| 80 |
int struct_test(S s1, S s2, S *s3, T t) { |
| 81 |
return s1.field == s1.field; // expected-warning {{self-comparison always evaluates to true}} |
| 82 |
return s2.field == s2.field; // expected-warning {{self-comparison always evaluates to true}} |
| 83 |
return s1.static_field == s2.static_field; // expected-warning {{self-comparison always evaluates to true}} |
| 84 |
return S::static_field == s1.static_field; // expected-warning {{self-comparison always evaluates to true}} |
| 85 |
return s1.array == s1.array; // expected-warning {{self-comparison always evaluates to true}} expected-warning {{deprecated}} |
| 86 |
return t.s.static_field == S::static_field; // expected-warning {{self-comparison always evaluates to true}} |
| 87 |
return s3->field == s3->field; // expected-warning {{self-comparison always evaluates to true}} |
| 88 |
return s3->static_field == S::static_field; // expected-warning {{self-comparison always evaluates to true}} |
| 89 |
return s1.array[0] == s1.array[0]; // expected-warning {{self-comparison always evaluates to true}} |
| 90 |
return s1.array[0] == s1.array[0ull]; // expected-warning {{self-comparison always evaluates to true}} |
| 91 |
return s1.array[I1] == s1.array[I1]; // expected-warning {{self-comparison always evaluates to true}} |
| 92 |
return s1.array[s2.array[0]] == s1.array[s2.array[0]]; // expected-warning {{self-comparison always evaluates to true}} |
| 93 |
return s3->array[t.field] == s3->array[t.field]; // expected-warning {{self-comparison always evaluates to true}} |
| 94 |
|
| 95 |
// Try all operators |
| 96 |
return t.field == t.field; // expected-warning {{self-comparison always evaluates to true}} |
| 97 |
return t.field <= t.field; // expected-warning {{self-comparison always evaluates to true}} |
| 98 |
return t.field >= t.field; // expected-warning {{self-comparison always evaluates to true}} |
| 99 |
|
| 100 |
return t.field != t.field; // expected-warning {{self-comparison always evaluates to false}} |
| 101 |
return t.field < t.field; // expected-warning {{self-comparison always evaluates to false}} |
| 102 |
return t.field > t.field; // expected-warning {{self-comparison always evaluates to false}} |
| 103 |
|
| 104 |
// no warning |
| 105 |
return s1.field == s2.field; |
| 106 |
return s2.array == s1.array; // FIXME: This always evaluates to false. expected-warning {{deprecated}} |
| 107 |
return s2.array[0] == s1.array[0]; |
| 108 |
return s1.array[I1] == s1.array[I2]; |
| 109 |
|
| 110 |
return s1.static_field == t.static_field; |
| 111 |
}; |
| 112 |
|
| 113 |
struct U { |
| 114 |
bool operator!=(const U&); |
| 115 |
}; |
| 116 |
|
| 117 |
bool operator==(const U&, const U&); |
| 118 |
|
| 119 |
// May want to warn on this in the future. |
| 120 |
int user_defined(U u) { |
| 121 |
return u == u; |
| 122 |
return u != u; |
| 123 |
} |
| 124 |
|
| 125 |
} // namespace member_tests |