xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-unused-variables.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s
f()2f4a2713aSLionel Sambuc template<typename T> void f() {
3f4a2713aSLionel Sambuc   T t;
4f4a2713aSLionel Sambuc   t = 17;
5f4a2713aSLionel Sambuc }
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc // PR5407
8f4a2713aSLionel Sambuc struct A { A(); };
9f4a2713aSLionel Sambuc struct B { ~B(); };
f()10f4a2713aSLionel Sambuc void f() {
11f4a2713aSLionel Sambuc   A a;
12f4a2713aSLionel Sambuc   B b;
13f4a2713aSLionel Sambuc }
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc // PR5531
16f4a2713aSLionel Sambuc namespace PR5531 {
17f4a2713aSLionel Sambuc   struct A {
18f4a2713aSLionel Sambuc   };
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc   struct B {
21f4a2713aSLionel Sambuc     B(int);
22f4a2713aSLionel Sambuc   };
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc   struct C {
25f4a2713aSLionel Sambuc     ~C();
26f4a2713aSLionel Sambuc   };
27f4a2713aSLionel Sambuc 
test()28f4a2713aSLionel Sambuc   void test() {
29f4a2713aSLionel Sambuc     A();
30f4a2713aSLionel Sambuc     B(17);
31f4a2713aSLionel Sambuc     C();
32f4a2713aSLionel Sambuc   }
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc template<typename T>
36f4a2713aSLionel Sambuc struct X0 { };
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc template<typename T>
test_dependent_init(T * p)39f4a2713aSLionel Sambuc void test_dependent_init(T *p) {
40f4a2713aSLionel Sambuc   X0<int> i(p);
41f4a2713aSLionel Sambuc   (void)i;
42f4a2713aSLionel Sambuc }
43f4a2713aSLionel Sambuc 
unused_local_static()44f4a2713aSLionel Sambuc void unused_local_static() {
45f4a2713aSLionel Sambuc   static int x = 0;
46f4a2713aSLionel Sambuc   static int y = 0; // expected-warning{{unused variable 'y'}}
47f4a2713aSLionel Sambuc #pragma unused(x)
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc // PR10168
51f4a2713aSLionel Sambuc namespace PR10168 {
52f4a2713aSLionel Sambuc   // We expect a warning in the definition only for non-dependent variables, and
53f4a2713aSLionel Sambuc   // a warning in the instantiation only for dependent variables.
54f4a2713aSLionel Sambuc   template<typename T>
55f4a2713aSLionel Sambuc   struct S {
fPR10168::S56f4a2713aSLionel Sambuc     void f() {
57f4a2713aSLionel Sambuc       int a; // expected-warning {{unused variable 'a'}}
58f4a2713aSLionel Sambuc       T b; // expected-warning 2{{unused variable 'b'}}
59f4a2713aSLionel Sambuc     }
60f4a2713aSLionel Sambuc   };
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   template<typename T>
f()63f4a2713aSLionel Sambuc   void f() {
64f4a2713aSLionel Sambuc     int a; // expected-warning {{unused variable 'a'}}
65f4a2713aSLionel Sambuc     T b; // expected-warning 2{{unused variable 'b'}}
66f4a2713aSLionel Sambuc   }
67f4a2713aSLionel Sambuc 
g()68f4a2713aSLionel Sambuc   void g() {
69f4a2713aSLionel Sambuc     S<int>().f(); // expected-note {{here}}
70f4a2713aSLionel Sambuc     S<char>().f(); // expected-note {{here}}
71f4a2713aSLionel Sambuc     f<int>(); // expected-note {{here}}
72f4a2713aSLionel Sambuc     f<char>(); // expected-note {{here}}
73f4a2713aSLionel Sambuc   }
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc namespace PR11550 {
77f4a2713aSLionel Sambuc   struct S1 {
78f4a2713aSLionel Sambuc     S1();
79f4a2713aSLionel Sambuc   };
80f4a2713aSLionel Sambuc   S1 makeS1();
testS1(S1 a)81f4a2713aSLionel Sambuc   void testS1(S1 a) {
82f4a2713aSLionel Sambuc     // This constructor call can be elided.
83f4a2713aSLionel Sambuc     S1 x = makeS1(); // expected-warning {{unused variable 'x'}}
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc     // This one cannot, so no warning.
86f4a2713aSLionel Sambuc     S1 y;
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc     // This call cannot, but the constructor is trivial.
89f4a2713aSLionel Sambuc     S1 z = a; // expected-warning {{unused variable 'z'}}
90f4a2713aSLionel Sambuc   }
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc   // The same is true even when we know thet constructor has side effects.
93f4a2713aSLionel Sambuc   void foo();
94f4a2713aSLionel Sambuc   struct S2 {
S2PR11550::S295f4a2713aSLionel Sambuc     S2() {
96f4a2713aSLionel Sambuc       foo();
97f4a2713aSLionel Sambuc     }
98f4a2713aSLionel Sambuc   };
99f4a2713aSLionel Sambuc   S2 makeS2();
testS2(S2 a)100f4a2713aSLionel Sambuc   void testS2(S2 a) {
101f4a2713aSLionel Sambuc     S2 x = makeS2(); // expected-warning {{unused variable 'x'}}
102f4a2713aSLionel Sambuc     S2 y;
103f4a2713aSLionel Sambuc     S2 z = a; // expected-warning {{unused variable 'z'}}
104f4a2713aSLionel Sambuc   }
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   // Or when the constructor is not declared by the user.
107f4a2713aSLionel Sambuc   struct S3 {
108f4a2713aSLionel Sambuc     S1 m;
109f4a2713aSLionel Sambuc   };
110f4a2713aSLionel Sambuc   S3 makeS3();
testS3(S3 a)111f4a2713aSLionel Sambuc   void testS3(S3 a) {
112f4a2713aSLionel Sambuc     S3 x = makeS3(); // expected-warning {{unused variable 'x'}}
113f4a2713aSLionel Sambuc     S3 y;
114f4a2713aSLionel Sambuc     S3 z = a; // expected-warning {{unused variable 'z'}}
115f4a2713aSLionel Sambuc   }
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc namespace PR19305 {
119*0a6a1f1dSLionel Sambuc   template<typename T> int n = 0; // no warning
120*0a6a1f1dSLionel Sambuc   int a = n<int>;
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc   template<typename T> const int l = 0; // no warning
123*0a6a1f1dSLionel Sambuc   int b = l<int>;
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc   // PR19558
126*0a6a1f1dSLionel Sambuc   template<typename T> const int o = 0; // no warning
127*0a6a1f1dSLionel Sambuc   template<typename T> const int o<T*> = 0; // no warning
128*0a6a1f1dSLionel Sambuc   int c = o<int*>;
129*0a6a1f1dSLionel Sambuc 
130*0a6a1f1dSLionel Sambuc   template<> int o<void> = 0; // no warning
131*0a6a1f1dSLionel Sambuc   int d = o<void>;
132*0a6a1f1dSLionel Sambuc 
133*0a6a1f1dSLionel Sambuc   // FIXME: It'd be nice to warn here.
134*0a6a1f1dSLionel Sambuc   template<typename T> int m = 0;
135*0a6a1f1dSLionel Sambuc   template<typename T> int m<T*> = 0;
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc   template<> const int m<void> = 0; // expected-warning {{unused variable}}
138*0a6a1f1dSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc 
140f4a2713aSLionel Sambuc namespace ctor_with_cleanups {
141f4a2713aSLionel Sambuc   struct S1 {
142f4a2713aSLionel Sambuc     ~S1();
143f4a2713aSLionel Sambuc   };
144f4a2713aSLionel Sambuc   struct S2 {
145f4a2713aSLionel Sambuc     S2(const S1&);
146f4a2713aSLionel Sambuc   };
func()147f4a2713aSLionel Sambuc   void func() {
148f4a2713aSLionel Sambuc     S2 s((S1()));
149f4a2713aSLionel Sambuc   }
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc #include "Inputs/warn-unused-variables.h"
153