Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -Wno-unused -Wunused-comparison %s
      2 
      3 struct A {
      4   bool operator==(const A&);
      5   bool operator!=(const A&);
      6   bool operator<(const A&);
      7   bool operator>(const A&);
      8   bool operator<=(const A&);
      9   bool operator>=(const A&);
     10   A operator|=(const A&);
     11   operator bool();
     12 };
     13 
     14 void test() {
     15   int x, *p;
     16   A a, b;
     17 
     18   x == 7; // expected-warning {{equality comparison result unused}} \
     19           // expected-note {{use '=' to turn this equality comparison into an assignment}}
     20   x != 7; // expected-warning {{inequality comparison result unused}} \
     21           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
     22   x < 7;  // expected-warning {{relational comparison result unused}}
     23   x > 7;  // expected-warning {{relational comparison result unused}}
     24   x <= 7; // expected-warning {{relational comparison result unused}}
     25   x >= 7; // expected-warning {{relational comparison result unused}}
     26 
     27   7 == x; // expected-warning {{equality comparison result unused}}
     28   p == p; // expected-warning {{equality comparison result unused}} \
     29           // expected-note {{use '=' to turn this equality comparison into an assignment}} \
     30           // expected-warning {{self-comparison always evaluates to true}}
     31   a == a; // expected-warning {{equality comparison result unused}} \
     32           // expected-note {{use '=' to turn this equality comparison into an assignment}}
     33   a == b; // expected-warning {{equality comparison result unused}} \
     34           // expected-note {{use '=' to turn this equality comparison into an assignment}}
     35   a != b; // expected-warning {{inequality comparison result unused}} \
     36           // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}}
     37   a < b;  // expected-warning {{relational comparison result unused}}
     38   a > b;  // expected-warning {{relational comparison result unused}}
     39   a <= b; // expected-warning {{relational comparison result unused}}
     40   a >= b; // expected-warning {{relational comparison result unused}}
     41 
     42   A() == b; // expected-warning {{equality comparison result unused}}
     43   if (42) x == 7; // expected-warning {{equality comparison result unused}} \
     44                   // expected-note {{use '=' to turn this equality comparison into an assignment}}
     45   else if (42) x == 7; // expected-warning {{equality comparison result unused}} \
     46                        // expected-note {{use '=' to turn this equality comparison into an assignment}}
     47   else x == 7; // expected-warning {{equality comparison result unused}} \
     48                // expected-note {{use '=' to turn this equality comparison into an assignment}}
     49   do x == 7; // expected-warning {{equality comparison result unused}} \
     50              // expected-note {{use '=' to turn this equality comparison into an assignment}}
     51   while (false);
     52   while (false) x == 7; // expected-warning {{equality comparison result unused}} \
     53                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
     54   for (x == 7; // expected-warning {{equality comparison result unused}} \
     55                // expected-note {{use '=' to turn this equality comparison into an assignment}}
     56        x == 7; // No warning -- result is used
     57        x == 7) // expected-warning {{equality comparison result unused}} \
     58                // expected-note {{use '=' to turn this equality comparison into an assignment}}
     59     x == 7; // expected-warning {{equality comparison result unused}} \
     60             // expected-note {{use '=' to turn this equality comparison into an assignment}}
     61   switch (42) default: x == 7; // expected-warning {{equality comparison result unused}} \
     62                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
     63   switch (42) case 42: x == 7; // expected-warning {{equality comparison result unused}} \
     64                                // expected-note {{use '=' to turn this equality comparison into an assignment}}
     65   switch (42) {
     66     case 1:
     67     case 2:
     68     default:
     69     case 3:
     70     case 4:
     71       x == 7; // expected-warning {{equality comparison result unused}} \
     72               // expected-note {{use '=' to turn this equality comparison into an assignment}}
     73   }
     74 
     75   (void)(x == 7);
     76   (void)(p == p); // expected-warning {{self-comparison always evaluates to true}}
     77   { bool b = x == 7; }
     78 
     79   { bool b = ({ x == 7; // expected-warning {{equality comparison result unused}} \
     80                         // expected-note {{use '=' to turn this equality comparison into an assignment}}
     81                 x == 7; }); } // no warning on the second, its result is used!
     82 
     83 #define EQ(x,y) (x) == (y)
     84   EQ(x, 5);
     85 #undef EQ
     86 }
     87 
     88 namespace PR10291 {
     89   template<typename T>
     90   class X
     91   {
     92   public:
     93 
     94     X() : i(0) { }
     95 
     96     void foo()
     97     {
     98       throw
     99         i == 0u ?
    100         5 : 6;
    101     }
    102 
    103   private:
    104     int i;
    105   };
    106 
    107   X<int> x;
    108 }
    109 
    110 namespace PR19724 {
    111 class stream {
    112 } cout, cin;
    113 
    114 stream &operator<(stream &s, int);
    115 bool operator<(stream &s, stream &s2);
    116 
    117 void test() {
    118   cout < 5;    // no warning, operator returns a reference
    119   cout < cin;  // expected-warning {{relational comparison result unused}}
    120 }
    121 }
    122 
    123 namespace PR19791 {
    124 struct S {
    125   void operator!=(int);
    126   int operator==(int);
    127 };
    128 
    129 void test() {
    130   S s;
    131   s != 1;
    132   s == 1;  // expected-warning{{equality comparison result unused}}
    133            // expected-note@-1{{use '=' to turn this equality comparison into an assignment}}
    134 }
    135 }
    136