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=gnu++11 %s
3 template<typename T
> void f() {
40 void test_dependent_init(T
*p
) {
45 void unused_local_static() {
47 static int y
= 0; // expected-warning{{unused variable 'y'}}
49 static __attribute__((used
)) int z
;
50 static __attribute__((unused
)) int w
;
51 [[maybe_unused
]] static int v
;
56 // We expect a warning in the definition only for non-dependent variables, and
57 // a warning in the instantiation only for dependent variables.
61 int a
; // expected-warning {{unused variable 'a'}}
62 T b
; // expected-warning 2{{unused variable 'b'}}
68 int a
; // expected-warning {{unused variable 'a'}}
69 T b
; // expected-warning 2{{unused variable 'b'}}
73 S
<int>().f(); // expected-note {{here}}
74 S
<char>().f(); // expected-note {{here}}
75 f
<int>(); // expected-note {{here}}
76 f
<char>(); // expected-note {{here}}
86 // This constructor call can be elided.
87 S1 x
= makeS1(); // expected-warning {{unused variable 'x'}}
89 // This one cannot, so no warning.
92 // This call cannot, but the constructor is trivial.
93 S1 z
= a
; // expected-warning {{unused variable 'z'}}
96 // The same is true even when we know thet constructor has side effects.
105 S2 x
= makeS2(); // expected-warning {{unused variable 'x'}}
107 S2 z
= a
; // expected-warning {{unused variable 'z'}}
110 // Or when the constructor is not declared by the user.
116 S3 x
= makeS3(); // expected-warning {{unused variable 'x'}}
118 S3 z
= a
; // expected-warning {{unused variable 'z'}}
123 template<typename T
> int n
= 0; // no warning
126 template<typename T
> const int l
= 0; // no warning
130 template<typename T
> const int o
= 0; // no warning
131 template<typename T
> const int o
<T
*> = 0; // no warning
134 template<> int o
<void> = 0; // no warning
137 // FIXME: It'd be nice to warn here.
138 template<typename T
> int m
= 0;
139 template<typename T
> int m
<T
*> = 0;
141 // This has external linkage, so could be referenced by a declaration in a
142 // different translation unit.
143 template<> const int m
<void> = 0; // no warning
146 namespace ctor_with_cleanups
{
158 #include "Inputs/warn-unused-variables.h"
160 class NonTriviallyDestructible
{
162 ~NonTriviallyDestructible() {}
165 namespace arrayRecords
{
177 Elidable elidable
; // no warning
178 Elidable elidableArray
[2]; // no warning
179 Elidable elidableDynArray
[size
]; // no warning
180 Elidable elidableNestedArray
[1][2][3]; // no warning
182 NonTriviallyDestructible scalar
; // no warning
183 NonTriviallyDestructible array
[2]; // no warning
184 NonTriviallyDestructible nestedArray
[2][2]; // no warning
186 Foo fooScalar
= 1; // expected-warning {{unused variable 'fooScalar'}}
187 Foo fooArray
[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
188 Foo fooNested
[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
193 NonTriviallyDestructible scaler
; // no warning
194 NonTriviallyDestructible array
[N
]; // no warning
202 } // namespace arrayRecords
204 #if __cplusplus >= 201103L
205 namespace with_constexpr
{
206 template <typename T
>
210 constexpr Literal(T i
) : i(i
) {}
215 NoLiteral() = default;
216 constexpr NoLiteral(int i
) : i(i
) {}
220 static Literal
<int> gl1
; // expected-warning {{unused variable 'gl1'}}
221 static Literal
<int> gl2(1); // expected-warning {{unused variable 'gl2'}}
222 static const Literal
<int> gl3(0); // expected-warning {{unused variable 'gl3'}}
224 template <typename T
>
226 Literal
<int> l1
; // expected-warning {{unused variable 'l1'}}
227 Literal
<int> l2(42); // expected-warning {{unused variable 'l2'}}
228 Literal
<int> l3(i
); // no-warning
229 Literal
<T
> l4(0); // no-warning
230 NoLiteral nl1
; // no-warning
231 NoLiteral
nl2(42); // no-warning
239 template <typename b
>
245 // Ensure we don't warn on dependent constructor calls.
246 namespace dependent_ctor
{
249 S(const S
&) = default;
253 template <typename T
>
260 // Ensure we do not warn on lifetime extension
264 const auto &a
= NonTriviallyDestructible();
265 const auto &b
= a
; // expected-warning {{unused variable 'b'}}
266 #if __cplusplus >= 201103L
267 const auto &&c
= NonTriviallyDestructible();
268 auto &&d
= c
; // expected-warning {{unused variable 'd'}}
274 S(const S
&) = default;
278 template <typename T
>
280 const auto &extended
= S
{t
};
293 void RAIIWrapperTest() {
294 auto const guard
= RAIIWrapper();
295 auto const &guard2
= RAIIWrapper();
296 auto &&guard3
= RAIIWrapper();
299 } // namespace gh54489