readability-convert-member-functions-to-static.cpp
5.23 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
// RUN: %check_clang_tidy %s readability-convert-member-functions-to-static %t
class DoNotMakeEmptyStatic {
void emptyMethod() {}
void empty_method_out_of_line();
};
void DoNotMakeEmptyStatic::empty_method_out_of_line() {}
class A {
int field;
const int const_field;
static int static_field;
void no_use() {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'no_use' can be made static
// CHECK-FIXES: {{^}} static void no_use() {
int i = 1;
}
int read_field() {
return field;
}
void write_field() {
field = 1;
}
int call_non_const_member() { return read_field(); }
int call_static_member() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'call_static_member' can be made static
// CHECK-FIXES: {{^}} static int call_static_member() {
already_static();
}
int read_static() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'read_static' can be made static
// CHECK-FIXES: {{^}} static int read_static() {
return static_field;
}
void write_static() {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'write_static' can be made static
// CHECK-FIXES: {{^}} static void write_static() {
static_field = 1;
}
static int already_static() { return static_field; }
int already_const() const { return field; }
int already_const_convert_to_static() const {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'already_const_convert_to_static' can be made static
// CHECK-FIXES: {{^}} static int already_const_convert_to_static() {
return static_field;
}
static int out_of_line_already_static();
void out_of_line_call_static();
// CHECK-FIXES: {{^}} static void out_of_line_call_static();
int out_of_line_const_to_static() const;
// CHECK-FIXES: {{^}} static int out_of_line_const_to_static() ;
};
int A::out_of_line_already_static() { return 0; }
void A::out_of_line_call_static() {
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: method 'out_of_line_call_static' can be made static
// CHECK-FIXES: {{^}}void A::out_of_line_call_static() {
already_static();
}
int A::out_of_line_const_to_static() const {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'out_of_line_const_to_static' can be made static
// CHECK-FIXES: {{^}}int A::out_of_line_const_to_static() {
return 0;
}
struct KeepVirtual {
virtual int f() { return 0; }
virtual int h() const { return 0; }
};
struct KeepVirtualDerived : public KeepVirtual {
int f() { return 0; }
int h() const override { return 0; }
};
// Don't add 'static' to special member functions and operators.
struct KeepSpecial {
KeepSpecial() { int L = 0; }
~KeepSpecial() { int L = 0; }
int operator+() { return 0; }
operator int() { return 0; }
};
void KeepLambdas() {
using FT = int (*)();
auto F = static_cast<FT>([]() { return 0; });
auto F2 = []() { return 0; };
}
template <class Base>
struct KeepWithTemplateBase : public Base {
int i;
// We cannot make these methods static because they might need to override
// a function from Base.
int static_f() { return 0; }
};
template <class T>
struct KeepTemplateClass {
int i;
// We cannot make these methods static because a specialization
// might use *this differently.
int static_f() { return 0; }
};
struct KeepTemplateMethod {
int i;
// We cannot make these methods static because a specialization
// might use *this differently.
template <class T>
static int static_f() { return 0; }
};
void instantiate() {
struct S {};
KeepWithTemplateBase<S> I1;
I1.static_f();
KeepTemplateClass<int> I2;
I2.static_f();
KeepTemplateMethod I3;
I3.static_f<int>();
}
struct Trailing {
auto g() const -> int {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'g' can be made static
// CHECK-FIXES: {{^}} static auto g() -> int {
return 0;
}
void vol() volatile {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'vol' can be made static
return;
}
void ref() const & {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'ref' can be made static
return;
}
void refref() const && {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'refref' can be made static
return;
}
void restr() __restrict {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'restr' can be made static
return;
}
};
struct UnevaluatedContext {
void f() { sizeof(this); }
void noex() noexcept(noexcept(this));
};
struct LambdaCapturesThis {
int Field;
int explicitCapture() {
return [this]() { return Field; }();
}
int implicitCapture() {
return [&]() { return Field; }();
}
};
struct NoFixitInMacro {
#define CONST const
int no_use_macro_const() CONST {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'no_use_macro_const' can be made static
return 0;
}
#define ADD_CONST(F) F const
int ADD_CONST(no_use_macro2()) {
return 0;
}
#define FUN no_use_macro()
int i;
int FUN {
return i;
}
#define T(FunctionName, Keyword) \
Keyword int FunctionName() { return 0; }
#define EMPTY
T(A, EMPTY)
T(B, static)
#define T2(FunctionName) \
int FunctionName() { return 0; }
T2(A2)
#define VOLATILE volatile
void volatileMacro() VOLATILE {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'volatileMacro' can be made static
return;
}
};