Skip to content

Commit 14b9478

Browse files
authored
[clang-tidy][NFC] Refactor bugprone-argument-comment [1/3] (#172521)
This is a necessary step to land #171757 Part of #170921
1 parent b5fd757 commit 14b9478

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,37 +142,37 @@ getCommentsBeforeLoc(ASTContext *Ctx, SourceLocation Loc) {
142142
return Comments;
143143
}
144144

145-
static bool isLikelyTypo(llvm::ArrayRef<ParmVarDecl *> Params,
146-
StringRef ArgName, unsigned ArgIndex) {
145+
template <typename NamedDeclRange>
146+
static bool isLikelyTypo(const NamedDeclRange &Candidates, StringRef ArgName,
147+
StringRef TargetName) {
147148
const std::string ArgNameLowerStr = ArgName.lower();
148149
const StringRef ArgNameLower = ArgNameLowerStr;
149150
// The threshold is arbitrary.
150151
const unsigned UpperBound = ((ArgName.size() + 2) / 3) + 1;
151-
const unsigned ThisED = ArgNameLower.edit_distance(
152-
Params[ArgIndex]->getIdentifier()->getName().lower(),
153-
/*AllowReplacements=*/true, UpperBound);
152+
const unsigned ThisED =
153+
ArgNameLower.edit_distance(TargetName.lower(),
154+
/*AllowReplacements=*/true, UpperBound);
154155
if (ThisED >= UpperBound)
155156
return false;
156157

157-
for (unsigned I = 0, E = Params.size(); I != E; ++I) {
158-
if (I == ArgIndex)
159-
continue;
160-
const IdentifierInfo *II = Params[I]->getIdentifier();
158+
return llvm::all_of(Candidates, [&](const auto &Candidate) {
159+
const IdentifierInfo *II = Candidate->getIdentifier();
161160
if (!II)
162-
continue;
161+
return true;
162+
163+
// Skip the target itself.
164+
if (II->getName() == TargetName)
165+
return true;
163166

164167
const unsigned Threshold = 2;
165-
// Other parameters must be an edit distance at least Threshold more away
166-
// from this parameter. This gives us greater confidence that this is a
167-
// typo of this parameter and not one with a similar name.
168+
// Other candidates must be an edit distance at least Threshold more away
169+
// from this candidate. This gives us greater confidence that this is a
170+
// typo of this candidate and not one with a similar name.
168171
const unsigned OtherED = ArgNameLower.edit_distance(
169172
II->getName().lower(),
170173
/*AllowReplacements=*/true, ThisED + Threshold);
171-
if (OtherED < ThisED + Threshold)
172-
return false;
173-
}
174-
175-
return true;
174+
return OtherED >= ThisED + Threshold;
175+
});
176176
}
177177

178178
static bool sameName(StringRef InComment, StringRef InDecl, bool StrictMode) {
@@ -318,7 +318,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
318318
diag(Comment.first, "argument name '%0' in comment does not "
319319
"match parameter name %1")
320320
<< Matches[2] << II;
321-
if (isLikelyTypo(Callee->parameters(), Matches[2], I)) {
321+
if (isLikelyTypo(Callee->parameters(), Matches[2], II->getName())) {
322322
Diag << FixItHint::CreateReplacement(
323323
Comment.first, (Matches[1] + II->getName() + Matches[3]).str());
324324
}
@@ -334,8 +334,8 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
334334

335335
// If the argument comments are missing for literals add them.
336336
if (Comments.empty() && shouldAddComment(Args[I])) {
337-
const std::string ArgComment =
338-
(llvm::Twine("/*") + II->getName() + "=*/").str();
337+
llvm::SmallString<32> ArgComment;
338+
(llvm::Twine("/*") + II->getName() + "=*/").toStringRef(ArgComment);
339339
const DiagnosticBuilder Diag =
340340
diag(Args[I]->getBeginLoc(),
341341
"argument comment missing for literal argument %0")

0 commit comments

Comments
 (0)