aboutsummaryrefslogtreecommitdiff
path: root/clang/test/SemaCXX/warn-unused-private-field.cpp
blob: 0bcca6b82227f9958edf977992fe004dbcabb505 (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// RUN: %clang_cc1 -fsyntax-only -Wunused -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++11 %s
// RUN: %clang_cc1 -fsyntax-only -Wunused -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++17 %s
// RUN: %clang_cc1 -fsyntax-only -Wunused -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++20 %s

#if __cplusplus >= 202002L

class EqDefaultCompare {
  int used;

public:
  EqDefaultCompare(int x) : used(x) {}
  bool operator==(const EqDefaultCompare &) const = default;
};

class SpaceShipDefaultCompare {
  int used;

public:
  SpaceShipDefaultCompare(int x) : used(x) {}
  int operator<=>(const SpaceShipDefaultCompare &) const = default;
};

class EqDefaultCompareOutOfClass {
  int used; // no warning, the compiler generated AST for the comparison operator
            // references the fields of the class, and this should be considered
            // a use.
            // This test case is needed because clang does not emit the body
            // of the defaulted operator when it is defined in-class until it
            // finds a call to it. `-Wunused-private-field` is suppressed in
            // a different way in that case.
  bool operator==(const EqDefaultCompareOutOfClass &) const;
};

bool EqDefaultCompareOutOfClass::operator==(const EqDefaultCompareOutOfClass &) const = default;

class FriendEqDefaultCompareOutOfClass {
  int used; // no warning, same reasoning just tested via a friend declaration.
  friend bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &);
};

bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &) = default;

class HasUnusedField {
  int unused_; // expected-warning{{private field 'unused_' is not used}}
};

class FriendEqDefaultCompare {
  int used;
  friend auto operator==(FriendEqDefaultCompare, FriendEqDefaultCompare) -> bool = default;
};

class UnrelatedFriendEqDefaultCompare {
  friend auto operator==(UnrelatedFriendEqDefaultCompare, UnrelatedFriendEqDefaultCompare) -> bool = default;
  int operator<=>(const UnrelatedFriendEqDefaultCompare &) const = default;
};

#endif

class NotFullyDefined {
 public:
  NotFullyDefined();
 private:
  int y;
};

class HasUndefinedNestedClass {
  class Undefined;
  int unused_;
};

class HasUndefinedPureVirtualDestructor {
  virtual ~HasUndefinedPureVirtualDestructor() = 0;
  int unused_;
};

class HasDefinedNestedClasses {
  class DefinedHere {};
  class DefinedOutside;
  int unused_; // expected-warning{{private field 'unused_' is not used}}
};
class HasDefinedNestedClasses::DefinedOutside {};

class HasUndefinedFriendFunction {
  friend void undefinedFriendFunction();
  int unused_;
};

class HasUndefinedFriendClass {
  friend class NotFullyDefined;
  friend class NotDefined;
  int unused_;
};

class HasFriend {
  friend class FriendClass;
  friend void friendFunction(HasFriend f);
  int unused_; // expected-warning{{private field 'unused_' is not used}}
  int used_by_friend_class_;
  int used_by_friend_function_;
};

class ClassWithTemplateFriend {
  template <typename T> friend class TemplateFriend;
  int used_by_friend_;
  int unused_;
};

template <typename T> class TemplateFriend {
public:
  TemplateFriend(ClassWithTemplateFriend my_friend) {
    int var = my_friend.used_by_friend_; // expected-warning {{unused variable 'var'}}
  }
};

class FriendClass {
  HasFriend my_friend_;
  void use() {
    my_friend_.used_by_friend_class_ = 42;
  }
};

void friendFunction(HasFriend my_friend) {
  my_friend.used_by_friend_function_ = 42;
}

class NonTrivialConstructor {
 public:
  NonTrivialConstructor() {}
};

class NonTrivialDestructor {
 public:
  ~NonTrivialDestructor() {}
};

class Trivial {
 public:
  Trivial() = default;
  Trivial(int a) {}
};

int side_effect() {
  return 42;
}

class A {
 public:
  A() : primitive_type_(42), default_initializer_(), other_initializer_(42),
        trivial_(), user_constructor_(42),
        initialized_with_side_effect_(side_effect()) {
    used_ = 42;
    attr_used_ = 42; // expected-warning{{'attr_used_' was marked unused but was used}}
  }

  A(int x, A* a) : pointer_(a) {}

 private:
  int primitive_type_; // expected-warning{{private field 'primitive_type_' is not used}}
  A* pointer_; // expected-warning{{private field 'pointer_' is not used}}
  int no_initializer_; // expected-warning{{private field 'no_initializer_' is not used}}
  int default_initializer_; // expected-warning{{private field 'default_initializer_' is not used}}
  int other_initializer_; // expected-warning{{private field 'other_initializer_' is not used}}
  int used_, unused_; // expected-warning{{private field 'unused_' is not used}}
  int in_class_initializer_ = 42; // expected-warning{{private field 'in_class_initializer_' is not used}}
  int in_class_initializer_with_side_effect_ = side_effect();
  Trivial trivial_initializer_ = Trivial(); // expected-warning{{private field 'trivial_initializer_' is not used}}
  Trivial non_trivial_initializer_ = Trivial(42);
  int initialized_with_side_effect_;
  static int static_fields_are_ignored_;

  Trivial trivial_; // expected-warning{{private field 'trivial_' is not used}}
  Trivial user_constructor_;
  NonTrivialConstructor non_trivial_constructor_;
  NonTrivialDestructor non_trivial_destructor_;

  int attr_ __attribute__((unused));
  int attr_used_ __attribute__((unused));
};

class EverythingUsed {
 public:
  EverythingUsed() : as_array_index_(0), var_(by_initializer_) {
    var_ = sizeof(sizeof_);
    int *use = &by_reference_; // expected-warning {{unused variable 'use'}}
    int test[2];
    test[as_array_index_] = 42;
    int EverythingUsed::*ptr = &EverythingUsed::by_pointer_to_member_; // expected-warning {{unused variable 'ptr'}}
  }

  template<class T>
  void useStuff(T t) {
    by_template_function_ = 42;
  }

 private:
  int var_;
  int sizeof_;
  int by_reference_;
  int by_template_function_;
  int as_array_index_;
  int by_initializer_;
  int by_pointer_to_member_;
};

class HasFeatureTest {
#if __has_feature(attribute_unused_on_fields)
  int unused_; // expected-warning{{private field 'unused_' is not used}}
  int unused2_ __attribute__((unused)); // no-warning
#endif
};

namespace templates {
class B {
  template <typename T> void f(T t);
  int a;
};
}  // namespace templates

namespace mutual_friends {
// Undefined methods make mutual friends undefined.
class A {
  int a;
  friend class B;
  void doSomethingToAOrB();
};
class B {
  int b;
  friend class A;
};

// Undefined friends do not make a mutual friend undefined.
class C {
  int c;
  void doSomethingElse() {}
  friend class E;
  friend class D;
};
class D {
  int d; // expected-warning{{private field 'd' is not used}}
  friend class C;
};

// Undefined nested classes make mutual friends undefined.
class F {
  int f;
  class G;
  friend class H;
};
class H {
  int h;
  friend class F;
};
}  // namespace mutual_friends

namespace anonymous_structs_unions {
class A {
 private:
  // FIXME: Look at the DeclContext for anonymous structs/unions.
  union {
    int *Aligner;
    unsigned char Data[8];
  };
};
union S {
 private:
  int *Aligner;
  unsigned char Data[8];
};
}  // namespace anonymous_structs_unions

namespace pr13413 {
class A {
  A() : p_(__null), b_(false), a_(this), p2_(nullptr) {}
  void* p_;  // expected-warning{{private field 'p_' is not used}}
  bool b_;  // expected-warning{{private field 'b_' is not used}}
  A* a_;  // expected-warning{{private field 'a_' is not used}}
  void* p2_;  // expected-warning{{private field 'p2_' is not used}}
};
}

namespace pr13543 {
  void f(int);
  void f(char);
  struct S {
    S() : p(&f) {}
  private:
    void (*p)(int); // expected-warning{{private field 'p' is not used}}
  };

  struct A { int n; };
  struct B {
    B() : a(A()) {}
    B(char) {}
    B(int n) : a{n}, b{(f(n), 0)} {}
  private:
    A a = A(); // expected-warning{{private field 'a' is not used}}
    A b;
  };

  struct X { ~X(); };
  class C {
    X x[4]; // no-warning
  };
}

class implicit_special_member {
public:
  static implicit_special_member make() { return implicit_special_member(); }

private:
  int n; // expected-warning{{private field 'n' is not used}}
};

class defaulted_special_member {
public:
  defaulted_special_member(const defaulted_special_member&) = default;

private:
  int n; // expected-warning{{private field 'n' is not used}}
};

namespace pr61334 {
class [[maybe_unused]] MaybeUnusedClass {};
enum [[maybe_unused]] MaybeUnusedEnum {};
typedef int MaybeUnusedTypedef [[maybe_unused]];
class C {
  MaybeUnusedClass c; // no-warning
  MaybeUnusedEnum e; // no-warning
  MaybeUnusedTypedef t; // no-warning
};
}

namespace GH62472 {
class [[gnu::warn_unused]] S {
public:
  S();
};

struct [[maybe_unused]] T {};

void f() {
  int i = 0; // expected-warning {{unused variable 'i'}}
  S s;       // expected-warning {{unused variable 's'}}
  T t;       // ok
}

class C {
private:
  const int i = 0; // expected-warning {{private field 'i' is not used}}
  int j = 0;       // expected-warning {{private field 'j' is not used}}
  const S s1;      // expected-warning {{private field 's1' is not used}}
  const T t1;      // ok
  S s2;            // expected-warning {{private field 's2' is not used}}
  T t2;            // ok
};
}