Skip to content

Commit d18a727

Browse files
committed
cleanup
1 parent f830989 commit d18a727

File tree

1 file changed

+43
-45
lines changed

1 file changed

+43
-45
lines changed

clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,6 @@ void CrtpConstructorAccessibilityCheck::check(
109109
CRTPDeclaration->getBraceRange().getEnd(),
110110
"friend " + DerivedTemplateParameter->getNameAsString() + ';' + '\n');
111111

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 ? "\nprivate:\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-
140112
if (hasPrivateConstructor(CRTPDeclaration) && NeedsFriend) {
141113
diag(CRTPDeclaration->getLocation(),
142114
"the CRTP cannot be constructed from the derived class")
@@ -145,30 +117,56 @@ void CrtpConstructorAccessibilityCheck::check(
145117
"consider declaring the derived class as friend", DiagnosticIDs::Note);
146118
}
147119

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 ? "\nprivate:\n" : "\n") +
148+
CRTPDeclaration->getNameAsString() + "() = default;\n" +
149+
(IsStruct ? "public:\n" : "")),
150+
NeedsFriend);
151+
152+
DiagNoteFriendPrivate(CRTPDeclaration->getLocation(), NeedsFriend);
153+
}
154+
148155
for (auto &&Ctor : CRTPDeclaration->ctors()) {
149156
if (Ctor->getAccess() == AS_private)
150157
continue;
151158

152159
const bool IsPublic = Ctor->getAccess() == AS_public;
153160
const std::string Access = IsPublic ? "public" : "protected";
154161

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);
172170
}
173171
}
174172

0 commit comments

Comments
 (0)