Skip to content

Commit 1b861cb

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (5/N) (#167047)
1 parent 0c18999 commit 1b861cb

21 files changed

+65
-62
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void InitVariablesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
3737
}
3838

3939
void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
40-
std::string BadDecl = "badDecl";
40+
const std::string BadDecl = "badDecl";
4141
Finder->addMatcher(
4242
varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
4343
isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
@@ -82,7 +82,7 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
8282
if (MatchedDecl->getEndLoc().isMacroID())
8383
return;
8484

85-
QualType TypePtr = MatchedDecl->getType();
85+
const QualType TypePtr = MatchedDecl->getType();
8686
std::optional<const char *> InitializationString;
8787
bool AddMathInclude = false;
8888

clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MacroUsageCallbacks : public PPCallbacks {
4747
SM.isWrittenInCommandLineFile(MD->getLocation()))
4848
return;
4949

50-
StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
50+
const StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
5151
if (MacroName == "__GCC_HAVE_DWARF2_CFI_ASM")
5252
return;
5353
if (!CheckCapsOnly && !RegExp.match(MacroName))

clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ void MisleadingCaptureDefaultByValueCheck::check(
8181
return;
8282

8383
if (Lambda->getCaptureDefault() == LCD_ByCopy) {
84-
bool IsThisImplicitlyCaptured = std::any_of(
84+
const bool IsThisImplicitlyCaptured = std::any_of(
8585
Lambda->implicit_capture_begin(), Lambda->implicit_capture_end(),
8686
[](const LambdaCapture &Capture) { return Capture.capturesThis(); });
8787
auto Diag = diag(Lambda->getCaptureDefaultLoc(),
8888
"lambdas that %select{|implicitly }0capture 'this' "
8989
"should not specify a by-value capture default")
9090
<< IsThisImplicitlyCaptured;
9191

92-
std::string ReplacementText = createReplacementText(Lambda);
93-
SourceLocation DefaultCaptureEnd =
92+
const std::string ReplacementText = createReplacementText(Lambda);
93+
const SourceLocation DefaultCaptureEnd =
9494
findDefaultCaptureEnd(Lambda, *Result.Context);
9595
Diag << FixItHint::CreateReplacement(
9696
CharSourceRange::getCharRange(Lambda->getCaptureDefaultLoc(),

clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ AST_MATCHER_P(QualType, possiblyPackExpansionOf,
2626
}
2727

2828
AST_MATCHER(ParmVarDecl, isTemplateTypeParameter) {
29-
ast_matchers::internal::Matcher<QualType> Inner = possiblyPackExpansionOf(
30-
qualType(rValueReferenceType(),
31-
references(templateTypeParmType(
32-
hasDeclaration(templateTypeParmDecl()))),
33-
unless(references(qualType(isConstQualified())))));
29+
const ast_matchers::internal::Matcher<QualType> Inner =
30+
possiblyPackExpansionOf(
31+
qualType(rValueReferenceType(),
32+
references(templateTypeParmType(
33+
hasDeclaration(templateTypeParmDecl()))),
34+
unless(references(qualType(isConstQualified())))));
3435
if (!Inner.matches(Node.getType(), Finder, Builder))
3536
return false;
3637

@@ -43,7 +44,7 @@ AST_MATCHER(ParmVarDecl, isTemplateTypeParameter) {
4344
if (!FuncTemplate)
4445
return false;
4546

46-
QualType ParamType =
47+
const QualType ParamType =
4748
Node.getType().getNonPackExpansionType()->getPointeeType();
4849
const auto *TemplateType = ParamType->getAsCanonical<TemplateTypeParmType>();
4950
if (!TemplateType)
@@ -54,10 +55,10 @@ AST_MATCHER(ParmVarDecl, isTemplateTypeParameter) {
5455
}
5556

5657
AST_MATCHER_P(NamedDecl, hasSameNameAsBoundNode, std::string, BindingID) {
57-
IdentifierInfo *II = Node.getIdentifier();
58+
const IdentifierInfo *II = Node.getIdentifier();
5859
if (nullptr == II)
5960
return false;
60-
StringRef Name = II->getName();
61+
const StringRef Name = II->getName();
6162

6263
return Builder->removeBindings(
6364
[this, Name](const ast_matchers::internal::BoundNodesMap &Nodes) {

clang-tools-extra/clang-tidy/cppcoreguidelines/NoSuspendWithLockCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void NoSuspendWithLockCheck::registerMatchers(MatchFinder *Finder) {
2727
hasDeclaration(namedDecl(matchers::matchesAnyListedName(
2828
utils::options::parseStringList(LockGuards)))));
2929

30-
StatementMatcher Lock =
30+
const StatementMatcher Lock =
3131
declStmt(has(varDecl(hasType(LockType)).bind("lock-decl")))
3232
.bind("lock-decl-stmt");
3333
Finder->addMatcher(
@@ -55,12 +55,12 @@ void NoSuspendWithLockCheck::check(const MatchFinder::MatchResult &Result) {
5555
Options.AddImplicitDtors = true;
5656
Options.AddTemporaryDtors = true;
5757

58-
std::unique_ptr<CFG> TheCFG = CFG::buildCFG(
58+
const std::unique_ptr<CFG> TheCFG = CFG::buildCFG(
5959
nullptr, const_cast<clang::CompoundStmt *>(Block), &Context, Options);
6060
if (!TheCFG)
6161
return;
6262

63-
utils::ExprSequence Sequence(TheCFG.get(), Block, &Context);
63+
const utils::ExprSequence Sequence(TheCFG.get(), Block, &Context);
6464
const Stmt *LastBlockStmt = Block->body_back();
6565
if (Sequence.inSequence(LockStmt, Suspend) &&
6666
(Suspend == LastBlockStmt ||

clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ void PreferMemberInitializerCheck::check(
164164
llvm::DenseMap<const FieldDecl *, AssignedLevel> AssignedFields{};
165165

166166
for (const CXXCtorInitializer *Init : Ctor->inits())
167-
if (FieldDecl *Field = Init->getMember())
167+
if (const FieldDecl *Field = Init->getMember())
168168
updateAssignmentLevel(Field, Init->getInit(), Ctor, AssignedFields);
169169

170170
for (const Stmt *S : Body->body()) {
171171
if (S->getBeginLoc().isMacroID()) {
172-
StringRef MacroName = Lexer::getImmediateMacroName(
172+
const StringRef MacroName = Lexer::getImmediateMacroName(
173173
S->getBeginLoc(), *Result.SourceManager, getLangOpts());
174174
if (MacroName.contains_insensitive("assert"))
175175
return;
@@ -206,7 +206,7 @@ void PreferMemberInitializerCheck::check(
206206
bool AddComma = false;
207207
bool AddBrace = false;
208208
bool InvalidFix = false;
209-
unsigned Index = Field->getFieldIndex();
209+
const unsigned Index = Field->getFieldIndex();
210210
const CXXCtorInitializer *LastInListInit = nullptr;
211211
for (const CXXCtorInitializer *Init : Ctor->inits()) {
212212
if (!Init->isWritten() || Init->isInClassMemberInitializer())
@@ -276,7 +276,7 @@ void PreferMemberInitializerCheck::check(
276276
<< Field;
277277
if (InvalidFix)
278278
continue;
279-
StringRef NewInit = Lexer::getSourceText(
279+
const StringRef NewInit = Lexer::getSourceText(
280280
Result.SourceManager->getExpansionRange(InitValue->getSourceRange()),
281281
*Result.SourceManager, getLangOpts());
282282
if (HasInitAlready) {
@@ -288,8 +288,8 @@ void PreferMemberInitializerCheck::check(
288288
else
289289
Diag << FixItHint::CreateReplacement(ReplaceRange, NewInit);
290290
} else {
291-
SmallString<128> Insertion({InsertPrefix, Field->getName(), "(", NewInit,
292-
AddComma ? "), " : ")"});
291+
const SmallString<128> Insertion({InsertPrefix, Field->getName(), "(",
292+
NewInit, AddComma ? "), " : ")"});
293293
Diag << FixItHint::CreateInsertion(InsertPos, Insertion,
294294
FirstToCtorInits);
295295
FirstToCtorInits = areDiagsSelfContained();

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ AST_MATCHER_P(Expr, hasParentIgnoringImpCasts,
3535
ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
3636
const Expr *E = &Node;
3737
do {
38-
DynTypedNodeList Parents = Finder->getASTContext().getParents(*E);
38+
const DynTypedNodeList Parents = Finder->getASTContext().getParents(*E);
3939
if (Parents.size() != 1)
4040
return false;
4141
E = Parents[0].get<Expr>();

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void ProBoundsConstantArrayIndexCheck::check(
7878
else
7979
BaseRange =
8080
cast<CXXOperatorCallExpr>(Matched)->getArg(0)->getSourceRange();
81-
SourceRange IndexRange = IndexExpr->getSourceRange();
81+
const SourceRange IndexRange = IndexExpr->getSourceRange();
8282

8383
auto Diag = diag(Matched->getExprLoc(),
8484
"do not use array subscript when the index is "
@@ -115,7 +115,7 @@ void ProBoundsConstantArrayIndexCheck::check(
115115
const auto &SizeArg = TemplateArgs[1];
116116
if (SizeArg.getKind() != TemplateArgument::Integral)
117117
return;
118-
llvm::APInt ArraySize = SizeArg.getAsIntegral();
118+
const llvm::APInt ArraySize = SizeArg.getAsIntegral();
119119

120120
// Get uint64_t values, because different bitwidths would lead to an assertion
121121
// in APInt::uge.

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeCstyleCastCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void ProTypeCstyleCastCheck::check(const MatchFinder::MatchResult &Result) {
4545
return;
4646
}
4747

48-
QualType SourceType = MatchedCast->getSubExpr()->getType();
48+
const QualType SourceType = MatchedCast->getSubExpr()->getType();
4949

5050
if (MatchedCast->getCastKind() == CK_BaseToDerived) {
5151
const auto *SourceDecl = SourceType->getPointeeCXXRecordDecl();
@@ -58,7 +58,7 @@ void ProTypeCstyleCastCheck::check(const MatchFinder::MatchResult &Result) {
5858
// Leave type spelling exactly as it was (unlike
5959
// getTypeAsWritten().getAsString() which would spell enum types 'enum
6060
// X').
61-
StringRef DestTypeString = Lexer::getSourceText(
61+
const StringRef DestTypeString = Lexer::getSourceText(
6262
CharSourceRange::getTokenRange(
6363
MatchedCast->getLParenLoc().getLocWithOffset(1),
6464
MatchedCast->getRParenLoc().getLocWithOffset(-1)),

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct InitializerInsertion {
173173
assert(!Initializers.empty() && "No initializers to insert");
174174
std::string Code;
175175
llvm::raw_string_ostream Stream(Code);
176-
std::string Joined =
176+
const std::string Joined =
177177
llvm::join(Initializers.begin(), Initializers.end(), "(), ");
178178
switch (Placement) {
179179
case InitializerPlacement::New:
@@ -434,7 +434,7 @@ static llvm::StringLiteral getInitializer(QualType QT, bool UseAssignment) {
434434
void ProTypeMemberInitCheck::checkMissingMemberInitializer(
435435
ASTContext &Context, const CXXRecordDecl &ClassDecl,
436436
const CXXConstructorDecl *Ctor) {
437-
bool IsUnion = ClassDecl.isUnion();
437+
const bool IsUnion = ClassDecl.isUnion();
438438

439439
if (IsUnion && ClassDecl.hasInClassInitializer())
440440
return;
@@ -583,7 +583,7 @@ void ProTypeMemberInitCheck::checkMissingBaseClassInitializer(
583583

584584
void ProTypeMemberInitCheck::checkUninitializedTrivialType(
585585
const ASTContext &Context, const VarDecl *Var) {
586-
DiagnosticBuilder Diag =
586+
const DiagnosticBuilder Diag =
587587
diag(Var->getBeginLoc(), "uninitialized record type: %0") << Var;
588588

589589
Diag << FixItHint::CreateInsertion(

0 commit comments

Comments
 (0)