File: trivially-destructible.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 (84 lines) | stat: -rw-r--r-- 3,404 bytes parent folder | download | duplicates (8)
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
// RUN: %check_clang_tidy %s performance-trivially-destructible %t
// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
// RUN: clang-tidy %t.cpp -checks='-*,performance-trivially-destructible' -fix --
// RUN: clang-tidy %t.cpp -checks='-*,performance-trivially-destructible' -warnings-as-errors='-*,performance-trivially-destructible' --

struct TriviallyDestructible1 {
  int a;
};

struct TriviallyDestructible2 : TriviallyDestructible1 {
  ~TriviallyDestructible2() = default;
  TriviallyDestructible1 b;
};

struct NotTriviallyDestructible1 : TriviallyDestructible2 {
  ~NotTriviallyDestructible1();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: class 'NotTriviallyDestructible1' can be made trivially destructible by defaulting the destructor on its first declaration [performance-trivially-destructible]
  // CHECK-FIXES: ~NotTriviallyDestructible1() = default;
  TriviallyDestructible2 b;
};

NotTriviallyDestructible1::~NotTriviallyDestructible1() = default; // to-be-removed
// CHECK-MESSAGES: :[[@LINE-1]]:28: note: destructor definition is here
// CHECK-FIXES: {{^}}// to-be-removed

// Don't emit for class template with type-dependent fields.
template <class T>
struct MaybeTriviallyDestructible1 {
  ~MaybeTriviallyDestructible1() noexcept;
  T t;
};

template <class T>
MaybeTriviallyDestructible1<T>::~MaybeTriviallyDestructible1() noexcept = default;

// Don't emit for specializations.
template struct MaybeTriviallyDestructible1<int>;

// Don't emit for class template with type-dependent bases.
template <class T>
struct MaybeTriviallyDestructible2 : T {
  ~MaybeTriviallyDestructible2() noexcept;
};

template <class T>
MaybeTriviallyDestructible2<T>::~MaybeTriviallyDestructible2() noexcept = default;

// Emit for templates without dependent bases and fields.
template <class T>
struct MaybeTriviallyDestructible1<T *> {
  ~MaybeTriviallyDestructible1() noexcept;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: class 'MaybeTriviallyDestructible1<T *>' can be made trivially destructible by defaulting the destructor on its first declaration [performance-trivially-destructible]
  // CHECK-FIXES: ~MaybeTriviallyDestructible1() noexcept = default;
  TriviallyDestructible1 t;
};

template <class T>
MaybeTriviallyDestructible1<T *>::~MaybeTriviallyDestructible1() noexcept = default; // to-be-removed
// CHECK-MESSAGES: :[[@LINE-1]]:35: note: destructor definition is here
// CHECK-FIXES: {{^}}// to-be-removed

// Emit for explicit specializations.
template <>
struct MaybeTriviallyDestructible1<double>: TriviallyDestructible1 {
  ~MaybeTriviallyDestructible1() noexcept;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: class 'MaybeTriviallyDestructible1<double>' can be made trivially destructible by defaulting the destructor on its first declaration [performance-trivially-destructible]
  // CHECK-FIXES: ~MaybeTriviallyDestructible1() noexcept = default;
};

MaybeTriviallyDestructible1<double>::~MaybeTriviallyDestructible1() noexcept = default; // to-be-removed
// CHECK-MESSAGES: :[[@LINE-1]]:38: note: destructor definition is here
// CHECK-FIXES: {{^}}// to-be-removed

struct NotTriviallyDestructible2 {
  virtual ~NotTriviallyDestructible2();
};

NotTriviallyDestructible2::~NotTriviallyDestructible2() = default;

struct NotTriviallyDestructible3: NotTriviallyDestructible2 {
  ~NotTriviallyDestructible3();
};

NotTriviallyDestructible3::~NotTriviallyDestructible3() = default;