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