Skip to content

Commit 385dbc1

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (12/N) (#167129)
1 parent 545c302 commit 385dbc1

21 files changed

+132
-121
lines changed

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ static std::vector<std::pair<SourceLocation, StringRef>>
8181
getCommentsInRange(ASTContext *Ctx, CharSourceRange Range) {
8282
std::vector<std::pair<SourceLocation, StringRef>> Comments;
8383
auto &SM = Ctx->getSourceManager();
84-
std::pair<FileID, unsigned> BeginLoc = SM.getDecomposedLoc(Range.getBegin()),
85-
EndLoc = SM.getDecomposedLoc(Range.getEnd());
84+
const std::pair<FileID, unsigned> BeginLoc =
85+
SM.getDecomposedLoc(Range.getBegin()),
86+
EndLoc =
87+
SM.getDecomposedLoc(Range.getEnd());
8688

8789
if (BeginLoc.first != EndLoc.first)
8890
return Comments;
8991

9092
bool Invalid = false;
91-
StringRef Buffer = SM.getBufferData(BeginLoc.first, &Invalid);
93+
const StringRef Buffer = SM.getBufferData(BeginLoc.first, &Invalid);
9294
if (Invalid)
9395
return Comments;
9496

@@ -106,7 +108,7 @@ getCommentsInRange(ASTContext *Ctx, CharSourceRange Range) {
106108
break;
107109

108110
if (Tok.is(tok::comment)) {
109-
std::pair<FileID, unsigned> CommentLoc =
111+
const std::pair<FileID, unsigned> CommentLoc =
110112
SM.getDecomposedLoc(Tok.getLocation());
111113
assert(CommentLoc.first == BeginLoc.first);
112114
Comments.emplace_back(
@@ -125,7 +127,7 @@ static std::vector<std::pair<SourceLocation, StringRef>>
125127
getCommentsBeforeLoc(ASTContext *Ctx, SourceLocation Loc) {
126128
std::vector<std::pair<SourceLocation, StringRef>> Comments;
127129
while (Loc.isValid()) {
128-
clang::Token Tok = utils::lexer::getPreviousToken(
130+
const clang::Token Tok = utils::lexer::getPreviousToken(
129131
Loc, Ctx->getSourceManager(), Ctx->getLangOpts(),
130132
/*SkipComments=*/false);
131133
if (Tok.isNot(tok::comment))
@@ -142,11 +144,11 @@ getCommentsBeforeLoc(ASTContext *Ctx, SourceLocation Loc) {
142144

143145
static bool isLikelyTypo(llvm::ArrayRef<ParmVarDecl *> Params,
144146
StringRef ArgName, unsigned ArgIndex) {
145-
std::string ArgNameLowerStr = ArgName.lower();
146-
StringRef ArgNameLower = ArgNameLowerStr;
147+
const std::string ArgNameLowerStr = ArgName.lower();
148+
const StringRef ArgNameLower = ArgNameLowerStr;
147149
// The threshold is arbitrary.
148-
unsigned UpperBound = ((ArgName.size() + 2) / 3) + 1;
149-
unsigned ThisED = ArgNameLower.edit_distance(
150+
const unsigned UpperBound = ((ArgName.size() + 2) / 3) + 1;
151+
const unsigned ThisED = ArgNameLower.edit_distance(
150152
Params[ArgIndex]->getIdentifier()->getName().lower(),
151153
/*AllowReplacements=*/true, UpperBound);
152154
if (ThisED >= UpperBound)
@@ -155,17 +157,17 @@ static bool isLikelyTypo(llvm::ArrayRef<ParmVarDecl *> Params,
155157
for (unsigned I = 0, E = Params.size(); I != E; ++I) {
156158
if (I == ArgIndex)
157159
continue;
158-
IdentifierInfo *II = Params[I]->getIdentifier();
160+
const IdentifierInfo *II = Params[I]->getIdentifier();
159161
if (!II)
160162
continue;
161163

162164
const unsigned Threshold = 2;
163165
// Other parameters must be an edit distance at least Threshold more away
164166
// from this parameter. This gives us greater confidence that this is a
165167
// typo of this parameter and not one with a similar name.
166-
unsigned OtherED = ArgNameLower.edit_distance(II->getName().lower(),
167-
/*AllowReplacements=*/true,
168-
ThisED + Threshold);
168+
const unsigned OtherED = ArgNameLower.edit_distance(
169+
II->getName().lower(),
170+
/*AllowReplacements=*/true, ThisED + Threshold);
169171
if (OtherED < ThisED + Threshold)
170172
return false;
171173
}
@@ -267,7 +269,8 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
267269
return;
268270

269271
Callee = Callee->getFirstDecl();
270-
unsigned NumArgs = std::min<unsigned>(Args.size(), Callee->getNumParams());
272+
const unsigned NumArgs =
273+
std::min<unsigned>(Args.size(), Callee->getNumParams());
271274
if ((NumArgs == 0) || (IgnoreSingleArgument && NumArgs == 1))
272275
return;
273276

@@ -279,7 +282,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
279282

280283
for (unsigned I = 0; I < NumArgs; ++I) {
281284
const ParmVarDecl *PVD = Callee->getParamDecl(I);
282-
IdentifierInfo *II = PVD->getIdentifier();
285+
const IdentifierInfo *II = PVD->getIdentifier();
283286
if (!II)
284287
continue;
285288
if (FunctionDecl *Template = Callee->getTemplateInstantiationPattern()) {
@@ -293,7 +296,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
293296
}
294297
}
295298

296-
CharSourceRange BeforeArgument =
299+
const CharSourceRange BeforeArgument =
297300
MakeFileCharRange(ArgBeginLoc, Args[I]->getBeginLoc());
298301
ArgBeginLoc = Args[I]->getEndLoc();
299302

@@ -302,7 +305,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
302305
Comments = getCommentsInRange(Ctx, BeforeArgument);
303306
} else {
304307
// Fall back to parsing back from the start of the argument.
305-
CharSourceRange ArgsRange =
308+
const CharSourceRange ArgsRange =
306309
MakeFileCharRange(Args[I]->getBeginLoc(), Args[I]->getEndLoc());
307310
Comments = getCommentsBeforeLoc(Ctx, ArgsRange.getBegin());
308311
}
@@ -312,7 +315,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
312315
if (IdentRE.match(Comment.second, &Matches) &&
313316
!sameName(Matches[2], II->getName(), StrictMode)) {
314317
{
315-
DiagnosticBuilder Diag =
318+
const DiagnosticBuilder Diag =
316319
diag(Comment.first, "argument name '%0' in comment does not "
317320
"match parameter name %1")
318321
<< Matches[2] << II;
@@ -332,9 +335,9 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
332335

333336
// If the argument comments are missing for literals add them.
334337
if (Comments.empty() && shouldAddComment(Args[I])) {
335-
std::string ArgComment =
338+
const std::string ArgComment =
336339
(llvm::Twine("/*") + II->getName() + "=*/").str();
337-
DiagnosticBuilder Diag =
340+
const DiagnosticBuilder Diag =
338341
diag(Args[I]->getBeginLoc(),
339342
"argument comment missing for literal argument %0")
340343
<< II

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, CheckFunctionCalls,
2929
const Expr *E = &Node;
3030

3131
if (const auto *Op = dyn_cast<UnaryOperator>(E)) {
32-
UnaryOperator::Opcode OC = Op->getOpcode();
32+
const UnaryOperator::Opcode OC = Op->getOpcode();
3333
return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
3434
OC == UO_PreDec;
3535
}
@@ -44,7 +44,7 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, CheckFunctionCalls,
4444
if (MethodDecl->isConst())
4545
return false;
4646

47-
OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
47+
const OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
4848
return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
4949
OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
5050
OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
@@ -130,7 +130,7 @@ void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
130130

131131
StringRef AssertMacroName;
132132
while (Loc.isValid() && Loc.isMacroID()) {
133-
StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
133+
const StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
134134
Loc = SM.getImmediateMacroCallerLoc(Loc);
135135

136136
// Check if this macro is an assert.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void AssignmentInIfConditionCheck::check(
6666
}
6767

6868
void AssignmentInIfConditionCheck::report(const Expr *AssignmentExpr) {
69-
SourceLocation OpLoc =
69+
const SourceLocation OpLoc =
7070
isa<BinaryOperator>(AssignmentExpr)
7171
? cast<BinaryOperator>(AssignmentExpr)->getOperatorLoc()
7272
: cast<CXXOperatorCallExpr>(AssignmentExpr)->getOperatorLoc();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
4040
const Token &T = MI->tokens().back();
4141
if (!T.isLiteral() || !T.getLiteralData())
4242
return std::nullopt;
43-
StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
43+
const StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
4444

4545
llvm::APInt IntValue;
4646
constexpr unsigned AutoSenseRadix = 0;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
281281
const auto *IntLit1 = cast<IntegerLiteral>(Stmt1);
282282
const auto *IntLit2 = cast<IntegerLiteral>(Stmt2);
283283

284-
llvm::APInt I1 = IntLit1->getValue();
285-
llvm::APInt I2 = IntLit2->getValue();
284+
const llvm::APInt I1 = IntLit1->getValue();
285+
const llvm::APInt I2 = IntLit2->getValue();
286286
if (I1.getBitWidth() != I2.getBitWidth())
287287
return false;
288288
return I1 == I2;
@@ -352,7 +352,7 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
352352
}
353353
}
354354

355-
size_t N = Branches.size();
355+
const size_t N = Branches.size();
356356
llvm::BitVector KnownAsClone(N);
357357

358358
for (size_t I = 0; I + 1 < N; I++) {
@@ -375,7 +375,7 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
375375
// We report the first occurrence only when we find the second one.
376376
diag(Branches[I]->getBeginLoc(),
377377
"repeated branch body in conditional chain");
378-
SourceLocation End =
378+
const SourceLocation End =
379379
Lexer::getLocForEndOfToken(Branches[I]->getEndLoc(), 0,
380380
*Result.SourceManager, getLangOpts());
381381
if (End.isValid()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void CopyConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
3131

3232
void CopyConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
3333
const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
34-
std::string ParamName = Ctor->getParamDecl(0)->getNameAsString();
34+
const std::string ParamName = Ctor->getParamDecl(0)->getNameAsString();
3535

3636
// We want only one warning (and FixIt) for each ctor.
3737
std::string FixItInitList;
@@ -40,7 +40,7 @@ void CopyConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
4040
bool HasWrittenInitializer = false;
4141
SmallVector<FixItHint, 2> SafeFixIts;
4242
for (const auto *Init : Ctor->inits()) {
43-
bool CtorInitIsWritten = Init->isWritten();
43+
const bool CtorInitIsWritten = Init->isWritten();
4444
HasWrittenInitializer = HasWrittenInitializer || CtorInitIsWritten;
4545
if (!Init->isBaseInitializer())
4646
continue;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ void CrtpConstructorAccessibilityCheck::check(
116116
assert(DerivedTemplateParameter &&
117117
"No template parameter corresponds to the derived class of the CRTP.");
118118

119-
bool NeedsFriend = !isDerivedParameterBefriended(CRTPDeclaration,
120-
DerivedTemplateParameter) &&
121-
!isDerivedClassBefriended(CRTPDeclaration, DerivedRecord);
119+
const bool NeedsFriend =
120+
!isDerivedParameterBefriended(CRTPDeclaration,
121+
DerivedTemplateParameter) &&
122+
!isDerivedClassBefriended(CRTPDeclaration, DerivedRecord);
122123

123124
const FixItHint HintFriend = FixItHint::CreateInsertion(
124125
CRTPDeclaration->getBraceRange().getEnd(),

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void DefaultOperatorNewOnOveralignedTypeCheck::check(
2626
// Get the found 'new' expression.
2727
const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>("new");
2828

29-
QualType T = NewExpr->getAllocatedType();
29+
const QualType T = NewExpr->getAllocatedType();
3030
// Dependent types do not have fixed alignment.
3131
if (T->isDependentType())
3232
return;
@@ -35,25 +35,25 @@ void DefaultOperatorNewOnOveralignedTypeCheck::check(
3535
if (!D || !D->isCompleteDefinition())
3636
return;
3737

38-
ASTContext &Context = D->getASTContext();
38+
const ASTContext &Context = D->getASTContext();
3939

4040
// Check if no alignment was specified for the type.
4141
if (!Context.isAlignmentRequired(T))
4242
return;
4343

4444
// The user-specified alignment (in bits).
45-
unsigned SpecifiedAlignment = D->getMaxAlignment();
45+
const unsigned SpecifiedAlignment = D->getMaxAlignment();
4646
// Double-check if no alignment was specified.
4747
if (!SpecifiedAlignment)
4848
return;
4949
// The alignment used by default 'operator new' (in bits).
50-
unsigned DefaultNewAlignment = Context.getTargetInfo().getNewAlign();
50+
const unsigned DefaultNewAlignment = Context.getTargetInfo().getNewAlign();
5151

52-
bool OverAligned = SpecifiedAlignment > DefaultNewAlignment;
53-
bool HasDefaultOperatorNew =
52+
const bool OverAligned = SpecifiedAlignment > DefaultNewAlignment;
53+
const bool HasDefaultOperatorNew =
5454
!NewExpr->getOperatorNew() || NewExpr->getOperatorNew()->isImplicit();
5555

56-
unsigned CharWidth = Context.getTargetInfo().getCharWidth();
56+
const unsigned CharWidth = Context.getTargetInfo().getCharWidth();
5757
if (HasDefaultOperatorNew && OverAligned)
5858
diag(NewExpr->getBeginLoc(),
5959
"allocation function returns a pointer with alignment %0 but the "

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ AST_MATCHER(CXXMethodDecl, nameCollidesWithMethodInBase) {
6565

6666
for (const auto &BaseMethod : CurrentRecord->methods()) {
6767
if (namesCollide(*BaseMethod, Node)) {
68-
ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
68+
const ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
6969
Builder->setBinding("base_method",
7070
clang::DynTypedNode::create(*BaseMethod));
7171
return true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void DynamicStaticInitializersCheck::registerMatchers(MatchFinder *Finder) {
4343
void DynamicStaticInitializersCheck::check(
4444
const MatchFinder::MatchResult &Result) {
4545
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
46-
SourceLocation Loc = Var->getLocation();
46+
const SourceLocation Loc = Var->getLocation();
4747
if (!Loc.isValid() || !utils::isPresumedLocInHeaderFile(
4848
Loc, *Result.SourceManager, HeaderFileExtensions))
4949
return;

0 commit comments

Comments
 (0)