Skip to content

Commit c374080

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (4/N) (#167042)
1 parent 74e34ef commit c374080

21 files changed

+137
-121
lines changed

clang-tools-extra/clang-tidy/utils/ASTUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool rangeIsEntirelyWithinMacroArgument(SourceRange Range,
6767
// Check if the range is entirely contained within a macro argument.
6868
SourceLocation MacroArgExpansionStartForRangeBegin;
6969
SourceLocation MacroArgExpansionStartForRangeEnd;
70-
bool RangeIsEntirelyWithinMacroArgument =
70+
const bool RangeIsEntirelyWithinMacroArgument =
7171
SM &&
7272
SM->isMacroArgExpansion(Range.getBegin(),
7373
&MacroArgExpansionStartForRangeBegin) &&

clang-tools-extra/clang-tidy/utils/BracesAroundStatement.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ FixItHint BraceInsertionHints::closingBraceFixIt() const {
4848
static tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM,
4949
const LangOptions &LangOpts) {
5050
Token Tok;
51-
SourceLocation Beginning = Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
51+
const SourceLocation Beginning =
52+
Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
5253
const bool Invalid = Lexer::getRawToken(Beginning, Tok, SM, LangOpts);
5354
assert(!Invalid && "Expected a valid token.");
5455

@@ -77,15 +78,16 @@ static SourceLocation findEndLocation(const Stmt &S, const SourceManager &SM,
7778
// EOL, insert brace before.
7879
break;
7980
}
80-
tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
81+
const tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
8182
if (TokKind != tok::comment) {
8283
// Non-comment token, insert brace before.
8384
break;
8485
}
8586

86-
SourceLocation TokEndLoc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts);
87-
SourceRange TokRange(Loc, TokEndLoc);
88-
StringRef Comment = Lexer::getSourceText(
87+
const SourceLocation TokEndLoc =
88+
Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts);
89+
const SourceRange TokRange(Loc, TokEndLoc);
90+
const StringRef Comment = Lexer::getSourceText(
8991
CharSourceRange::getTokenRange(TokRange), SM, LangOpts);
9092
if (Comment.starts_with("/*") && Comment.contains('\n')) {
9193
// Multi-line block comment, insert brace before.

clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static bool hasSameParameterTypes(const CXXMethodDecl &D,
6363
static const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {
6464
assert(!D.isConst());
6565

66-
DeclContext::lookup_result LookupResult =
66+
const DeclContext::lookup_result LookupResult =
6767
D.getParent()->lookup(D.getNameInfo().getName());
6868
if (LookupResult.isSingleResult()) {
6969
// No overload.

clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ ExceptionAnalyzer::ExceptionInfo::filterByCatch(const Type *HandlerTy,
360360
llvm::SmallVector<const Type *, 8> TypesToDelete;
361361
for (const auto &ThrownException : ThrownExceptions) {
362362
const Type *ExceptionTy = ThrownException.getFirst();
363-
CanQualType ExceptionCanTy = ExceptionTy->getCanonicalTypeUnqualified();
364-
CanQualType HandlerCanTy = HandlerTy->getCanonicalTypeUnqualified();
363+
const CanQualType ExceptionCanTy =
364+
ExceptionTy->getCanonicalTypeUnqualified();
365+
const CanQualType HandlerCanTy = HandlerTy->getCanonicalTypeUnqualified();
365366

366367
// The handler is of type cv T or cv T& and E and T are the same type
367368
// (ignoring the top-level cv-qualifiers) ...
@@ -476,7 +477,7 @@ ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::throwsException(
476477
// For a constructor, we also have to check the initializers.
477478
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Func)) {
478479
for (const CXXCtorInitializer *Init : Ctor->inits()) {
479-
ExceptionInfo Excs =
480+
const ExceptionInfo Excs =
480481
throwsException(Init->getInit(), Caught, CallStack);
481482
Result.merge(Excs);
482483
}
@@ -533,7 +534,7 @@ ExceptionAnalyzer::throwsException(const Stmt *St,
533534

534535
// Everything is caught through 'catch(...)'.
535536
if (!Catch->getExceptionDecl()) {
536-
ExceptionInfo Rethrown = throwsException(
537+
const ExceptionInfo Rethrown = throwsException(
537538
Catch->getHandlerBlock(), Uncaught.getExceptions(), CallStack);
538539
Results.merge(Rethrown);
539540
Uncaught.clear();
@@ -554,7 +555,7 @@ ExceptionAnalyzer::throwsException(const Stmt *St,
554555
Uncaught.filterByCatch(CaughtType,
555556
Catch->getExceptionDecl()->getASTContext());
556557
if (!FilteredExceptions.empty()) {
557-
ExceptionInfo Rethrown = throwsException(
558+
const ExceptionInfo Rethrown = throwsException(
558559
Catch->getHandlerBlock(), FilteredExceptions, CallStack);
559560
Results.merge(Rethrown);
560561
}
@@ -563,44 +564,46 @@ ExceptionAnalyzer::throwsException(const Stmt *St,
563564
Results.merge(Uncaught);
564565
} else if (const auto *Call = dyn_cast<CallExpr>(St)) {
565566
if (const FunctionDecl *Func = Call->getDirectCallee()) {
566-
ExceptionInfo Excs =
567+
const ExceptionInfo Excs =
567568
throwsException(Func, Caught, CallStack, Call->getBeginLoc());
568569
Results.merge(Excs);
569570
}
570571
} else if (const auto *Construct = dyn_cast<CXXConstructExpr>(St)) {
571-
ExceptionInfo Excs = throwsException(Construct->getConstructor(), Caught,
572-
CallStack, Construct->getBeginLoc());
572+
const ExceptionInfo Excs =
573+
throwsException(Construct->getConstructor(), Caught, CallStack,
574+
Construct->getBeginLoc());
573575
Results.merge(Excs);
574576
} else if (const auto *DefaultInit = dyn_cast<CXXDefaultInitExpr>(St)) {
575-
ExceptionInfo Excs =
577+
const ExceptionInfo Excs =
576578
throwsException(DefaultInit->getExpr(), Caught, CallStack);
577579
Results.merge(Excs);
578580
} else if (const auto *Coro = dyn_cast<CoroutineBodyStmt>(St)) {
579581
for (const Stmt *Child : Coro->childrenExclBody()) {
580582
if (Child != Coro->getExceptionHandler()) {
581-
ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
583+
const ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
582584
Results.merge(Excs);
583585
}
584586
}
585-
ExceptionInfo Excs = throwsException(Coro->getBody(), Caught, CallStack);
587+
const ExceptionInfo Excs =
588+
throwsException(Coro->getBody(), Caught, CallStack);
586589
Results.merge(throwsException(Coro->getExceptionHandler(),
587590
Excs.getExceptions(), CallStack));
588591
for (const auto &Exception : Excs.getExceptions()) {
589592
const Type *ExcType = Exception.getFirst();
590593
if (const CXXRecordDecl *ThrowableRec = ExcType->getAsCXXRecordDecl()) {
591-
ExceptionInfo DestructorExcs = throwsException(
594+
const ExceptionInfo DestructorExcs = throwsException(
592595
ThrowableRec->getDestructor(), Caught, CallStack, SourceLocation{});
593596
Results.merge(DestructorExcs);
594597
}
595598
}
596599
} else if (const auto *Lambda = dyn_cast<LambdaExpr>(St)) {
597600
for (const Stmt *Init : Lambda->capture_inits()) {
598-
ExceptionInfo Excs = throwsException(Init, Caught, CallStack);
601+
const ExceptionInfo Excs = throwsException(Init, Caught, CallStack);
599602
Results.merge(Excs);
600603
}
601604
} else {
602605
for (const Stmt *Child : St->children()) {
603-
ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
606+
const ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
604607
Results.merge(Excs);
605608
}
606609
}

clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ExceptionSpecAnalyzer::analyze(const FunctionDecl *FuncDecl) {
2020
const auto [CacheEntry, NotFound] =
2121
FunctionCache.try_emplace(FuncDecl, State::NotThrowing);
2222
if (NotFound) {
23-
ExceptionSpecAnalyzer::State State = analyzeImpl(FuncDecl);
23+
const ExceptionSpecAnalyzer::State State = analyzeImpl(FuncDecl);
2424
// Update result with calculated value
2525
FunctionCache[FuncDecl] = State;
2626
return State;
@@ -87,20 +87,20 @@ ExceptionSpecAnalyzer::analyzeRecord(const CXXRecordDecl *RecordDecl,
8787
return analyze(MethodDecl);
8888

8989
for (const auto &BaseSpec : RecordDecl->bases()) {
90-
State Result = analyzeBase(BaseSpec, Kind);
90+
const State Result = analyzeBase(BaseSpec, Kind);
9191
if (Result == State::Throwing || Result == State::Unknown)
9292
return Result;
9393
}
9494

9595
for (const auto &BaseSpec : RecordDecl->vbases()) {
96-
State Result = analyzeBase(BaseSpec, Kind);
96+
const State Result = analyzeBase(BaseSpec, Kind);
9797
if (Result == State::Throwing || Result == State::Unknown)
9898
return Result;
9999
}
100100

101101
for (const auto *FDecl : RecordDecl->fields())
102102
if (!FDecl->isInvalidDecl() && !FDecl->isUnnamedBitField()) {
103-
State Result = analyzeFieldDecl(FDecl, Kind);
103+
const State Result = analyzeFieldDecl(FDecl, Kind);
104104
if (Result == State::Throwing || Result == State::Unknown)
105105
return Result;
106106
}

clang-tools-extra/clang-tidy/utils/ExprSequence.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ static SmallVector<const Stmt *, 1> getParentStmts(const Stmt *S,
2929
ASTContext *Context) {
3030
SmallVector<const Stmt *, 1> Result;
3131

32-
TraversalKindScope RAII(*Context, TK_AsIs);
32+
const TraversalKindScope RAII(*Context, TK_AsIs);
3333
DynTypedNodeList Parents = Context->getParents(*S);
3434

3535
SmallVector<DynTypedNode, 1> NodesToProcess(Parents.begin(), Parents.end());
3636

3737
while (!NodesToProcess.empty()) {
38-
DynTypedNode Node = NodesToProcess.back();
38+
const DynTypedNode Node = NodesToProcess.back();
3939
NodesToProcess.pop_back();
4040

4141
if (const auto *S = Node.get<Stmt>()) {
@@ -95,7 +95,8 @@ bool ExprSequence::inSequence(const Stmt *Before, const Stmt *After) const {
9595
return true;
9696
}
9797

98-
SmallVector<const Stmt *, 1> BeforeParents = getParentStmts(Before, Context);
98+
const SmallVector<const Stmt *, 1> BeforeParents =
99+
getParentStmts(Before, Context);
99100

100101
// Since C++17, the callee of a call expression is guaranteed to be sequenced
101102
// before all of the arguments.

clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,35 @@ namespace clang::tidy::utils {
1515

1616
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM,
1717
const FileExtensionsSet &HeaderFileExtensions) {
18-
SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
18+
const SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
1919
return isFileExtension(SM.getFilename(ExpansionLoc), HeaderFileExtensions);
2020
}
2121

2222
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
2323
const FileExtensionsSet &HeaderFileExtensions) {
24-
PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
24+
const PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
2525
return isFileExtension(PresumedLocation.getFilename(), HeaderFileExtensions);
2626
}
2727

2828
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
2929
const FileExtensionsSet &HeaderFileExtensions) {
30-
SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
30+
const SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
3131
return isFileExtension(SM.getFilename(SpellingLoc), HeaderFileExtensions);
3232
}
3333

3434
bool parseFileExtensions(StringRef AllFileExtensions,
3535
FileExtensionsSet &FileExtensions,
3636
StringRef Delimiters) {
3737
SmallVector<StringRef, 5> Suffixes;
38-
for (char Delimiter : Delimiters) {
38+
for (const char Delimiter : Delimiters) {
3939
if (AllFileExtensions.contains(Delimiter)) {
4040
AllFileExtensions.split(Suffixes, Delimiter);
4141
break;
4242
}
4343
}
4444

4545
FileExtensions.clear();
46-
for (StringRef Suffix : Suffixes) {
46+
for (const StringRef Suffix : Suffixes) {
4747
StringRef Extension = Suffix.trim();
4848
if (!llvm::all_of(Extension, isAlphanumeric))
4949
return false;

clang-tools-extra/clang-tidy/utils/FixItHintUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ changePointer(const VarDecl &Var, Qualifiers::TQ Qualifier, const Type *Pointee,
140140
// the `*` token and placing the `const` left of it.
141141
// (`int const* p = nullptr;`)
142142
if (QualPolicy == QualifierPolicy::Right) {
143-
SourceLocation BeforeStar = lexer::findPreviousTokenKind(
143+
const SourceLocation BeforeStar = lexer::findPreviousTokenKind(
144144
Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
145145
tok::star);
146146
if (locDangerous(BeforeStar))
@@ -161,7 +161,7 @@ changePointer(const VarDecl &Var, Qualifiers::TQ Qualifier, const Type *Pointee,
161161
// is the same as 'QualPolicy == Right && isValueType(Pointee)'.
162162
// The `const` must be left of the last `*` token.
163163
// (`int * const* p = nullptr;`)
164-
SourceLocation BeforeStar = lexer::findPreviousTokenKind(
164+
const SourceLocation BeforeStar = lexer::findPreviousTokenKind(
165165
Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
166166
tok::star);
167167
return fixIfNotDangerous(BeforeStar, buildQualifier(Qualifier, true));
@@ -178,7 +178,7 @@ changeReferencee(const VarDecl &Var, Qualifiers::TQ Qualifier, QualType Pointee,
178178
return fixIfNotDangerous(Var.getTypeSpecStartLoc(),
179179
buildQualifier(Qualifier));
180180

181-
SourceLocation BeforeRef = lexer::findPreviousAnyTokenKind(
181+
const SourceLocation BeforeRef = lexer::findPreviousAnyTokenKind(
182182
Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
183183
tok::amp, tok::ampamp);
184184
std::optional<SourceLocation> IgnoredParens =
@@ -201,7 +201,7 @@ std::optional<FixItHint> addQualifierToVarDecl(const VarDecl &Var,
201201
QualTarget == QualifierTarget::Value) &&
202202
"Unexpected Target");
203203

204-
QualType ParenStrippedType = Var.getType().IgnoreParens();
204+
const QualType ParenStrippedType = Var.getType().IgnoreParens();
205205
if (isValueType(ParenStrippedType))
206206
return changeValue(Var, Qualifier, QualTarget, QualPolicy, Context);
207207

clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ FormatStringConverter::formatStringContainsUnreplaceableMacro(
245245
// inhibit conversion. The whole format string will appear to come from that
246246
// macro, as will the function call.
247247
std::optional<StringRef> MaybeSurroundingMacroName;
248-
if (SourceLocation BeginCallLoc = Call->getBeginLoc();
248+
if (const SourceLocation BeginCallLoc = Call->getBeginLoc();
249249
BeginCallLoc.isMacroID())
250250
MaybeSurroundingMacroName =
251251
Lexer::getImmediateMacroName(BeginCallLoc, SM, PP.getLangOpts());
@@ -283,7 +283,8 @@ FormatStringConverter::formatStringContainsUnreplaceableMacro(
283283

284284
void FormatStringConverter::emitAlignment(const PrintfSpecifier &FS,
285285
std::string &FormatSpec) {
286-
ConversionSpecifier::Kind ArgKind = FS.getConversionSpecifier().getKind();
286+
const ConversionSpecifier::Kind ArgKind =
287+
FS.getConversionSpecifier().getKind();
287288

288289
// We only care about alignment if a field width is specified
289290
if (FS.getFieldWidth().getHowSpecified() != OptionalAmount::NotSpecified) {
@@ -499,7 +500,8 @@ bool FormatStringConverter::emitIntegerArgument(
499500
/// @returns true on success, false on failure
500501
bool FormatStringConverter::emitType(const PrintfSpecifier &FS, const Expr *Arg,
501502
std::string &FormatSpec) {
502-
ConversionSpecifier::Kind ArgKind = FS.getConversionSpecifier().getKind();
503+
const ConversionSpecifier::Kind ArgKind =
504+
FS.getConversionSpecifier().getKind();
503505
switch (ArgKind) {
504506
case ConversionSpecifier::Kind::sArg:
505507
emitStringArgument(FS.getArgIndex() + ArgsOffset, Arg);
@@ -798,7 +800,7 @@ void FormatStringConverter::applyFixes(DiagnosticBuilder &Diag,
798800
}
799801

800802
for (const auto &[ArgIndex, Replacement] : ArgFixes) {
801-
SourceLocation AfterOtherSide =
803+
const SourceLocation AfterOtherSide =
802804
Lexer::findNextToken(Args[ArgIndex]->getEndLoc(), SM, LangOpts)
803805
->getLocation();
804806

0 commit comments

Comments
 (0)