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