@@ -109,34 +109,6 @@ void CrtpConstructorAccessibilityCheck::check(
109
109
CRTPDeclaration->getBraceRange ().getEnd (),
110
110
" friend " + DerivedTemplateParameter->getNameAsString () + ' ;' + ' \n ' );
111
111
112
- if (!CRTPDeclaration->hasUserDeclaredConstructor ()) {
113
- const bool IsStruct = CRTPDeclaration->isStruct ();
114
-
115
- // FIXME: Clean this up!
116
- {
117
- DiagnosticBuilder Diag =
118
- diag (CRTPDeclaration->getLocation (),
119
- " the implicit default constructor of the CRTP is publicly "
120
- " accessible" )
121
- << CRTPDeclaration
122
- << FixItHint::CreateInsertion (
123
- CRTPDeclaration->getBraceRange ().getBegin ().getLocWithOffset (
124
- 1 ),
125
- (IsStruct ? " \n private:\n " : " \n " ) +
126
- CRTPDeclaration->getNameAsString () + " () = default;\n " +
127
- (IsStruct ? " public:\n " : " " ));
128
-
129
- if (NeedsFriend)
130
- Diag << HintFriend;
131
- }
132
-
133
- diag (CRTPDeclaration->getLocation (),
134
- " consider making it private%select{| and declaring the derived class "
135
- " as friend}0" ,
136
- DiagnosticIDs::Note)
137
- << NeedsFriend;
138
- }
139
-
140
112
if (hasPrivateConstructor (CRTPDeclaration) && NeedsFriend) {
141
113
diag (CRTPDeclaration->getLocation (),
142
114
" the CRTP cannot be constructed from the derived class" )
@@ -145,30 +117,56 @@ void CrtpConstructorAccessibilityCheck::check(
145
117
" consider declaring the derived class as friend" , DiagnosticIDs::Note);
146
118
}
147
119
120
+ auto DiagNoteFriendPrivate = [&](const SourceLocation &Loc, bool Friend) {
121
+ return diag (Loc,
122
+ " consider making it private%select{| and declaring the "
123
+ " derived class "
124
+ " as friend}0" ,
125
+ DiagnosticIDs::Note)
126
+ << Friend;
127
+ };
128
+
129
+ auto WithFriendHintIfNeeded = [&](DiagnosticBuilder Diag, bool NeedsFriend) {
130
+ if (NeedsFriend)
131
+ Diag << HintFriend;
132
+
133
+ return Diag;
134
+ };
135
+
136
+ if (!CRTPDeclaration->hasUserDeclaredConstructor ()) {
137
+ const bool IsStruct = CRTPDeclaration->isStruct ();
138
+
139
+ WithFriendHintIfNeeded (
140
+ diag (CRTPDeclaration->getLocation (),
141
+ " the implicit default constructor of the CRTP is publicly "
142
+ " accessible" )
143
+ << CRTPDeclaration
144
+ << FixItHint::CreateInsertion (
145
+ CRTPDeclaration->getBraceRange ().getBegin ().getLocWithOffset (
146
+ 1 ),
147
+ (IsStruct ? " \n private:\n " : " \n " ) +
148
+ CRTPDeclaration->getNameAsString () + " () = default;\n " +
149
+ (IsStruct ? " public:\n " : " " )),
150
+ NeedsFriend);
151
+
152
+ DiagNoteFriendPrivate (CRTPDeclaration->getLocation (), NeedsFriend);
153
+ }
154
+
148
155
for (auto &&Ctor : CRTPDeclaration->ctors ()) {
149
156
if (Ctor->getAccess () == AS_private)
150
157
continue ;
151
158
152
159
const bool IsPublic = Ctor->getAccess () == AS_public;
153
160
const std::string Access = IsPublic ? " public" : " protected" ;
154
161
155
- // FIXME: Clean this up!
156
- {
157
- DiagnosticBuilder Diag =
158
- diag (Ctor->getLocation (),
159
- " %0 contructor allows the CRTP to be %select{inherited "
160
- " from|constructed}1 as a regular template class" )
161
- << Access << IsPublic << Ctor << hintMakeCtorPrivate (Ctor, Access);
162
-
163
- if (NeedsFriend)
164
- Diag << HintFriend;
165
- }
166
-
167
- diag (Ctor->getLocation (),
168
- " consider making it private%select{| and declaring the derived class "
169
- " as friend}0" ,
170
- DiagnosticIDs::Note)
171
- << NeedsFriend;
162
+ WithFriendHintIfNeeded (
163
+ diag (Ctor->getLocation (),
164
+ " %0 contructor allows the CRTP to be %select{inherited "
165
+ " from|constructed}1 as a regular template class" )
166
+ << Access << IsPublic << Ctor << hintMakeCtorPrivate (Ctor, Access),
167
+ NeedsFriend);
168
+
169
+ DiagNoteFriendPrivate (Ctor->getLocation (), NeedsFriend);
172
170
}
173
171
}
174
172
0 commit comments