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
f()3 template<typename T> void f() {
4   T t;
5   t = 17;
6 }
7 
8 // PR5407
9 struct A { A(); };
10 struct B { ~B(); };
f()11 void f() {
12   A a;
13   B b;
14 }
15 
16 // PR5531
17 namespace PR5531 {
18   struct A {
19   };
20 
21   struct B {
22     B(int);
23   };
24 
25   struct C {
26     ~C();
27   };
28 
test()29   void test() {
30     A();
31     B(17);
32     C();
33   }
34 }
35 
36 template<typename T>
37 struct X0 { };
38 
39 template<typename T>
test_dependent_init(T * p)40 void test_dependent_init(T *p) {
41   X0<int> i(p);
42   (void)i;
43 }
44 
unused_local_static()45 void 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
52 namespace 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 {
fPR10168::S57     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>
f()64   void f() {
65     int a; // expected-warning {{unused variable 'a'}}
66     T b; // expected-warning 2{{unused variable 'b'}}
67   }
68 
g()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 
77 namespace PR11550 {
78   struct S1 {
79     S1();
80   };
81   S1 makeS1();
testS1(S1 a)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 {
S2PR11550::S296     S2() {
97       foo();
98     }
99   };
100   S2 makeS2();
testS2(S2 a)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();
testS3(S3 a)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 
119 namespace 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   // This has external linkage, so could be referenced by a declaration in a
139   // different translation unit.
140   template<> const int m<void> = 0; // no warning
141 }
142 
143 namespace ctor_with_cleanups {
144   struct S1 {
145     ~S1();
146   };
147   struct S2 {
148     S2(const S1&);
149   };
func()150   void func() {
151     S2 s((S1()));
152   }
153 }
154 
155 #include "Inputs/warn-unused-variables.h"
156 
157 namespace arrayRecords {
158 
159 class NonTriviallyDestructible {
160 public:
~NonTriviallyDestructible()161   ~NonTriviallyDestructible() {}
162 };
163 
164 struct Foo {
165   int x;
FooarrayRecords::Foo166   Foo(int x) : x(x) {}
167 };
168 
169 struct Elidable {
170   Elidable();
171 };
172 
foo(int size)173 void foo(int size) {
174   Elidable elidable; // no warning
175   Elidable elidableArray[2]; // no warning
176   Elidable elidableDynArray[size]; // no warning
177   Elidable elidableNestedArray[1][2][3]; // no warning
178 
179   NonTriviallyDestructible scalar; // no warning
180   NonTriviallyDestructible array[2];  // no warning
181   NonTriviallyDestructible nestedArray[2][2]; // no warning
182 
183   Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}}
184   Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
185   Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
186 }
187 
188 template<int N>
bar()189 void bar() {
190   NonTriviallyDestructible scaler; // no warning
191   NonTriviallyDestructible array[N]; // no warning
192 }
193 
test()194 void test() {
195   foo(10);
196   bar<2>();
197 }
198 
199 }
200 
201 #if __cplusplus >= 201103L
202 namespace with_constexpr {
203 template <typename T>
204 struct Literal {
205   T i;
206   Literal() = default;
Literalwith_constexpr::Literal207   constexpr Literal(T i) : i(i) {}
208 };
209 
210 struct NoLiteral {
211   int i;
212   NoLiteral() = default;
NoLiteralwith_constexpr::NoLiteral213   constexpr NoLiteral(int i) : i(i) {}
~NoLiteralwith_constexpr::NoLiteral214   ~NoLiteral() {}
215 };
216 
217 static Literal<int> gl1;          // expected-warning {{unused variable 'gl1'}}
218 static Literal<int> gl2(1);       // expected-warning {{unused variable 'gl2'}}
219 static const Literal<int> gl3(0); // expected-warning {{unused variable 'gl3'}}
220 
221 template <typename T>
test(int i)222 void test(int i) {
223   Literal<int> l1;     // expected-warning {{unused variable 'l1'}}
224   Literal<int> l2(42); // expected-warning {{unused variable 'l2'}}
225   Literal<int> l3(i);  // no-warning
226   Literal<T> l4(0);    // no-warning
227   NoLiteral nl1;       // no-warning
228   NoLiteral nl2(42);   // no-warning
229 }
230 }
231 
232 namespace crash {
233 struct a {
234   a(const char *);
235 };
236 template <typename b>
c()237 void c() {
238   a d(b::e ? "" : "");
239 }
240 }
241 
242 // Ensure we don't warn on dependent constructor calls.
243 namespace dependent_ctor {
244 struct S {
245   S() = default;
246   S(const S &) = default;
247   S(int);
248 };
249 
250 template <typename T>
foo(T & t)251 void foo(T &t) {
252   S s{t};
253 }
254 }
255 #endif
256