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