Clang Project

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