readability-make-member-function-const.cpp
8.04 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// RUN: %check_clang_tidy %s readability-make-member-function-const %t
struct Str {
void const_method() const;
void non_const_method();
};
namespace Diagnose {
struct A;
void free_const_use(const A *);
void free_const_use(const A &);
struct A {
int M;
const int ConstM;
struct {
int M;
} Struct;
Str S;
Str &Sref;
void already_const() const;
int read_field() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'read_field' can be made const
// CHECK-FIXES: {{^}} int read_field() const {
return M;
}
int read_struct_field() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'read_struct_field' can be made const
// CHECK-FIXES: {{^}} int read_struct_field() const {
return Struct.M;
}
int read_const_field() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'read_const_field' can be made const
// CHECK-FIXES: {{^}} int read_const_field() const {
return ConstM;
}
void call_const_member() {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'call_const_member' can be made const
// CHECK-FIXES: {{^}} void call_const_member() const {
already_const();
}
void call_const_member_on_public_field() {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'call_const_member_on_public_field' can be made const
// CHECK-FIXES: {{^}} void call_const_member_on_public_field() const {
S.const_method();
}
void call_const_member_on_public_field_ref() {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'call_const_member_on_public_field_ref' can be made const
// CHECK-FIXES: {{^}} void call_const_member_on_public_field_ref() const {
Sref.const_method();
}
const Str &return_public_field_ref() {
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: method 'return_public_field_ref' can be made const
// CHECK-FIXES: {{^}} const Str &return_public_field_ref() const {
return S;
}
const A *return_this_const() {
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: method 'return_this_const' can be made const
// CHECK-FIXES: {{^}} const A *return_this_const() const {
return this;
}
const A &return_this_const_ref() {
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: method 'return_this_const_ref' can be made const
// CHECK-FIXES: {{^}} const A &return_this_const_ref() const {
return *this;
}
void const_use() {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'const_use' can be made const
// CHECK-FIXES: {{^}} void const_use() const {
free_const_use(this);
}
void const_use_ref() {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'const_use_ref' can be made const
// CHECK-FIXES: {{^}} void const_use_ref() const {
free_const_use(*this);
}
auto trailingReturn() -> int {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'trailingReturn' can be made const
// CHECK-FIXES: {{^}} auto trailingReturn() const -> int {
return M;
}
int volatileFunction() volatile {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'volatileFunction' can be made const
// CHECK-FIXES: {{^}} int volatileFunction() const volatile {
return M;
}
int restrictFunction() __restrict {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'restrictFunction' can be made const
// CHECK-FIXES: {{^}} int restrictFunction() const __restrict {
return M;
}
int refFunction() & {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'refFunction' can be made const
// CHECK-FIXES: {{^}} int refFunction() const & {
return M;
}
void out_of_line_call_const();
// CHECK-FIXES: {{^}} void out_of_line_call_const() const;
};
void A::out_of_line_call_const() {
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: method 'out_of_line_call_const' can be made const
// CHECK-FIXES: {{^}}void A::out_of_line_call_const() const {
already_const();
}
} // namespace Diagnose
namespace Keep {
struct Keep;
void free_non_const_use(Keep *);
void free_non_const_use(Keep &);
struct Keep {
private:
void private_const_method() const;
Str PrivateS;
Str *Sptr;
Str &Sref;
public:
int M;
Str S;
void keepTrivial() {}
// See readability-convert-member-functions-to-static instead.
void keepStatic() { int I = 0; }
const int *keepConstCast() const;
int *keepConstCast() { // Needs to stay non-const.
return const_cast<int *>(static_cast<const Keep *>(this)->keepConstCast());
}
void non_const_use() { free_non_const_use(this); }
void non_const_use_ref() { free_non_const_use(*this); }
Keep *return_this() {
return this;
}
Keep &return_this_ref() {
return *this;
}
void escape_this() {
Keep *Escaped = this;
}
void call_private_const_method() {
private_const_method();
}
int keepConst() const { return M; }
virtual int keepVirtual() { return M; }
void writeField() {
M = 1;
}
void callNonConstMember() { writeField(); }
void call_non_const_member_on_field() { S.non_const_method(); }
void call_const_member_on_private_field() {
// Technically, this method could be const-qualified,
// but it might not be logically const.
PrivateS.const_method();
}
const Str &return_private_field_ref() {
// Technically, this method could be const-qualified,
// but it might not be logically const.
return PrivateS;
}
void call_non_const_member_on_pointee() {
Sptr->non_const_method();
}
void call_const_member_on_pointee() {
// Technically, this method could be const-qualified,
// but it might not be logically const.
Sptr->const_method();
}
Str *return_pointer() {
// Technically, this method could be const-qualified,
// but it might not be logically const.
return Sptr;
}
const Str *return_const_pointer() {
// Technically, this method could be const-qualified,
// but it might not be logically const.
return Sptr;
}
void call_non_const_member_on_ref() {
Sref.non_const_method();
}
void escaped_private_field() {
const auto &Escaped = Sref;
}
Str &return_field_ref() {
// Technically, this method could be const-qualified,
// but it might not be logically const.
return Sref;
}
const Str &return_field_const_ref() {
// Technically, this method could be const-qualified,
// but it might not be logically const.
return Sref;
}
};
struct KeepVirtualDerived : public Keep {
int keepVirtual() { return M; }
};
void KeepLambdas() {
auto F = +[]() { return 0; };
auto F2 = []() { return 0; };
}
template <class Base>
struct KeepWithDependentBase : public Base {
int M;
// We cannot make this method const because it might need to override
// a function from Base.
int const_f() { return M; }
};
template <class T>
struct KeepClassTemplate {
int M;
// We cannot make this method const because a specialization
// might use *this differently.
int const_f() { return M; }
};
struct KeepMemberFunctionTemplate {
int M;
// We cannot make this method const because a specialization
// might use *this differently.
template <class T>
int const_f() { return M; }
};
void instantiate() {
struct S {};
KeepWithDependentBase<S> I1;
I1.const_f();
KeepClassTemplate<int> I2;
I2.const_f();
KeepMemberFunctionTemplate I3;
I3.const_f<int>();
}
struct NoFixitInMacro {
int M;
#define FUN const_use_macro()
int FUN {
return M;
}
#define T(FunctionName, Keyword) \
int FunctionName() Keyword { return M; }
#define EMPTY
T(A, EMPTY)
T(B, const)
#define T2(FunctionName) \
int FunctionName() { return M; }
T2(A2)
};
// Real-world code, see clang::ObjCInterfaceDecl.
class DataPattern {
int &data() const;
public:
const int &get() const {
return const_cast<DataPattern *>(this)->get();
}
// This member function must stay non-const, even though
// it only calls other private const member functions.
int &get() {
return data();
}
void set() {
data() = 42;
}
};
struct MemberFunctionPointer {
void call_non_const(void (MemberFunctionPointer::*FP)()) {
(this->*FP)();
}
void call_const(void (MemberFunctionPointer::*FP)() const) {
(this->*FP)();
}
};
} // namespace Keep