xref: /llvm-project/clang/test/SemaCXX/warn-unused-variables.cpp (revision 7c23b0891bc7ed73ad478fd34f9c3468ad25b59f)
1 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -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 namespace PR6948 {
45   template<typename T> class X;
46 
47   void f() {
48     X<char> str (read_from_file()); // expected-error{{use of undeclared identifier 'read_from_file'}}
49   }
50 }
51 
52 void unused_local_static() {
53   static int x = 0;
54   static int y = 0; // expected-warning{{unused variable 'y'}}
55 #pragma unused(x)
56 }
57 
58 // PR10168
59 namespace PR10168 {
60   // We expect a warning in the definition only for non-dependent variables, and
61   // a warning in the instantiation only for dependent variables.
62   template<typename T>
63   struct S {
64     void f() {
65       int a; // expected-warning {{unused variable 'a'}}
66       T b; // expected-warning 2{{unused variable 'b'}}
67     }
68   };
69 
70   template<typename T>
71   void f() {
72     int a; // expected-warning {{unused variable 'a'}}
73     T b; // expected-warning 2{{unused variable 'b'}}
74   }
75 
76   void g() {
77     S<int>().f(); // expected-note {{here}}
78     S<char>().f(); // expected-note {{here}}
79     f<int>(); // expected-note {{here}}
80     f<char>(); // expected-note {{here}}
81   }
82 }
83 
84 namespace PR11550 {
85   struct S1 {
86     S1();
87   };
88   S1 makeS1();
89   void testS1(S1 a) {
90     // This constructor call can be elided.
91     S1 x = makeS1(); // expected-warning {{unused variable 'x'}}
92 
93     // This one cannot, so no warning.
94     S1 y;
95 
96     // This call cannot, but the constructor is trivial.
97     S1 z = a; // expected-warning {{unused variable 'z'}}
98   }
99 
100   // The same is true even when we know thet constructor has side effects.
101   void foo();
102   struct S2 {
103     S2() {
104       foo();
105     }
106   };
107   S2 makeS2();
108   void testS2(S2 a) {
109     S2 x = makeS2(); // expected-warning {{unused variable 'x'}}
110     S2 y;
111     S2 z = a; // expected-warning {{unused variable 'z'}}
112   }
113 
114   // Or when the constructor is not declared by the user.
115   struct S3 {
116     S1 m;
117   };
118   S3 makeS3();
119   void testS3(S3 a) {
120     S3 x = makeS3(); // expected-warning {{unused variable 'x'}}
121     S3 y;
122     S3 z = a; // expected-warning {{unused variable 'z'}}
123   }
124 }
125