misc-unused-parameters.cpp
8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// RUN: echo "static void staticFunctionHeader(int i) {;}" > %T/header.h
// RUN: echo "static void staticFunctionHeader(int /*i*/) {;}" > %T/header-fixed.h
// RUN: %check_clang_tidy -std=c++11 %s misc-unused-parameters %t -- -header-filter='.*' -- -fno-delayed-template-parsing
// RUN: diff %T/header.h %T/header-fixed.h
// FIXME: Make the test work in all language modes.
#include "header.h"
// CHECK-MESSAGES: header.h:1:38: warning
// Basic removal
// =============
void a(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}void a(int /*i*/) {;}{{$}}
void b(int i = 1) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}void b(int /*i*/ = 1) {;}{{$}}
void c(int *i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: parameter 'i' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}void c(int * /*i*/) {;}{{$}}
void d(int i[]) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}void d(int /*i*/[]) {;}{{$}}
void e(int i[1]) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}void e(int /*i*/[1]) {;}{{$}}
void f(void (*fn)()) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: parameter 'fn' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}void f(void (* /*fn*/)()) {;}{{$}}
// Unchanged cases
// ===============
void f(int i); // Don't remove stuff in declarations
void g(int i = 1);
void h(int i[]);
void s(int i[1]);
void u(void (*fn)());
void w(int i) { (void)i; } // Don't remove used parameters
bool useLambda(int (*fn)(int));
static bool static_var = useLambda([] (int a) { return a; });
// Remove parameters of local functions
// ====================================
static void staticFunctionA(int i);
// CHECK-FIXES: {{^}}static void staticFunctionA();
static void staticFunctionA(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning
// CHECK-FIXES: {{^}}static void staticFunctionA()
static void staticFunctionB(int i, int j) { (void)i; }
// CHECK-MESSAGES: :[[@LINE-1]]:40: warning
// CHECK-FIXES: {{^}}static void staticFunctionB(int i)
static void staticFunctionC(int i, int j) { (void)j; }
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning
// CHECK-FIXES: {{^}}static void staticFunctionC(int j)
static void staticFunctionD(int i, int j, int k) { (void)i; (void)k; }
// CHECK-MESSAGES: :[[@LINE-1]]:40: warning
// CHECK-FIXES: {{^}}static void staticFunctionD(int i, int k)
static void staticFunctionE(int i = 4) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning
// CHECK-FIXES: {{^}}static void staticFunctionE()
static void staticFunctionF(int i = 4);
// CHECK-FIXES: {{^}}static void staticFunctionF();
static void staticFunctionF(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning
// CHECK-FIXES: {{^}}static void staticFunctionF()
static void staticFunctionG(int i[]);
// CHECK-FIXES: {{^}}static void staticFunctionG();
static void staticFunctionG(int i[]) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning
// CHECK-FIXES: {{^}}static void staticFunctionG()
static void staticFunctionH(void (*fn)());
// CHECK-FIXES: {{^}}static void staticFunctionH();
static void staticFunctionH(void (*fn)()) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning
// CHECK-FIXES: {{^}}static void staticFunctionH()
static void someCallSites() {
staticFunctionA(1);
// CHECK-FIXES: staticFunctionA();
staticFunctionB(1, 2);
// CHECK-FIXES: staticFunctionB(1);
staticFunctionC(1, 2);
// CHECK-FIXES: staticFunctionC(2);
staticFunctionD(1, 2, 3);
// CHECK-FIXES: staticFunctionD(1, 3);
staticFunctionE(1);
// CHECK-FIXES: staticFunctionE();
staticFunctionF(1);
// CHECK-FIXES: staticFunctionF();
staticFunctionF();
// CHECK-FIXES: staticFunctionF();
int t[] = {1};
staticFunctionG(t);
// CHECK-FIXES: staticFunctionG();
void func();
staticFunctionH(&func);
// CHECK-FIXES: staticFunctionH();
}
/*
* FIXME: This fails because the removals overlap and ClangTidy doesn't apply
* them.
* static void bothVarsUnused(int a, int b) {;}
*/
// Regression test for long variable names and expressions
// =======================================================
static int variableWithLongName1(int LongName1, int LongName2) {
// CHECK-MESSAGES: :[[@LINE-1]]:53: warning: parameter 'LongName2' is unused
// CHECK-FIXES: {{^}}static int variableWithLongName1(int LongName1) {
return LongName1;
}
static int variableWithLongName2(int LongName1, int LongName2) {
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: parameter 'LongName1' is unused
// CHECK-FIXES: {{^}}static int variableWithLongName2(int LongName2) {
return LongName2;
}
static void someLongNameCallSites() {
int LongName1 = 7, LongName2 = 17;
variableWithLongName1(LongName1, LongName2);
// CHECK-FIXES: variableWithLongName1(LongName1);
variableWithLongName2(LongName1, LongName2);
// CHECK-FIXES: variableWithLongName2(LongName2);
}
class SomeClass {
static void f(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning
// CHECK-FIXES: static void f(int /*i*/) {;}
static void g(int i = 1) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning
// CHECK-FIXES: static void g(int /*i*/ = 1) {;}
static void h(int i[]) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning
// CHECK-FIXES: static void h(int /*i*/[]) {;}
static void s(void (*fn)()) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning
// CHECK-FIXES: static void s(void (* /*fn*/)()) {;}
};
namespace {
class C {
public:
void f(int i);
// CHECK-FIXES: void f();
void g(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning
// CHECK-FIXES: void g() {;}
void h(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning
// CHECK-FIXES: void h(int /*i*/) {;}
void s(int i = 1) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning
// CHECK-FIXES: void s(int /*i*/ = 1) {;}
void u(int i[]) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning
// CHECK-FIXES: void u(int /*i*/[]) {;}
void w(void (*fn)()) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning
// CHECK-FIXES: void w(void (* /*fn*/)()) {;}
};
void C::f(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning
// CHECK-FIXES: void C::f() {;}
template <typename T>
void useFunction(T t);
void someMoreCallSites() {
C c;
c.f(1);
// CHECK-FIXES: c.f();
c.g(1);
// CHECK-FIXES: c.g();
useFunction(&C::h);
useFunction(&C::s);
useFunction(&C::u);
useFunction(&C::w);
}
class Base {
virtual void f(int i);
};
class Derived : public Base {
void f(int i) override {;}
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning
// CHECK-FIXES: void f(int /*i*/) override {;}
};
} // end namespace
template <typename T> void someFunctionTemplate(T b, T e) { (void)b; (void)e; }
template <typename T> void someFunctionTemplateOneUnusedParam(T b, T e) { (void)e; }
// CHECK-MESSAGES: :[[@LINE-1]]:65: warning
// CHECK-FIXES: {{^}}template <typename T> void someFunctionTemplateOneUnusedParam(T /*b*/, T e) { (void)e; }
template <typename T> void someFunctionTemplateAllUnusedParams(T b, T e) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:66: warning
// CHECK-MESSAGES: :[[@LINE-2]]:71: warning
// CHECK-FIXES: {{^}}template <typename T> void someFunctionTemplateAllUnusedParams(T /*b*/, T /*e*/) {;}
static void dontGetConfusedByParametersInFunctionTypes() { void (*F)(int i); }
template <typename T> class Function {};
static Function<void(int, int i)> dontGetConfusedByFunctionReturnTypes() {
return Function<void(int, int)>();
}
namespace PR38055 {
namespace {
struct a {
void b(int c) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: parameter 'c' is unused
// CHECK-FIXES: {{^}} void b() {;}{{$}}
};
template <class>
class d {
a e;
void f() { e.b(0); }
};
} // namespace
} // namespace PR38055
namespace strict_mode_off {
// Do not warn on empty function bodies.
void f1(int foo1) {}
void f2(int foo2) {
// "empty" in the AST sense, not in textual sense.
}
void f3(int foo3) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: parameter 'foo3' is unused
// CHECK-FIXES: {{^}}void f3(int /*foo3*/) {;}{{$}}
class E {
int i;
public:
E(int j) {}
};
class F {
int i;
public:
// Constructor initializer counts as a non-empty body.
F(int j) : i() {}
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'j' is unused
// CHECK-FIXES: {{^}} F(int /*j*/) : i() {}{{$}}
};
class A {
public:
A();
A(int);
};
class B : public A {
public:
B(int i) : A() {}
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is unused
// CHECK-FIXES: {{^}} B(int /*i*/) : A() {}{{$}}
};
} // namespace strict_mode_off
namespace lambda {
using fn = void(int);
void f(fn *);
void test() {
// CHECK-MESSAGES: :[[@LINE+2]]:12: warning: parameter 'I' is unused
// CHECK-FIXES: {{^}} f([](int /*I*/) {
f([](int I) { return; });
}
} // namespace lambda