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

source code of clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp