Clang Project

clang_source_code/test/SemaCXX/self-comparison.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++2a
2
3int foo(int x) {
4  return x == x; // expected-warning {{self-comparison always evaluates to true}}
5}
6
7struct X {
8  bool operator==(const X &x);
9};
10
11struct A {
12  int x;
13  X x2;
14  int a[3];
15  int b[3];
16  bool f() { return x == x; } // expected-warning {{self-comparison always evaluates to true}}
17  bool g() { return x2 == x2; } // no-warning
18  bool h() { return a == b; } // expected-warning {{array comparison always evaluates to false}}
19  bool i() {
20    int c[3];
21    return a == c; // expected-warning {{array comparison always evaluates to false}}
22  }
23};
24
25namespace NA { extern "C" int x[3]; }
26namespace NB { extern "C" int x[3]; }
27bool k = NA::x == NB::x; // expected-warning {{self-comparison always evaluates to true}}
28
29template<typename T> struct Y { static inline int n; };
30bool f() {
31  return
32    Y<int>::n == Y<int>::n || // expected-warning {{self-comparison always evaluates to true}}
33    Y<void>::n == Y<int>::n;
34}
35template<typename T, typename U>
36bool g() {
37  // FIXME: Ideally we'd produce a self-comparison warning on the first of these.
38  return
39    Y<T>::n == Y<T>::n ||
40    Y<T>::n == Y<U>::n;
41}
42template bool g<int, int>(); // should not produce any warnings
43