1// RUN: %check_clang_tidy --match-partial-fixes -std=c++11-or-later %s bugprone-incorrect-enable-shared-from-this %t
2
3// NOLINTBEGIN
4namespace std {
5 template <typename T> class enable_shared_from_this {};
6} //namespace std
7// NOLINTEND
8
9class BadClassExample : std::enable_shared_from_this<BadClassExample> {};
10// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'BadClassExample' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
11// CHECK-FIXES: public std::enable_shared_from_this<BadClassExample>
12
13class BadClass2Example : private std::enable_shared_from_this<BadClass2Example> {};
14// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'BadClass2Example' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
15// CHECK-FIXES: public std::enable_shared_from_this<BadClass2Example>
16
17struct BadStructExample : private std::enable_shared_from_this<BadStructExample> {};
18// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 'BadStructExample' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
19// CHECK-FIXES: public std::enable_shared_from_this<BadStructExample>
20
21class GoodClassExample : public std::enable_shared_from_this<GoodClassExample> {};
22
23struct GoodStructExample : public std::enable_shared_from_this<GoodStructExample> {};
24
25struct GoodStruct2Example : std::enable_shared_from_this<GoodStruct2Example> {};
26
27class dummy_class1 {};
28class dummy_class2 {};
29
30class BadMultiClassExample : std::enable_shared_from_this<BadMultiClassExample>, dummy_class1 {};
31// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'BadMultiClassExample' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
32// CHECK-FIXES: public std::enable_shared_from_this<BadMultiClassExample>, dummy_class1
33
34class BadMultiClass2Example : dummy_class1, std::enable_shared_from_this<BadMultiClass2Example>, dummy_class2 {};
35// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'BadMultiClass2Example' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
36// CHECK-FIXES: dummy_class1, public std::enable_shared_from_this<BadMultiClass2Example>, dummy_class2
37
38class BadMultiClass3Example : dummy_class1, dummy_class2, std::enable_shared_from_this<BadMultiClass3Example> {};
39// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'BadMultiClass3Example' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
40// CHECK-FIXES: dummy_class1, dummy_class2, public std::enable_shared_from_this<BadMultiClass3Example>
41
42class ClassBase : public std::enable_shared_from_this<ClassBase> {};
43class PrivateInheritClassBase : private ClassBase {};
44// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'PrivateInheritClassBase' is not publicly inheriting from 'ClassBase' which inherits from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
45
46class DefaultInheritClassBase : ClassBase {};
47// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'DefaultInheritClassBase' is not publicly inheriting from 'ClassBase' which inherits from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
48
49class PublicInheritClassBase : public ClassBase {};
50
51struct StructBase : public std::enable_shared_from_this<StructBase> {};
52struct PrivateInheritStructBase : private StructBase {};
53// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 'PrivateInheritStructBase' is not publicly inheriting from 'StructBase' which inherits from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
54
55struct DefaultInheritStructBase : StructBase {};
56
57struct PublicInheritStructBase : StructBase {};
58
59//alias the template itself
60template <typename T> using esft_template = std::enable_shared_from_this<T>;
61
62class PrivateAliasTemplateClassBase : private esft_template<PrivateAliasTemplateClassBase> {};
63// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'PrivateAliasTemplateClassBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
64// CHECK-FIXES: class PrivateAliasTemplateClassBase : public esft_template<PrivateAliasTemplateClassBase> {};
65
66class DefaultAliasTemplateClassBase : esft_template<DefaultAliasTemplateClassBase> {};
67// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'DefaultAliasTemplateClassBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
68// CHECK-FIXES: class DefaultAliasTemplateClassBase : public esft_template<DefaultAliasTemplateClassBase> {};
69
70class PublicAliasTemplateClassBase : public esft_template<PublicAliasTemplateClassBase> {};
71
72struct PrivateAliasTemplateStructBase : private esft_template<PrivateAliasTemplateStructBase> {};
73// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 'PrivateAliasTemplateStructBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
74// CHECK-FIXES: struct PrivateAliasTemplateStructBase : public esft_template<PrivateAliasTemplateStructBase> {};
75
76struct DefaultAliasTemplateStructBase : esft_template<DefaultAliasTemplateStructBase> {};
77
78struct PublicAliasTemplateStructBase : public esft_template<PublicAliasTemplateStructBase> {};
79
80//alias with specific instance
81using esft = std::enable_shared_from_this<ClassBase>;
82class PrivateAliasClassBase : private esft {};
83// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'PrivateAliasClassBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
84// CHECK-FIXES: class PrivateAliasClassBase : public esft {};
85
86class DefaultAliasClassBase : esft {};
87// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'DefaultAliasClassBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
88// CHECK-FIXES: class DefaultAliasClassBase : public esft {};
89
90class PublicAliasClassBase : public esft {};
91
92struct PrivateAliasStructBase : private esft {};
93// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 'PrivateAliasStructBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
94// CHECK-FIXES: struct PrivateAliasStructBase : public esft {};
95
96struct DefaultAliasStructBase : esft {};
97
98struct PublicAliasStructBase : public esft {};
99
100//we can only typedef a specific instance of the template
101typedef std::enable_shared_from_this<ClassBase> EnableSharedFromThis;
102class PrivateTypedefClassBase : private EnableSharedFromThis {};
103// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'PrivateTypedefClassBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
104// CHECK-FIXES: class PrivateTypedefClassBase : public EnableSharedFromThis {};
105
106class DefaultTypedefClassBase : EnableSharedFromThis {};
107// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'DefaultTypedefClassBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
108// CHECK-FIXES: class DefaultTypedefClassBase : public EnableSharedFromThis {};
109
110class PublicTypedefClassBase : public EnableSharedFromThis {};
111
112struct PrivateTypedefStructBase : private EnableSharedFromThis {};
113// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: 'PrivateTypedefStructBase' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
114// CHECK-FIXES: struct PrivateTypedefStructBase : public EnableSharedFromThis {};
115
116struct DefaultTypedefStructBase : EnableSharedFromThis {};
117
118struct PublicTypedefStructBase : public EnableSharedFromThis {};
119
120#define PRIVATE_ESFT_CLASS(ClassName) \
121 class ClassName: private std::enable_shared_from_this<ClassName> { \
122 };
123
124PRIVATE_ESFT_CLASS(PrivateEsftClass);
125// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 'PrivateEsftClass' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
126
127#define DEFAULT_ESFT_CLASS(ClassName) \
128 class ClassName: std::enable_shared_from_this<ClassName> { \
129 };
130
131DEFAULT_ESFT_CLASS(DefaultEsftClass);
132// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 'DefaultEsftClass' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
133
134#define PUBLIC_ESFT_CLASS(ClassName) \
135 class ClassName: public std::enable_shared_from_this<ClassName> { \
136 };
137
138PUBLIC_ESFT_CLASS(PublicEsftClass);
139
140#define PRIVATE_ESFT_STRUCT(StructName) \
141 struct StructName: private std::enable_shared_from_this<StructName> { \
142 };
143
144PRIVATE_ESFT_STRUCT(PrivateEsftStruct);
145// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 'PrivateEsftStruct' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
146
147#define DEFAULT_ESFT_STRUCT(StructName) \
148 struct StructName: std::enable_shared_from_this<StructName> { \
149 };
150
151DEFAULT_ESFT_STRUCT(DefaultEsftStruct);
152
153#define PUBLIC_ESFT_STRUCT(StructName) \
154 struct StructName: std::enable_shared_from_this<StructName> { \
155 };
156
157PUBLIC_ESFT_STRUCT(PublicEsftStruct);
158
159struct A : std::enable_shared_from_this<A> {};
160#define MACRO_A A
161
162class B : MACRO_A {};
163// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'B' is not publicly inheriting from 'A' which inherits from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
164
165class C : private MACRO_A {};
166// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'C' is not publicly inheriting from 'A' which inherits from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
167
168class D : public MACRO_A {};
169
170#define MACRO_PARAM(CLASS) std::enable_shared_from_this<CLASS>
171
172class E : MACRO_PARAM(E) {};
173// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'E' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
174// CHECK-FIXES: class E : public MACRO_PARAM(E) {};
175
176class F : private MACRO_PARAM(F) {};
177// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'F' is not publicly inheriting from 'std::enable_shared_from_this', which will cause unintended behaviour when using 'shared_from_this'; make the inheritance public [bugprone-incorrect-enable-shared-from-this]
178// CHECK-FIXES: class F : public MACRO_PARAM(F) {};
179
180class G : public MACRO_PARAM(G) {};
181

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-enable-shared-from-this.cpp