181 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s2// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value -std=c++98 %s3// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value -std=c++11 %s4// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value -std=c++17 %s5 6// PR48067namespace test0 {8 class Box {9 public:10 int i;11 volatile int j;12 };13 14 void doit() {15 // pointer to volatile has side effect (thus no warning)16 Box* box = new Box;17 box->i; // expected-warning {{expression result unused}}18 box->j;19#if __cplusplus <= 199711L20 // expected-warning@-2 {{expression result unused}}21#endif22 }23}24 25namespace test1 {26struct Foo {27 int i;28 bool operator==(const Foo& rhs) {29 return i == rhs.i;30 }31};32 33#define NOP(x) (x)34void b(Foo f1, Foo f2) {35 NOP(f1 == f2); // expected-warning {{expression result unused}}36}37#undef NOP38}39 40namespace test2 {41 extern "C++" {42 namespace std {43 template<typename T> struct basic_string {44 struct X {};45 void method() const {46 X* x;47 &x[0]; // expected-warning {{expression result unused}}48 }49 };50 typedef basic_string<char> string;51 void func(const std::string& str) {52 str.method(); // expected-note {{in instantiation of member function}}53 }54 }55 }56}57 58namespace test3 {59struct Used {60 Used();61 Used(int);62 Used(int, int);63 ~Used() {}64};65struct __attribute__((warn_unused)) Unused {66 Unused();67 Unused(int);68 Unused(int, int);69 ~Unused() {}70};71void f() {72 Used();73 Used(1);74 Used(1, 1);75 Unused(); // expected-warning {{expression result unused}}76 Unused(1); // expected-warning {{expression result unused}}77 Unused(1, 1); // expected-warning {{expression result unused}}78#if __cplusplus >= 201103L // C++11 or later79 Used({});80 Unused({}); // expected-warning {{expression result unused}}81#endif82}83}84 85namespace std {86 struct type_info {};87}88 89namespace test4 {90struct Good { Good &f(); };91struct Bad { virtual Bad& f(); };92 93void f() {94 int i = 0;95 (void)typeid(++i); // expected-warning {{expression with side effects has no effect in an unevaluated context}}96 97 Good g;98 (void)typeid(g.f()); // Ok; not a polymorphic use of a glvalue.99 100 // This is a polymorphic use of a glvalue, which results in the typeid being101 // evaluated instead of unevaluated.102 Bad b;103 (void)typeid(b.f()); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}104 105 extern Bad * pb;106 // This typeid can throw but that is not a side-effect that we care about107 // warning for since this is idiomatic code108 (void)typeid(*pb);109 (void)sizeof(typeid(*pb));110 (void)typeid(*++pb); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}111 (void)sizeof(typeid(*++pb)); // expected-warning {{expression with side effects has no effect in an unevaluated context}}112 // FIXME: we should not warn about this in an unevaluated context113 // expected-warning@-2 {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}114 115 // A dereference of a volatile pointer is a side effecting operation, however116 // since it is idiomatic code, and the alternatives induce higher maintenance117 // costs, it is allowed.118 int * volatile x;119 (void)sizeof(*x); // Ok120}121}122 123static volatile char var1 = 'a';124volatile char var2 = 'a';125static volatile char arr1[] = "hello";126volatile char arr2[] = "hello";127void volatile_array() {128 static volatile char var3 = 'a';129 volatile char var4 = 'a';130 static volatile char arr3[] = "hello";131 volatile char arr4[] = "hello";132 133 // These all result in volatile loads in C and C++11. In C++98, they don't,134 // but we suppress the warning in the case where '(void)var;' might be135 // idiomatically suppressing an 'unused variable' warning.136 (void)var1;137 (void)var2;138#if __cplusplus < 201103L139 // expected-warning@-2 {{expression result unused; assign into a variable to force a volatile load}}140#endif141 (void)var3;142 (void)var4;143 144 // None of these result in volatile loads in any language mode, and it's not145 // really reasonable to assume that they would, since volatile array loads146 // don't really exist anywhere.147 (void)arr1;148 (void)arr2;149 (void)arr3;150 (void)arr4;151}152 153#if __cplusplus >= 201103L // C++11 or later154namespace test5 {155int v[(5, 6)]; // expected-warning {{left operand of comma operator has no effect}}156void foo() {157 new double[false ? (1, 2) : 3]158 // FIXME: We shouldn't diagnose the unreachable constant expression159 // here.160 [false ? (1, 2) : 3]; // expected-warning {{left operand of comma operator has no effect}}161}162} // namespace test5163 164// comma operator diagnostics should be suppressed in a SFINAE context.165template <typename T, int = (T{},0)> int c(int) { return 0; }166template <typename T, int> int c(double) { return 1; }167int foo() { return c<int>(0); }168 169#endif170 171#if __cplusplus >= 201703L // C++17 or later172namespace test6 {173auto b() {174 if constexpr (false)175 return (1,0);176 else177 return (1.0,0.0); // expected-warning {{left operand of comma operator has no effect}}178}179} // namespace test6180#endif181