1 // RUN: %check_clang_tidy %s misc-unused-using-decls %t -- --fix-notes -- -fno-delayed-template-parsing -isystem %S/Inputs/
2 
3 // ----- Definitions -----
4 template <typename T> class vector {};
5 namespace n {
6 class A;
7 class B;
8 class C;
9 class D;
10 class D { public: static int i; };
11 template <typename T> class E {};
12 template <typename T> class F {};
func()13 class G { public: static void func() {} };
14 class H { public: static int i; };
15 class I {
16  public:
17   static int ii;
18 };
19 template <typename T> class J {};
20 class G;
21 class H;
22 
23 template <typename T> class K {};
24 template <template <typename> class S>
25 class L {};
26 
27 template <typename T> class M {};
28 class N {};
29 
30 template <int T> class P {};
31 const int Constant = 0;
32 
33 template <typename T> class Q {};
34 
35 class Base {
36  public:
37   void f();
38 };
39 
40 D UsedInstance;
41 D UnusedInstance;
42 
UsedFunc()43 int UsedFunc() { return 1; }
UnusedFunc()44 int UnusedFunc() { return 1; }
UsedTemplateFunc()45 template <typename T> int UsedTemplateFunc() { return 1; }
UnusedTemplateFunc()46 template <typename T> int UnusedTemplateFunc() { return 1; }
UsedInTemplateFunc()47 template <typename T> int UsedInTemplateFunc() { return 1; }
48 void OverloadFunc(int);
49 void OverloadFunc(double);
FuncUsedByUsingDeclInMacro()50 int FuncUsedByUsingDeclInMacro() { return 1; }
51 
52 class ostream {
53 public:
54   ostream &operator<<(ostream &(*PF)(ostream &));
55 };
56 extern ostream cout;
57 ostream &endl(ostream &os);
58 
59 enum Color1 { Green };
60 
61 enum Color2 { Red };
62 
63 enum Color3 { Yellow };
64 
65 enum Color4 { Blue };
66 
67 }  // namespace n
68 
69 #include "unused-using-decls.h"
70 namespace ns {
71 template <typename T>
72 class AA {
73   T t;
74 };
75 template <typename T>
ff()76 T ff() { T t; return t; }
77 } // namespace ns
78 
79 // ----- Using declarations -----
80 // eol-comments aren't removed (yet)
81 using n::A; // A
82 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'A' is unused
83 // CHECK-MESSAGES: :[[@LINE-2]]:10: note: remove the using
84 // CHECK-FIXES: {{^}}// A
85 using n::B;
86 using n::C;
87 using n::D;
88 using n::E; // E
89 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'E' is unused
90 // CHECK-FIXES: {{^}}// E
91 using n::F;
92 using n::G;
93 using n::H;
94 using n::I;
95 int I::ii = 1;
96 class Derived : public n::Base {
97  public:
98   using Base::f;
99 };
100 using n::UsedInstance;
101 using n::UsedFunc;
102 using n::UsedTemplateFunc;
103 using n::UnusedInstance; // UnusedInstance
104 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'UnusedInstance' is unused
105 // CHECK-FIXES: {{^}}// UnusedInstance
106 using n::UnusedFunc; // UnusedFunc
107 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'UnusedFunc' is unused
108 // CHECK-FIXES: {{^}}// UnusedFunc
109 using n::cout;
110 using n::endl;
111 
112 using n::UsedInTemplateFunc;
113 using n::J;
Callee()114 template <typename T> void Callee() {
115   J<T> j;
116   UsedInTemplateFunc<T>();
117 }
118 
119 using n::OverloadFunc; // OverloadFunc
120 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'OverloadFunc' is unused
121 // CHECK-FIXES: {{^}}// OverloadFunc
122 
123 #define DEFINE_INT(name)        \
124   namespace INT {               \
125   static const int _##name = 1; \
126   }                             \
127   using INT::_##name
128 DEFINE_INT(test);
129 #undef DEFIND_INT
130 
131 #define USING_FUNC \
132   using n::FuncUsedByUsingDeclInMacro;
133 USING_FUNC
134 #undef USING_FUNC
135 
136 namespace N1 {
137 // n::G is used in namespace N2.
138 // Currently, the check doesn't support multiple scopes. All the relevant
139 // using-decls will be marked as used once we see an usage even the usage is in
140 // other scope.
141 using n::G;
142 }
143 
144 namespace N2 {
145 using n::G;
146 void f(G g);
147 }
148 
IgnoreFunctionScope()149 void IgnoreFunctionScope() {
150 // Using-decls defined in function scope will be ignored.
151 using n::H;
152 }
153 
154 using n::Color1;
155 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'Color1' is unused
156 using n::Green;
157 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'Green' is unused
158 using n::Color2;
159 using n::Color3;
160 using n::Blue;
161 
162 using ns::AA;
163 using ns::ff;
164 
165 using n::K;
166 
167 using n::N;
168 
169 // FIXME: Currently non-type template arguments are not supported.
170 using n::Constant;
171 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'Constant' is unused
172 
173 using n::Q;
174 
175 // ----- Usages -----
176 void f(B b);
g()177 void g() {
178   vector<C> data;
179   D::i = 1;
180   F<int> f;
181   void (*func)() = &G::func;
182   int *i = &H::i;
183   UsedInstance.i;
184   UsedFunc();
185   UsedTemplateFunc<int>();
186   cout << endl;
187   Color2 color2;
188   int t1 = Color3::Yellow;
189   int t2 = Blue;
190 
191   MyClass a;
192   int t3 = 0;
193   a.func1<AA>(&t3);
194   a.func2<int, ff>(t3);
195 
196   n::L<K> l;
197 }
198 
199 template<class T>
h(n::M<T> * t)200 void h(n::M<T>* t) {}
201 // n::N is used the explicit template instantiation.
202 template void h(n::M<N>* t);
203 
204 // Test on Non-type template arguments.
205 template <int T>
i(n::P<T> * t)206 void i(n::P<T>* t) {}
207 template void i(n::P<Constant>* t);
208 
209 template <typename T, template <typename> class U> class Bar {};
210 // We used to report Q unsued, because we only checked the first template
211 // argument.
212 Bar<int, Q> *bar;
213