Clang Project

clang_source_code/test/SemaCXX/warn-unused-variables.cpp
1// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s
2// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=c++11 %s
3template<typename T> void f() {
4  T t;
5  t = 17;
6}
7
8// PR5407
9struct A { A(); };
10struct B { ~B(); };
11void f() {
12  A a;
13  B b;
14}
15
16// PR5531
17namespace PR5531 {
18  struct A {
19  };
20
21  struct B {
22    B(int);
23  };
24
25  struct C {
26    ~C();
27  };
28
29  void test() {
30    A();
31    B(17);
32    C();
33  }
34}
35
36template<typename T>
37struct X0 { };
38
39template<typename T>
40void test_dependent_init(T *p) {
41  X0<int> i(p);
42  (void)i;
43}
44
45void unused_local_static() {
46  static int x = 0;
47  static int y = 0; // expected-warning{{unused variable 'y'}}
48#pragma unused(x)
49}
50
51// PR10168
52namespace PR10168 {
53  // We expect a warning in the definition only for non-dependent variables, and
54  // a warning in the instantiation only for dependent variables.
55  template<typename T>
56  struct S {
57    void f() {
58      int a; // expected-warning {{unused variable 'a'}}
59      T b; // expected-warning 2{{unused variable 'b'}}
60    }
61  };
62
63  template<typename T>
64  void f() {
65    int a; // expected-warning {{unused variable 'a'}}
66    T b; // expected-warning 2{{unused variable 'b'}}
67  }
68
69  void g() {
70    S<int>().f(); // expected-note {{here}}
71    S<char>().f(); // expected-note {{here}}
72    f<int>(); // expected-note {{here}}
73    f<char>(); // expected-note {{here}}
74  }
75}
76
77namespace PR11550 {
78  struct S1 {
79    S1();
80  };
81  S1 makeS1();
82  void testS1(S1 a) {
83    // This constructor call can be elided.
84    S1 x = makeS1(); // expected-warning {{unused variable 'x'}}
85
86    // This one cannot, so no warning.
87    S1 y;
88
89    // This call cannot, but the constructor is trivial.
90    S1 z = a; // expected-warning {{unused variable 'z'}}
91  }
92
93  // The same is true even when we know thet constructor has side effects.
94  void foo();
95  struct S2 {
96    S2() {
97      foo();
98    }
99  };
100  S2 makeS2();
101  void testS2(S2 a) {
102    S2 x = makeS2(); // expected-warning {{unused variable 'x'}}
103    S2 y;
104    S2 z = a; // expected-warning {{unused variable 'z'}}
105  }
106
107  // Or when the constructor is not declared by the user.
108  struct S3 {
109    S1 m;
110  };
111  S3 makeS3();
112  void testS3(S3 a) {
113    S3 x = makeS3(); // expected-warning {{unused variable 'x'}}
114    S3 y;
115    S3 z = a; // expected-warning {{unused variable 'z'}}
116  }
117}
118
119namespace PR19305 {
120  template<typename T> int n = 0; // no warning
121  int a = n<int>;
122
123  template<typename T> const int l = 0; // no warning
124  int b = l<int>;
125
126  // PR19558
127  template<typename T> const int o = 0; // no warning
128  template<typename T> const int o<T*> = 0; // no warning
129  int c = o<int*>;
130
131  template<> int o<void> = 0; // no warning
132  int d = o<void>;
133
134  // FIXME: It'd be nice to warn here.
135  template<typename T> int m = 0;
136  template<typename T> int m<T*> = 0;
137
138  template<> const int m<void> = 0; // expected-warning {{unused variable}}
139}
140
141namespace ctor_with_cleanups {
142  struct S1 {
143    ~S1();
144  };
145  struct S2 {
146    S2(const S1&);
147  };
148  void func() {
149    S2 s((S1()));
150  }
151}
152
153#include "Inputs/warn-unused-variables.h"
154
155namespace arrayRecords {
156
157class NonTriviallyDestructible {
158public:
159  ~NonTriviallyDestructible() {}
160};
161
162struct Foo {
163  int x;
164  Foo(int x) : x(x) {}
165};
166
167struct Elidable {
168  Elidable();
169};
170
171void foo(int size) {
172  Elidable elidable; // no warning
173  Elidable elidableArray[2]; // no warning
174  Elidable elidableDynArray[size]; // no warning
175  Elidable elidableNestedArray[1][2][3]; // no warning
176
177  NonTriviallyDestructible scalar; // no warning
178  NonTriviallyDestructible array[2];  // no warning
179  NonTriviallyDestructible nestedArray[2][2]; // no warning
180
181  Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}}
182  Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
183  Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
184}
185
186template<int N>
187void bar() {
188  NonTriviallyDestructible scaler; // no warning
189  NonTriviallyDestructible array[N]; // no warning
190}
191
192void test() {
193  foo(10);
194  bar<2>();
195}
196
197}
198
199#if __cplusplus >= 201103L
200namespace with_constexpr {
201template <typename T>
202struct Literal {
203  T i;
204  Literal() = default;
205  constexpr Literal(T i) : i(i) {}
206};
207
208struct NoLiteral {
209  int i;
210  NoLiteral() = default;
211  constexpr NoLiteral(int i) : i(i) {}
212  ~NoLiteral() {}
213};
214
215static Literal<int> gl1;          // expected-warning {{unused variable 'gl1'}}
216static Literal<int> gl2(1);       // expected-warning {{unused variable 'gl2'}}
217static const Literal<int> gl3(0); // expected-warning {{unused variable 'gl3'}}
218
219template <typename T>
220void test(int i) {
221  Literal<int> l1;     // expected-warning {{unused variable 'l1'}}
222  Literal<int> l2(42); // expected-warning {{unused variable 'l2'}}
223  Literal<int> l3(i);  // no-warning
224  Literal<T> l4(0);    // no-warning
225  NoLiteral nl1;       // no-warning
226  NoLiteral nl2(42);   // no-warning
227}
228}
229
230namespace crash {
231struct a {
232  a(const char *);
233};
234template <typename b>
235void c() {
236  a d(b::e ? "" : "");
237}
238}
239#endif
240