summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/modernize-use-default.cpp
blob: 504a605e7e62c833125bd485fb8bd818abc6f49b (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
// RUN: %check_clang_tidy %s modernize-use-default %t -- -std=c++11 -fno-delayed-template-parsing

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

A::A() {}
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial default constructor [modernize-use-default]
// CHECK-FIXES: A::A() = default;
A::~A() {}
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial destructor [modernize-use-default]
// CHECK-FIXES: A::~A() = default;

// Inline definitions.
class B {
public:
  B() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: B() = default;
  ~B() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: ~B() = default;
};

void f();

class C {
public:
  // Non-empty constructor body.
  C() { f(); }
  // Non-empty destructor body.
  ~C() { f(); }
};

class D {
public:
  // Constructor with initializer.
  D() : Field(5) {}
  // Constructor with arguments.
  D(int Arg1, int Arg2) {}
  int Field;
};

// Private constructor/destructor.
class E {
  E() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: E() = default;
  ~E() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: ~E() = default;
};

// struct.
struct F {
  F() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: F() = default;
  ~F() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: F() = default;
};

// Deleted constructor/destructor.
class G {
public:
  G() = delete;
  ~G() = delete;
};

// Do not remove other keywords.
class H {
public:
  explicit H() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: explicit H() = default;
  virtual ~H() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: virtual ~H() = default;
};

// Nested class.
struct I {
  struct II {
    II() {}
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use '= default'
    // CHECK-FIXES: II() = default;
    ~II() {}
    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use '= default'
    // CHECK-FIXES: ~II() = default;
  };
  int Int;
};

// Class template.
template <class T>
class J {
public:
  J() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: J() = default;
  ~J() {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: ~J() = default;
};

// Non user-provided constructor/destructor.
struct K {
  int Int;
};
void g() {
  K *PtrK = new K();
  PtrK->~K();
  delete PtrK;
}

// Already using default.
struct L {
  L() = default;
  ~L() = default;
};
struct M {
  M();
  ~M();
};
M::M() = default;
M::~M() = default;

// Delegating constructor and overriden destructor.
struct N : H {
  N() : H() {}
  ~N() override {}
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
  // CHECK-FIXES: ~N() override = default;
};
OpenPOWER on IntegriCloud