1 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s 2 3 // PR4806 4 namespace test0 { 5 class Box { 6 public: 7 int i; 8 volatile int j; 9 }; 10 11 void doit() { 12 // pointer to volatile has side effect (thus no warning) 13 Box* box = new Box; 14 box->i; // expected-warning {{expression result unused}} 15 box->j; // expected-warning {{expression result unused}} 16 } 17 } 18 19 namespace test1 { 20 struct Foo { 21 int i; 22 bool operator==(const Foo& rhs) { 23 return i == rhs.i; 24 } 25 }; 26 27 #define NOP(x) (x) 28 void b(Foo f1, Foo f2) { 29 NOP(f1 == f2); // expected-warning {{expression result unused}} 30 } 31 #undef NOP 32 } 33 34 namespace test2 { 35 extern "C++" { 36 namespace std { 37 template<typename T> struct basic_string { 38 struct X {}; 39 void method() const { 40 X* x; 41 &x[0]; // expected-warning {{expression result unused}} 42 } 43 }; 44 typedef basic_string<char> string; 45 void func(const std::string& str) { 46 str.method(); // expected-note {{in instantiation of member function}} 47 } 48 } 49 } 50 } 51 52 namespace test3 { 53 struct Used { 54 Used(); 55 Used(int); 56 Used(int, int); 57 }; 58 struct __attribute__((warn_unused)) Unused { 59 Unused(); 60 Unused(int); 61 Unused(int, int); 62 }; 63 void f() { 64 Used(); 65 Used(1); 66 Used(1, 1); 67 Unused(); // expected-warning {{expression result unused}} 68 Unused(1); // expected-warning {{expression result unused}} 69 Unused(1, 1); // expected-warning {{expression result unused}} 70 } 71 } 72 73 namespace std { 74 struct type_info {}; 75 } 76 77 namespace test4 { 78 struct Good { Good &f(); }; 79 struct Bad { virtual Bad& f(); }; 80 81 void f() { 82 int i = 0; 83 (void)typeid(++i); // expected-warning {{expression with side effects has no effect in an unevaluated context}} 84 85 Good g; 86 (void)typeid(g.f()); // Ok; not a polymorphic use of a glvalue. 87 88 // This is a polymorphic use of a glvalue, which results in the typeid being 89 // evaluated instead of unevaluated. 90 Bad b; 91 (void)typeid(b.f()); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}} 92 93 // A dereference of a volatile pointer is a side effecting operation, however 94 // since it is idiomatic code, and the alternatives induce higher maintenance 95 // costs, it is allowed. 96 int * volatile x; 97 (void)sizeof(*x); // Ok 98 } 99 } 100