File: avoid-const-or-ref-data-members.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (334 lines) | stat: -rw-r--r-- 9,176 bytes parent folder | download | duplicates (3)
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
333
334
// RUN: %check_clang_tidy %s cppcoreguidelines-avoid-const-or-ref-data-members %t
namespace std {
template <typename T>
struct unique_ptr {};

template <typename T>
struct shared_ptr {};
} // namespace std

namespace gsl {
template <typename T>
struct not_null {};
} // namespace gsl

struct Ok {
  int i;
  int *p;
  const int *pc;
  std::unique_ptr<int> up;
  std::shared_ptr<int> sp;
  gsl::not_null<int> n;
};

struct ConstMember {
  const int c;
  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const int' is const qualified [cppcoreguidelines-avoid-const-or-ref-data-members]
};

struct LvalueRefMember {
  int &lr;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'int &' is a reference
};

struct ConstRefMember {
  const int &cr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const int &' is a reference
};

struct RvalueRefMember {
  int &&rr;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'int &&' is a reference
};

struct ConstAndRefMembers {
  const int c;
  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const int' is const qualified
  int &lr;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'int &' is a reference
  const int &cr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const int &' is a reference
  int &&rr;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'int &&' is a reference
};

struct Foo {};

struct Ok2 {
  Foo i;
  Foo *p;
  const Foo *pc;
  std::unique_ptr<Foo> up;
  std::shared_ptr<Foo> sp;
  gsl::not_null<Foo> n;
};

struct ConstMember2 {
  const Foo c;
  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const Foo' is const qualified
};

struct LvalueRefMember2 {
  Foo &lr;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'Foo &' is a reference
};

struct ConstRefMember2 {
  const Foo &cr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const Foo &' is a reference
};

struct RvalueRefMember2 {
  Foo &&rr;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'Foo &&' is a reference
};

struct ConstAndRefMembers2 {
  const Foo c;
  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const Foo' is const qualified
  Foo &lr;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'Foo &' is a reference
  const Foo &cr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const Foo &' is a reference
  Foo &&rr;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'Foo &&' is a reference
};

using ConstType = const int;
using RefType = int &;
using ConstRefType = const int &;
using RefRefType = int &&;

struct WithAlias {
  ConstType c;
  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'ConstType' (aka 'const int') is const qualified
  RefType lr;
  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: member 'lr' of type 'RefType' (aka 'int &') is a reference
  ConstRefType cr;
  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: member 'cr' of type 'ConstRefType' (aka 'const int &') is a reference
  RefRefType rr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'rr' of type 'RefRefType' (aka 'int &&') is a reference
};

template <int N>
using Array = int[N];

struct ConstArrayMember {
  const Array<1> c;
  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: member 'c' of type 'const Array<1>' (aka 'const int[1]') is const qualified
};

struct LvalueRefArrayMember {
  Array<2> &lr;
  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'lr' of type 'Array<2> &' (aka 'int (&)[2]') is a reference
};

struct ConstLvalueRefArrayMember {
  const Array<3> &cr;
  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: member 'cr' of type 'const Array<3> &' (aka 'const int (&)[3]') is a reference
};

struct RvalueRefArrayMember {
  Array<4> &&rr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'rr' of type 'Array<4> &&' (aka 'int (&&)[4]') is a reference
};

template <typename T>
struct TemplatedOk {
  T t;
};

template <typename T>
struct TemplatedConst {
  T t;
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'const int' is const qualified
};

template <typename T>
struct TemplatedConstRef {
  T t;
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'const int &' is a reference
};

template <typename T>
struct TemplatedRefRef {
  T t;
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'int &&' is a reference
};

template <typename T>
struct TemplatedRef {
  T t;
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'int &' is a reference
};

TemplatedOk<int> t1{};
TemplatedConst<const int> t2{123};
TemplatedConstRef<const int &> t3{123};
TemplatedRefRef<int &&> t4{123};
TemplatedRef<int &> t5{t1.t};

// Lambdas capturing const or ref members should not trigger warnings
void lambdas()
{
  int x1{123};
  const int x2{123};
  const int& x3{123};
  int&& x4{123};
  int& x5{x1};

  auto v1 = [x1]{};
  auto v2 = [x2]{};
  auto v3 = [x3]{};
  auto v4 = [x4]{};
  auto v5 = [x5]{};

  auto r1 = [&x1]{};
  auto r2 = [&x2]{};
  auto r3 = [&x3]{};
  auto r4 = [&x4]{};
  auto r5 = [&x5]{};

  auto iv = [=]{
    auto c1 = x1;
    auto c2 = x2;
    auto c3 = x3;
    auto c4 = x4;
    auto c5 = x5;
  };

  auto ir = [&]{
    auto c1 = x1;
    auto c2 = x2;
    auto c3 = x3;
    auto c4 = x4;
    auto c5 = x5;
  };
}

struct NonCopyableWithRef
{
  NonCopyableWithRef(NonCopyableWithRef const&) = delete;
  NonCopyableWithRef& operator=(NonCopyableWithRef const&) = delete;
  NonCopyableWithRef(NonCopyableWithRef&&) = default;
  NonCopyableWithRef& operator=(NonCopyableWithRef&&) = default;

  int& x;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'x' of type 'int &' is a reference
};

struct NonMovableWithRef
{
  NonMovableWithRef(NonMovableWithRef const&) = default;
  NonMovableWithRef& operator=(NonMovableWithRef const&) = default;
  NonMovableWithRef(NonMovableWithRef&&) = delete;
  NonMovableWithRef& operator=(NonMovableWithRef&&) = delete;

  int& x;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'x' of type 'int &' is a reference
};

struct NonCopyableNonMovableWithRef
{
  NonCopyableNonMovableWithRef(NonCopyableNonMovableWithRef const&) = delete;
  NonCopyableNonMovableWithRef(NonCopyableNonMovableWithRef&&) = delete;
  NonCopyableNonMovableWithRef& operator=(NonCopyableNonMovableWithRef const&) = delete;
  NonCopyableNonMovableWithRef& operator=(NonCopyableNonMovableWithRef&&) = delete;

  int& x; // OK, non copyable nor movable
};

struct NonCopyable
{
  NonCopyable(NonCopyable const&) = delete;
  NonCopyable& operator=(NonCopyable const&) = delete;
  NonCopyable(NonCopyable&&) = default;
  NonCopyable& operator=(NonCopyable&&) = default;
};

struct NonMovable
{
  NonMovable(NonMovable const&) = default;
  NonMovable& operator=(NonMovable const&) = default;
  NonMovable(NonMovable&&) = delete;
  NonMovable& operator=(NonMovable&&) = delete;
};

struct NonCopyableNonMovable
{
  NonCopyableNonMovable(NonCopyableNonMovable const&) = delete;
  NonCopyableNonMovable(NonCopyableNonMovable&&) = delete;
  NonCopyableNonMovable& operator=(NonCopyableNonMovable const&) = delete;
  NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete;
};

// Test inheritance
struct InheritFromNonCopyable : NonCopyable
{
  int& x;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'x' of type 'int &' is a reference
};

struct InheritFromNonMovable : NonMovable
{
  int& x;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'x' of type 'int &' is a reference
};

struct InheritFromNonCopyableNonMovable : NonCopyableNonMovable
{
  int& x;  // OK, non copyable nor movable
};

struct InheritBothFromNonCopyableAndNonMovable : NonCopyable, NonMovable
{
  int& x;  // OK, non copyable nor movable
};

// Test composition
struct ContainsNonCopyable
{
  NonCopyable x;
  int& y;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'y' of type 'int &' is a reference
};

struct ContainsNonMovable
{
  NonMovable x;
  int& y;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'y' of type 'int &' is a reference
};

struct ContainsNonCopyableNonMovable
{
  NonCopyableNonMovable x;
  int& y;  // OK, non copyable nor movable
};

struct ContainsBothNonCopyableAndNonMovable
{
  NonCopyable x;
  NonMovable y;
  int& z;  // OK, non copyable nor movable
};

// If copies are deleted and moves are not declared, moves are not implicitly declared,
// so the class is also not movable and we should not warn
struct NonCopyableMovesNotDeclared
{
  NonCopyableMovesNotDeclared(NonCopyableMovesNotDeclared const&) = delete;
  NonCopyableMovesNotDeclared& operator=(NonCopyableMovesNotDeclared const&) = delete;

  int& x;  // OK, non copyable nor movable
};

// If moves are deleted but copies are not declared, copies are implicitly deleted,
// so the class is also not copyable and we should not warn
struct NonMovableCopiesNotDeclared
{
  NonMovableCopiesNotDeclared(NonMovableCopiesNotDeclared&&) = delete;
  NonMovableCopiesNotDeclared& operator=(NonMovableCopiesNotDeclared&&) = delete;

  int& x;  // OK, non copyable nor movable
};