1// RUN: %check_clang_tidy %s modernize-use-equals-delete %t
2
3struct PositivePrivate {
4private:
5 PositivePrivate();
6 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
7 // CHECK-FIXES: PositivePrivate() = delete;
8 PositivePrivate(const PositivePrivate &);
9 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
10 // CHECK-FIXES: PositivePrivate(const PositivePrivate &) = delete;
11 PositivePrivate &operator=(const PositivePrivate &);
12 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
13 // CHECK-FIXES: PositivePrivate &operator=(const PositivePrivate &) = delete;
14 PositivePrivate(PositivePrivate &&);
15 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
16 // CHECK-FIXES: PositivePrivate(PositivePrivate &&) = delete;
17 PositivePrivate &operator=(PositivePrivate &&);
18 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
19 // CHECK-FIXES: PositivePrivate &operator=(PositivePrivate &&) = delete;
20 ~PositivePrivate();
21 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
22 // CHECK-FIXES: ~PositivePrivate() = delete;
23};
24
25template<typename T>
26struct PositivePrivateTemplate {
27private:
28 PositivePrivateTemplate();
29 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
30 // CHECK-FIXES: PositivePrivateTemplate() = delete;
31 PositivePrivateTemplate(const PositivePrivateTemplate &);
32 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
33 // CHECK-FIXES: PositivePrivateTemplate(const PositivePrivateTemplate &) = delete;
34 PositivePrivateTemplate &operator=(const PositivePrivateTemplate &);
35 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
36 // CHECK-FIXES: PositivePrivateTemplate &operator=(const PositivePrivateTemplate &) = delete;
37 PositivePrivateTemplate(PositivePrivateTemplate &&);
38 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
39 // CHECK-FIXES: PositivePrivateTemplate(PositivePrivateTemplate &&) = delete;
40 PositivePrivateTemplate &operator=(PositivePrivateTemplate &&);
41 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
42 // CHECK-FIXES: PositivePrivateTemplate &operator=(PositivePrivateTemplate &&) = delete;
43 ~PositivePrivateTemplate();
44 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
45 // CHECK-FIXES: ~PositivePrivateTemplate() = delete;
46};
47
48template struct PositivePrivateTemplate<int>;
49template struct PositivePrivateTemplate<char>;
50
51struct NegativePublic {
52 NegativePublic(const NegativePublic &);
53};
54
55struct NegativeProtected {
56protected:
57 NegativeProtected(const NegativeProtected &);
58};
59
60struct PositiveInlineMember {
61 int foo() { return 0; }
62
63private:
64 PositiveInlineMember(const PositiveInlineMember &);
65 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
66 // CHECK-FIXES: PositiveInlineMember(const PositiveInlineMember &) = delete;
67};
68
69struct PositiveOutOfLineMember {
70 int foo();
71
72private:
73 PositiveOutOfLineMember(const PositiveOutOfLineMember &);
74 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
75 // CHECK-FIXES: PositiveOutOfLineMember(const PositiveOutOfLineMember &) = delete;
76};
77
78int PositiveOutOfLineMember::foo() { return 0; }
79
80struct PositiveAbstractMember {
81 virtual int foo() = 0;
82
83private:
84 PositiveAbstractMember(const PositiveAbstractMember &);
85 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
86 // CHECK-FIXES: PositiveAbstractMember(const PositiveAbstractMember &) = delete;
87};
88
89struct NegativeMemberNotImpl {
90 int foo();
91
92private:
93 NegativeMemberNotImpl(const NegativeMemberNotImpl &);
94};
95
96struct NegativeStaticMemberNotImpl {
97 static int foo();
98
99private:
100 NegativeStaticMemberNotImpl(const NegativeStaticMemberNotImpl &);
101};
102
103struct NegativeInline {
104private:
105 NegativeInline(const NegativeInline &) {}
106};
107
108struct NegativeOutOfLine {
109private:
110 NegativeOutOfLine(const NegativeOutOfLine &);
111};
112
113NegativeOutOfLine::NegativeOutOfLine(const NegativeOutOfLine &) {}
114
115struct NegativeConstructNotImpl {
116 NegativeConstructNotImpl();
117
118private:
119 NegativeConstructNotImpl(const NegativeConstructNotImpl &);
120};
121
122struct PositiveDefaultedConstruct {
123 PositiveDefaultedConstruct() = default;
124
125private:
126 PositiveDefaultedConstruct(const PositiveDefaultedConstruct &);
127 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
128 // CHECK-FIXES: PositiveDefaultedConstruct(const PositiveDefaultedConstruct &) = delete;
129};
130
131struct PositiveDeletedConstruct {
132 PositiveDeletedConstruct() = delete;
133
134private:
135 PositiveDeletedConstruct(const PositiveDeletedConstruct &);
136 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= delete' to prohibit calling of a special member function [modernize-use-equals-delete]
137 // CHECK-FIXES: PositiveDeletedConstruct(const PositiveDeletedConstruct &) = delete;
138};
139
140struct NegativeDefaulted {
141private:
142 NegativeDefaulted(const NegativeDefaulted &) = default;
143};
144
145struct PrivateDeleted {
146private:
147 PrivateDeleted(const PrivateDeleted &) = delete;
148 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: deleted member function should be public [modernize-use-equals-delete]
149};
150
151struct ProtectedDeleted {
152protected:
153 ProtectedDeleted(const ProtectedDeleted &) = delete;
154 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: deleted member function should be public [modernize-use-equals-delete]
155};
156
157struct PublicDeleted {
158public:
159 PublicDeleted(const PublicDeleted &) = delete;
160};
161
162#define M1 \
163 struct PrivateDeletedMacro { \
164 private: \
165 PrivateDeletedMacro(const PrivateDeletedMacro &) = delete; \
166 }; \
167 struct ProtectedDeletedMacro { \
168 protected: \
169 ProtectedDeletedMacro(const ProtectedDeletedMacro &) = delete; \
170 }
171
172M1;
173
174#define DISALLOW_COPY_AND_ASSIGN(name) \
175 name(const name &) = delete; \
176 void operator=(const name &) = delete
177
178struct PrivateDeletedMacro2 {
179private:
180 DISALLOW_COPY_AND_ASSIGN(PrivateDeletedMacro2);
181};
182
183struct ProtectedDeletedMacro2 {
184protected:
185 DISALLOW_COPY_AND_ASSIGN(ProtectedDeletedMacro2);
186};
187
188// This resulted in a warning by default.
189#define MACRO(type) void operator=(type const &)
190class C {
191private:
192 MACRO(C);
193};
194
195namespace PR33759 {
196
197 class Number {
198 private:
199 Number();
200 ~Number();
201
202 public:
203 static Number& getNumber() {
204 static Number number;
205 return number;
206 }
207
208 int getIntValue() { return (int)someFloat; }
209 float getFloatValue() { return someFloat; }
210 private:
211 float someFloat;
212 };
213
214 class Number2 {
215 private:
216 Number2();
217 ~Number2();
218 public:
219 static Number& getNumber();
220 };
221}
222

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-delete.cpp