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=expected,cxx98-14 -std=gnu++11 %s
3 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify=expected,cxx98-14 -std=gnu++14 %s
4 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++17 %s
6 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s -fexperimental-new-constant-interpreter
7 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify=expected,cxx98-14 -std=gnu++11 %s -fexperimental-new-constant-interpreter
8 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify=expected,cxx98-14 -std=gnu++14 %s -fexperimental-new-constant-interpreter
9 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++17 %s -fexperimental-new-constant-interpreter
11 template<typename T
> void f() {
48 void test_dependent_init(T
*p
) {
53 void unused_local_static() {
55 static int y
= 0; // expected-warning{{unused variable 'y'}}
57 static __attribute__((used
)) int z
;
58 static __attribute__((unused
)) int w
;
59 [[maybe_unused
]] static int v
;
64 // We expect a warning in the definition only for non-dependent variables, and
65 // a warning in the instantiation only for dependent variables.
69 int a
; // expected-warning {{unused variable 'a'}}
70 T b
; // expected-warning 2{{unused variable 'b'}}
76 int a
; // expected-warning {{unused variable 'a'}}
77 T b
; // expected-warning 2{{unused variable 'b'}}
81 S
<int>().f(); // expected-note {{here}}
82 S
<char>().f(); // expected-note {{here}}
83 f
<int>(); // expected-note {{here}}
84 f
<char>(); // expected-note {{here}}
94 // This constructor call can be elided.
95 S1 x
= makeS1(); // expected-warning {{unused variable 'x'}}
97 // This one cannot, so no warning.
100 // This call cannot, but the constructor is trivial.
101 S1 z
= a
; // expected-warning {{unused variable 'z'}}
104 // The same is true even when we know thet constructor has side effects.
113 S2 x
= makeS2(); // expected-warning {{unused variable 'x'}}
115 S2 z
= a
; // expected-warning {{unused variable 'z'}}
118 // Or when the constructor is not declared by the user.
124 S3 x
= makeS3(); // expected-warning {{unused variable 'x'}}
126 S3 z
= a
; // expected-warning {{unused variable 'z'}}
131 template<typename T
> int n
= 0; // no warning
134 template<typename T
> const int l
= 0; // no warning
138 template<typename T
> const int o
= 0; // no warning
139 template<typename T
> const int o
<T
*> = 0; // no warning
142 template<> int o
<void> = 0; // no warning
145 // FIXME: It'd be nice to warn here.
146 template<typename T
> int m
= 0;
147 template<typename T
> int m
<T
*> = 0;
149 // This has external linkage, so could be referenced by a declaration in a
150 // different translation unit.
151 template<> const int m
<void> = 0; // no warning
154 namespace ctor_with_cleanups
{
166 #include "Inputs/warn-unused-variables.h"
168 class NonTriviallyDestructible
{
170 ~NonTriviallyDestructible() {}
173 namespace arrayRecords
{
185 Elidable elidable
; // no warning
186 Elidable elidableArray
[2]; // no warning
187 Elidable elidableDynArray
[size
]; // no warning
188 Elidable elidableNestedArray
[1][2][3]; // no warning
190 NonTriviallyDestructible scalar
; // no warning
191 NonTriviallyDestructible array
[2]; // no warning
192 NonTriviallyDestructible nestedArray
[2][2]; // no warning
194 // Copy initialzation gives warning before C++17
195 Foo fooScalar
= 1; // cxx98-14-warning {{unused variable 'fooScalar'}}
196 Foo fooArray
[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
197 Foo fooNested
[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
202 NonTriviallyDestructible scaler
; // no warning
203 NonTriviallyDestructible array
[N
]; // no warning
211 } // namespace arrayRecords
213 #if __cplusplus >= 201103L
214 namespace with_constexpr
{
215 template <typename T
>
219 constexpr Literal(T i
) : i(i
) {}
224 NoLiteral() = default;
225 constexpr NoLiteral(int i
) : i(i
) {}
229 static Literal
<int> gl1
; // expected-warning {{unused variable 'gl1'}}
230 static Literal
<int> gl2(1); // expected-warning {{unused variable 'gl2'}}
231 static const Literal
<int> gl3(0); // expected-warning {{unused variable 'gl3'}}
233 template <typename T
>
235 Literal
<int> l1
; // expected-warning {{unused variable 'l1'}}
236 Literal
<int> l2(42); // expected-warning {{unused variable 'l2'}}
237 Literal
<int> l3(i
); // no-warning
238 Literal
<T
> l4(0); // no-warning
239 NoLiteral nl1
; // no-warning
240 NoLiteral
nl2(42); // no-warning
248 template <typename b
>
254 // Ensure we don't warn on dependent constructor calls.
255 namespace dependent_ctor
{
258 S(const S
&) = default;
262 template <typename T
>
269 // Ensure we do not warn on lifetime extension
273 const auto &a
= NonTriviallyDestructible();
274 const auto &b
= a
; // expected-warning {{unused variable 'b'}}
275 #if __cplusplus >= 201103L
276 const auto &&c
= NonTriviallyDestructible();
277 auto &&d
= c
; // expected-warning {{unused variable 'd'}}
283 S(const S
&) = default;
287 template <typename T
>
289 const auto &extended
= S
{t
};
302 void RAIIWrapperTest() {
303 auto const guard
= RAIIWrapper();
304 auto const &guard2
= RAIIWrapper();
305 auto &&guard3
= RAIIWrapper();
308 } // namespace gh54489
310 // Ensure that -Wunused-variable does not emit warning
311 // on copy constructors with side effects (C++17 and later)
312 #if __cplusplus >= 201703L
319 // With an initializer list
326 S
s(0); // no warning
327 S s2
= 0; // no warning
328 S s3
{0}; // no warning
330 A a
= 1; // no warning
333 } // namespace gh79518