xref: /llvm-project/clang/test/SemaCXX/warn-unused-variables.cpp (revision 2602a683e09c84ec4e28314abb60c6e2a900ef0e)
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