Skip to content

Commit c6ffc93

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (14/N) (#167131)
1 parent 6deb50d commit c6ffc93

19 files changed

+99
-92
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void StringIntegerAssignmentCheck::check(
129129
const auto *Argument = Result.Nodes.getNodeAs<Expr>("expr");
130130
const auto CharType =
131131
Result.Nodes.getNodeAs<QualType>("type")->getCanonicalType();
132-
SourceLocation Loc = Argument->getBeginLoc();
132+
const SourceLocation Loc = Argument->getBeginLoc();
133133

134134
// Try to detect a few common expressions to reduce false positives.
135135
if (CharExpressionDetector(CharType, *Result.Context)
@@ -145,7 +145,7 @@ void StringIntegerAssignmentCheck::check(
145145
if (Loc.isMacroID())
146146
return;
147147

148-
bool IsWideCharType = CharType->isWideCharType();
148+
const bool IsWideCharType = CharType->isWideCharType();
149149
if (!CharType->isCharType() && !IsWideCharType)
150150
return;
151151
bool IsOneDigit = false;
@@ -155,7 +155,7 @@ void StringIntegerAssignmentCheck::check(
155155
IsLiteral = true;
156156
}
157157

158-
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
158+
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
159159
Argument->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
160160
if (IsOneDigit) {
161161
Diag << FixItHint::CreateInsertion(Loc, IsWideCharType ? "L'" : "'")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static int enumLength(const EnumDecl *EnumDec) {
5454

5555
static bool hasDisjointValueRange(const EnumDecl *Enum1,
5656
const EnumDecl *Enum2) {
57-
ValueRange Range1(Enum1), Range2(Enum2);
57+
const ValueRange Range1(Enum1), Range2(Enum2);
5858
return llvm::APSInt::compareValues(Range1.MaxVal, Range2.MinVal) < 0 ||
5959
llvm::APSInt::compareValues(Range2.MaxVal, Range1.MinVal) < 0;
6060
}
@@ -94,9 +94,9 @@ static int countNonPowOfTwoLiteralNum(const EnumDecl *EnumDec) {
9494
/// last enumerator is the sum of the lesser values (and initialized by a
9595
/// literal) or when it could contain consecutive values.
9696
static bool isPossiblyBitMask(const EnumDecl *EnumDec) {
97-
ValueRange VR(EnumDec);
98-
int EnumLen = enumLength(EnumDec);
99-
int NonPowOfTwoCounter = countNonPowOfTwoLiteralNum(EnumDec);
97+
const ValueRange VR(EnumDec);
98+
const int EnumLen = enumLength(EnumDec);
99+
const int NonPowOfTwoCounter = countNonPowOfTwoLiteralNum(EnumDec);
100100
return NonPowOfTwoCounter >= 1 && NonPowOfTwoCounter <= 2 &&
101101
NonPowOfTwoCounter < EnumLen / 2 &&
102102
(VR.MaxVal - VR.MinVal != EnumLen - 1) &&

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
6666
if (!Check.IgnoredRegexString.empty() && Check.IgnoredRegex.match(FileName))
6767
return;
6868

69-
SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
69+
const SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
7070

7171
const std::optional<StringRef> IFE =
7272
utils::getFileExtension(FileName, Check.ImplementationFileExtensions);
@@ -81,7 +81,7 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
8181
llvm::sys::path::replace_extension(GuessedFileName,
8282
(!HFE.empty() ? "." : "") + HFE);
8383

84-
OptionalFileEntryRef File =
84+
const OptionalFileEntryRef File =
8585
PP->LookupFile(DiagLoc, GuessedFileName, IsAngled, nullptr, nullptr,
8686
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
8787
if (File) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ void SuspiciousMemoryComparisonCheck::check(
4444

4545
for (unsigned int ArgIndex = 0; ArgIndex < 2; ++ArgIndex) {
4646
const Expr *ArgExpr = CE->getArg(ArgIndex);
47-
QualType ArgType = ArgExpr->IgnoreImplicit()->getType();
47+
const QualType ArgType = ArgExpr->IgnoreImplicit()->getType();
4848
const Type *PointeeType = ArgType->getPointeeOrArrayElementType();
4949
assert(PointeeType != nullptr && "PointeeType should always be available.");
50-
QualType PointeeQualifiedType(PointeeType, 0);
50+
const QualType PointeeQualifiedType(PointeeType, 0);
5151

5252
if (PointeeType->isRecordType()) {
5353
if (const RecordDecl *RD =
@@ -65,7 +65,7 @@ void SuspiciousMemoryComparisonCheck::check(
6565
}
6666

6767
if (!PointeeType->isIncompleteType()) {
68-
uint64_t PointeeSize = Ctx.getTypeSize(PointeeType);
68+
const uint64_t PointeeSize = Ctx.getTypeSize(PointeeType);
6969
if (ComparedBits && *ComparedBits >= PointeeSize &&
7070
!Ctx.hasUniqueObjectRepresentations(PointeeQualifiedType)) {
7171
diag(CE->getBeginLoc(),

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
6060
// Case 1: fill_char of memset() is a character '0'. Probably an
6161
// integer zero was intended.
6262

63-
SourceRange CharRange = CharZeroFill->getSourceRange();
63+
const SourceRange CharRange = CharZeroFill->getSourceRange();
6464
auto Diag =
6565
diag(CharZeroFill->getBeginLoc(), "memset fill value is char '0', "
6666
"potentially mistaken for int 0");
@@ -82,7 +82,7 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
8282
if (!NumFill->EvaluateAsInt(EVResult, *Result.Context))
8383
return;
8484

85-
llvm::APSInt NumValue = EVResult.Val.getInt();
85+
const llvm::APSInt NumValue = EVResult.Val.getInt();
8686
if (NumValue >= 0 && NumValue <= UCharMax)
8787
return;
8888

@@ -110,7 +110,7 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
110110
Expr::EvalResult EVResult;
111111
if (!FillChar->isValueDependent() &&
112112
FillChar->EvaluateAsInt(EVResult, *Result.Context)) {
113-
llvm::APSInt Value1 = EVResult.Val.getInt();
113+
const llvm::APSInt Value1 = EVResult.Val.getInt();
114114
if (Value1 == 0 || Value1.isNegative())
115115
return;
116116
}
@@ -120,8 +120,10 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
120120
// and fix-its to swap the arguments.
121121
auto D = diag(Call->getBeginLoc(),
122122
"memset of size zero, potentially swapped arguments");
123-
StringRef RHSString = tooling::fixit::getText(*ByteCount, *Result.Context);
124-
StringRef LHSString = tooling::fixit::getText(*FillChar, *Result.Context);
123+
const StringRef RHSString =
124+
tooling::fixit::getText(*ByteCount, *Result.Context);
125+
const StringRef LHSString =
126+
tooling::fixit::getText(*FillChar, *Result.Context);
125127
if (LHSString.empty() || RHSString.empty())
126128
return;
127129

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static bool isConcatenatedLiteralsOnPurpose(ASTContext *Ctx,
1919
// String literals surrounded by parentheses are assumed to be on purpose.
2020
// i.e.: const char* Array[] = { ("a" "b" "c"), "d", [...] };
2121

22-
TraversalKindScope RAII(*Ctx, TK_AsIs);
22+
const TraversalKindScope RAII(*Ctx, TK_AsIs);
2323
auto Parents = Ctx->getParents(*Lit);
2424
if (Parents.size() == 1 && Parents[0].get<ParenExpr>() != nullptr)
2525
return true;
@@ -35,15 +35,15 @@ static bool isConcatenatedLiteralsOnPurpose(ASTContext *Ctx,
3535
// };
3636
const SourceManager &SM = Ctx->getSourceManager();
3737
bool IndentedCorrectly = true;
38-
SourceLocation FirstToken = Lit->getStrTokenLoc(0);
39-
FileID BaseFID = SM.getFileID(FirstToken);
40-
unsigned int BaseIndent = SM.getSpellingColumnNumber(FirstToken);
41-
unsigned int BaseLine = SM.getSpellingLineNumber(FirstToken);
38+
const SourceLocation FirstToken = Lit->getStrTokenLoc(0);
39+
const FileID BaseFID = SM.getFileID(FirstToken);
40+
const unsigned int BaseIndent = SM.getSpellingColumnNumber(FirstToken);
41+
const unsigned int BaseLine = SM.getSpellingLineNumber(FirstToken);
4242
for (unsigned int TokNum = 1; TokNum < Lit->getNumConcatenated(); ++TokNum) {
43-
SourceLocation Token = Lit->getStrTokenLoc(TokNum);
44-
FileID FID = SM.getFileID(Token);
45-
unsigned int Indent = SM.getSpellingColumnNumber(Token);
46-
unsigned int Line = SM.getSpellingLineNumber(Token);
43+
const SourceLocation Token = Lit->getStrTokenLoc(TokNum);
44+
const FileID FID = SM.getFileID(Token);
45+
const unsigned int Indent = SM.getSpellingColumnNumber(Token);
46+
const unsigned int Line = SM.getSpellingLineNumber(Token);
4747
if (FID != BaseFID || Line != BaseLine + TokNum || Indent <= BaseIndent) {
4848
IndentedCorrectly = false;
4949
break;
@@ -100,7 +100,7 @@ void SuspiciousMissingCommaCheck::check(
100100
assert(InitializerList && ConcatenatedLiteral);
101101

102102
// Skip small arrays as they often generate false-positive.
103-
unsigned int Size = InitializerList->getNumInits();
103+
const unsigned int Size = InitializerList->getNumInits();
104104
if (Size < SizeThreshold)
105105
return;
106106

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class IsSamePtrExpr : public StmtVisitor<IsSamePtrExpr, bool> {
4444
return false;
4545
if (!check(E1->getBase(), E2->getBase()))
4646
return false;
47-
DeclAccessPair FD = E1->getFoundDecl();
47+
const DeclAccessPair FD = E1->getFoundDecl();
4848
return isa<FieldDecl>(FD.getDecl()) && FD == E2->getFoundDecl();
4949
}
5050

@@ -145,7 +145,7 @@ void SuspiciousReallocUsageCheck::check(
145145
if (FindAssignToVarBefore{Var, DeclRef, SM}.Visit(Func->getBody()))
146146
return;
147147

148-
StringRef CodeOfAssignedExpr = Lexer::getSourceText(
148+
const StringRef CodeOfAssignedExpr = Lexer::getSourceText(
149149
CharSourceRange::getTokenRange(PtrResultExpr->getSourceRange()), SM,
150150
getLangOpts());
151151
diag(Call->getBeginLoc(), "'%0' may be set to null if 'realloc' fails, which "

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
3131
return;
3232

3333
const auto *Semicolon = Result.Nodes.getNodeAs<NullStmt>("semi");
34-
SourceLocation LocStart = Semicolon->getBeginLoc();
34+
const SourceLocation LocStart = Semicolon->getBeginLoc();
3535

3636
if (LocStart.isMacroID())
3737
return;
@@ -40,7 +40,7 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
4040
auto Token = utils::lexer::getPreviousToken(LocStart, Ctxt.getSourceManager(),
4141
Ctxt.getLangOpts());
4242
auto &SM = *Result.SourceManager;
43-
unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
43+
const unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
4444

4545
const auto *Statement = Result.Nodes.getNodeAs<Stmt>("stmt");
4646
const bool IsIfStmt = isa<IfStmt>(Statement);
@@ -49,18 +49,20 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
4949
SM.getSpellingLineNumber(Token.getLocation()) != SemicolonLine)
5050
return;
5151

52-
SourceLocation LocEnd = Semicolon->getEndLoc();
53-
FileID FID = SM.getFileID(LocEnd);
54-
llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, LocEnd);
52+
const SourceLocation LocEnd = Semicolon->getEndLoc();
53+
const FileID FID = SM.getFileID(LocEnd);
54+
const llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, LocEnd);
5555
Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(),
5656
Buffer.getBufferStart(), SM.getCharacterData(LocEnd) + 1,
5757
Buffer.getBufferEnd());
5858
if (Lexer.LexFromRawLexer(Token))
5959
return;
6060

61-
unsigned BaseIndent = SM.getSpellingColumnNumber(Statement->getBeginLoc());
62-
unsigned NewTokenIndent = SM.getSpellingColumnNumber(Token.getLocation());
63-
unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());
61+
const unsigned BaseIndent =
62+
SM.getSpellingColumnNumber(Statement->getBeginLoc());
63+
const unsigned NewTokenIndent =
64+
SM.getSpellingColumnNumber(Token.getLocation());
65+
const unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());
6466

6567
if (!IsIfStmt && NewTokenIndent <= BaseIndent &&
6668
Token.getKind() != tok::l_brace && NewTokenLine != SemicolonLine)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void SuspiciousStringCompareCheck::registerMatchers(MatchFinder *Finder) {
8888

8989
// Add the list of known string compare-like functions and add user-defined
9090
// functions.
91-
std::vector<StringRef> FunctionNames = utils::options::parseListPair(
91+
const std::vector<StringRef> FunctionNames = utils::options::parseListPair(
9292
KnownStringCompareFunctions, StringCompareLikeFunctions);
9393

9494
// Match a call to a string compare functions.
@@ -163,7 +163,7 @@ void SuspiciousStringCompareCheck::check(
163163
assert(Decl != nullptr && Call != nullptr);
164164

165165
if (Result.Nodes.getNodeAs<Stmt>("missing-comparison")) {
166-
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
166+
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
167167
Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
168168
getLangOpts());
169169

@@ -173,10 +173,10 @@ void SuspiciousStringCompareCheck::check(
173173
}
174174

175175
if (const auto *E = Result.Nodes.getNodeAs<Expr>("logical-not-comparison")) {
176-
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
176+
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
177177
Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
178178
getLangOpts());
179-
SourceLocation NotLoc = E->getBeginLoc();
179+
const SourceLocation NotLoc = E->getBeginLoc();
180180

181181
diag(Call->getBeginLoc(),
182182
"function %0 is compared using logical not operator")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static bool areArgumentsPotentiallySwapped(const QualType LTo,
7070
if (LTo == RFrom && REq)
7171
return true;
7272

73-
bool LEq = areTypesSemiEqual(LTo, RFrom);
73+
const bool LEq = areTypesSemiEqual(LTo, RFrom);
7474
if (RTo == LFrom && LEq)
7575
return true;
7676

0 commit comments

Comments
 (0)