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 A operator|=(const A&); 7 operator bool(); 8 }; 9 10 void test() { 11 int x, *p; 12 A a, b; 13 14 x == 7; // expected-warning {{equality comparison result unused}} \ 15 // expected-note {{use '=' to turn this equality comparison into an assignment}} 16 x != 7; // expected-warning {{inequality comparison result unused}} \ 17 // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}} 18 7 == x; // expected-warning {{equality comparison result unused}} 19 p == p; // expected-warning {{equality comparison result unused}} \ 20 // expected-note {{use '=' to turn this equality comparison into an assignment}} \ 21 // expected-warning {{self-comparison always evaluates to true}} 22 a == a; // expected-warning {{equality comparison result unused}} \ 23 // expected-note {{use '=' to turn this equality comparison into an assignment}} 24 a == b; // expected-warning {{equality comparison result unused}} \ 25 // expected-note {{use '=' to turn this equality comparison into an assignment}} 26 a != b; // expected-warning {{inequality comparison result unused}} \ 27 // expected-note {{use '|=' to turn this inequality comparison into an or-assignment}} 28 A() == b; // expected-warning {{equality comparison result unused}} 29 if (42) x == 7; // expected-warning {{equality comparison result unused}} \ 30 // expected-note {{use '=' to turn this equality comparison into an assignment}} 31 else if (42) x == 7; // expected-warning {{equality comparison result unused}} \ 32 // expected-note {{use '=' to turn this equality comparison into an assignment}} 33 else x == 7; // expected-warning {{equality comparison result unused}} \ 34 // expected-note {{use '=' to turn this equality comparison into an assignment}} 35 do x == 7; // expected-warning {{equality comparison result unused}} \ 36 // expected-note {{use '=' to turn this equality comparison into an assignment}} 37 while (false); 38 while (false) x == 7; // expected-warning {{equality comparison result unused}} \ 39 // expected-note {{use '=' to turn this equality comparison into an assignment}} 40 for (x == 7; // expected-warning {{equality comparison result unused}} \ 41 // expected-note {{use '=' to turn this equality comparison into an assignment}} 42 x == 7; // No warning -- result is used 43 x == 7) // expected-warning {{equality comparison result unused}} \ 44 // expected-note {{use '=' to turn this equality comparison into an assignment}} 45 x == 7; // expected-warning {{equality comparison result unused}} \ 46 // expected-note {{use '=' to turn this equality comparison into an assignment}} 47 switch (42) default: x == 7; // expected-warning {{equality comparison result unused}} \ 48 // expected-note {{use '=' to turn this equality comparison into an assignment}} 49 switch (42) case 42: x == 7; // expected-warning {{equality comparison result unused}} \ 50 // expected-note {{use '=' to turn this equality comparison into an assignment}} 51 switch (42) { 52 case 1: 53 case 2: 54 default: 55 case 3: 56 case 4: 57 x == 7; // expected-warning {{equality comparison result unused}} \ 58 // expected-note {{use '=' to turn this equality comparison into an assignment}} 59 } 60 61 (void)(x == 7); 62 (void)(p == p); // expected-warning {{self-comparison always evaluates to true}} 63 { bool b = x == 7; } 64 65 { bool b = ({ x == 7; // expected-warning {{equality comparison result unused}} \ 66 // expected-note {{use '=' to turn this equality comparison into an assignment}} 67 x == 7; }); } // no warning on the second, its result is used! 68 69 #define EQ(x,y) (x) == (y) 70 EQ(x, 5); 71 #undef EQ 72 } 73 74 namespace PR10291 { 75 template<typename T> 76 class X 77 { 78 public: 79 80 X() : i(0) { } 81 82 void foo() 83 { 84 throw 85 i == 0u ? 86 5 : 6; 87 } 88 89 private: 90 int i; 91 }; 92 93 X<int> x; 94 } 95