Skip to content

Commit 6deb50d

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

15 files changed

+87
-82
lines changed

clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static bool isZero(const Expr *E) {
163163
case Stmt::IntegerLiteralClass:
164164
return !cast<IntegerLiteral>(E)->getValue();
165165
case Stmt::FloatingLiteralClass: {
166-
llvm::APFloat Value = cast<FloatingLiteral>(E)->getValue();
166+
const llvm::APFloat Value = cast<FloatingLiteral>(E)->getValue();
167167
return Value.isZero() && !Value.isNegative();
168168
}
169169
default:
@@ -297,16 +297,16 @@ void UseDefaultMemberInitCheck::checkDefaultInit(
297297
}) > 1)
298298
return;
299299

300-
SourceLocation StartLoc = Field->getBeginLoc();
300+
const SourceLocation StartLoc = Field->getBeginLoc();
301301
if (StartLoc.isMacroID() && IgnoreMacros)
302302
return;
303303

304-
SourceLocation FieldEnd =
304+
const SourceLocation FieldEnd =
305305
Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
306306
*Result.SourceManager, getLangOpts());
307-
SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
307+
const SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
308308
Init->getLParenLoc(), 0, *Result.SourceManager, getLangOpts());
309-
CharSourceRange InitRange =
309+
const CharSourceRange InitRange =
310310
CharSourceRange::getCharRange(LParenEnd, Init->getRParenLoc());
311311

312312
const Expr *InitExpression = Init->getInit();

clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void UseDesignatedInitializersCheck::check(
152152
if (IgnoreMacros && InitList->getBeginLoc().isMacroID())
153153
return;
154154
{
155-
DiagnosticBuilder Diag =
155+
const DiagnosticBuilder Diag =
156156
diag(InitList->getLBraceLoc(),
157157
"use designated initializer list to initialize %0");
158158
Diag << InitList->getType() << InitList->getSourceRange();

clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static bool isCopyAssignmentAndCanBeDefaulted(ASTContext *Context,
200200
/// Returns false if the body has any non-whitespace character.
201201
static bool bodyEmpty(const ASTContext *Context, const CompoundStmt *Body) {
202202
bool Invalid = false;
203-
StringRef Text = Lexer::getSourceText(
203+
const StringRef Text = Lexer::getSourceText(
204204
CharSourceRange::getCharRange(Body->getLBracLoc().getLocWithOffset(1),
205205
Body->getRBracLoc()),
206206
Context->getSourceManager(), Context->getLangOpts(), &Invalid);
@@ -306,8 +306,8 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
306306
return;
307307

308308
// If there are comments inside the body, don't do the change.
309-
bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() ||
310-
bodyEmpty(Result.Context, Body);
309+
const bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() ||
310+
bodyEmpty(Result.Context, Body);
311311

312312
std::vector<FixItHint> RemoveInitializers;
313313
unsigned MemberType = 0;
@@ -345,14 +345,14 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
345345
Diag << MemberType;
346346

347347
if (ApplyFix) {
348-
SourceLocation UnifiedEnd = utils::lexer::getUnifiedEndLoc(
348+
const SourceLocation UnifiedEnd = utils::lexer::getUnifiedEndLoc(
349349
*Body, Result.Context->getSourceManager(),
350350
Result.Context->getLangOpts());
351351
// Skipping comments, check for a semicolon after Body->getSourceRange()
352352
std::optional<Token> Token = utils::lexer::findNextTokenSkippingComments(
353353
UnifiedEnd, Result.Context->getSourceManager(),
354354
Result.Context->getLangOpts());
355-
StringRef Replacement =
355+
const StringRef Replacement =
356356
Token && Token->is(tok::semi) ? "= default" : "= default;";
357357
Diag << FixItHint::CreateReplacement(Body->getSourceRange(), Replacement)
358358
<< RemoveInitializers;

clang-tools-extra/clang-tidy/modernize/UseEqualsDeleteCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
7474
void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
7575
if (const auto *Func =
7676
Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
77-
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
77+
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
7878
Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
7979

8080
if (IgnoreMacros && Func->getLocation().isMacroID())

clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void UseIntegerSignComparisonCheck::check(
146146
R3.setBegin(Lexer::getLocForEndOfToken(
147147
SubExprRHS->getEndLoc(), 0, *Result.SourceManager, getLangOpts()));
148148
}
149-
DiagnosticBuilder Diag =
149+
const DiagnosticBuilder Diag =
150150
diag(BinaryOp->getBeginLoc(),
151151
"comparison between 'signed' and 'unsigned' integers");
152152
StringRef CmpNamespace;

clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ void UseNodiscardCheck::registerMatchers(MatchFinder *Finder) {
110110
void UseNodiscardCheck::check(const MatchFinder::MatchResult &Result) {
111111
const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>("no_discard");
112112
// Don't make replacements if the location is invalid or in a macro.
113-
SourceLocation Loc = MatchedDecl->getLocation();
113+
const SourceLocation Loc = MatchedDecl->getLocation();
114114
if (Loc.isInvalid() || Loc.isMacroID())
115115
return;
116116

117-
SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
117+
const SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
118118

119119
ASTContext &Context = *Result.Context;
120120

clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ void UseNoexceptCheck::check(const MatchFinder::MatchResult &Result) {
8181

8282
assert(Range.isValid() && "Exception Source Range is invalid.");
8383

84-
CharSourceRange CRange = Lexer::makeFileCharRange(
84+
const CharSourceRange CRange = Lexer::makeFileCharRange(
8585
CharSourceRange::getTokenRange(Range), *Result.SourceManager,
8686
Result.Context->getLangOpts());
8787

88-
bool IsNoThrow = FnTy->isNothrow();
89-
StringRef ReplacementStr =
88+
const bool IsNoThrow = FnTy->isNothrow();
89+
const StringRef ReplacementStr =
9090
IsNoThrow ? NoexceptMacro.empty() ? "noexcept" : NoexceptMacro
9191
: NoexceptMacro.empty()
9292
? (DtorOrOperatorDel || UseNoexceptFalse) ? "noexcept(false)" : ""

clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ static bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,
9292
/// Returns true if and only if a replacement was made.
9393
static void replaceWithNullptr(ClangTidyCheck &Check, SourceManager &SM,
9494
SourceLocation StartLoc, SourceLocation EndLoc) {
95-
CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);
95+
const CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);
9696
// Add a space if nullptr follows an alphanumeric character. This happens
9797
// whenever there is an c-style explicit cast to nullptr not surrounded by
9898
// parentheses and right beside a return statement.
99-
SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);
100-
bool NeedsSpace = isAlphanumeric(*SM.getCharacterData(PreviousLocation));
99+
const SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);
100+
const bool NeedsSpace =
101+
isAlphanumeric(*SM.getCharacterData(PreviousLocation));
101102
Check.diag(Range.getBegin(), "use nullptr") << FixItHint::CreateReplacement(
102103
Range, NeedsSpace ? " nullptr" : "nullptr");
103104
}
@@ -136,7 +137,7 @@ class MacroArgUsageVisitor : public RecursiveASTVisitor<MacroArgUsageVisitor> {
136137
}
137138

138139
bool TraverseStmt(Stmt *S) {
139-
bool VisitedPreviously = Visited;
140+
const bool VisitedPreviously = Visited;
140141

141142
if (!RecursiveASTVisitor<MacroArgUsageVisitor>::TraverseStmt(S))
142143
return false;
@@ -258,8 +259,8 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
258259
// If the location comes from a macro body expansion, check to see if its
259260
// coming from one of the allowed 'NULL' macros.
260261
if (SM.isMacroArgExpansion(StartLoc) && SM.isMacroArgExpansion(EndLoc)) {
261-
SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
262-
FileLocEnd = SM.getFileLoc(EndLoc);
262+
const SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
263+
FileLocEnd = SM.getFileLoc(EndLoc);
263264
SourceLocation ImmediateMacroArgLoc, MacroLoc;
264265
// Skip NULL macros used in macro.
265266
if (!getMacroAndArgLocations(StartLoc, ImmediateMacroArgLoc, MacroLoc) ||
@@ -274,7 +275,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
274275
}
275276

276277
if (SM.isMacroBodyExpansion(StartLoc) && SM.isMacroBodyExpansion(EndLoc)) {
277-
StringRef OutermostMacroName =
278+
const StringRef OutermostMacroName =
278279
getOutermostMacroName(StartLoc, SM, Context.getLangOpts());
279280

280281
// Check to see if the user wants to replace the macro being expanded.
@@ -302,7 +303,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
302303
/// Tests that all expansions of a macro arg, one of which expands to
303304
/// result in \p CE, yield NullTo(Member)Pointer casts.
304305
bool allArgUsesValid(const CastExpr *CE) {
305-
SourceLocation CastLoc = CE->getBeginLoc();
306+
const SourceLocation CastLoc = CE->getBeginLoc();
306307

307308
// Step 1: Get location of macro arg and location of the macro the arg was
308309
// provided to.
@@ -348,17 +349,17 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
348349

349350
// Find the location of the immediate macro expansion.
350351
while (true) {
351-
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);
352+
const std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);
352353
const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
353354
const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
354355

355-
SourceLocation OldArgLoc = ArgLoc;
356+
const SourceLocation OldArgLoc = ArgLoc;
356357
ArgLoc = Expansion.getExpansionLocStart();
357358
if (!Expansion.isMacroArgExpansion()) {
358359
if (!MacroLoc.isFileID())
359360
return false;
360361

361-
StringRef Name =
362+
const StringRef Name =
362363
Lexer::getImmediateMacroName(OldArgLoc, SM, Context.getLangOpts());
363364
return llvm::is_contained(NullMacros, Name);
364365
}
@@ -371,7 +372,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
371372

372373
// If spelling location resides in the same FileID as macro expansion
373374
// location, it means there is no inner macro.
374-
FileID MacroFID = SM.getFileID(MacroLoc);
375+
const FileID MacroFID = SM.getFileID(MacroLoc);
375376
if (SM.isInFileID(ArgLoc, MacroFID)) {
376377
// Don't transform this case. If the characters that caused the
377378
// null-conversion come from within a macro, they can't be changed.
@@ -401,7 +402,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
401402
SourceLocation Loc = TestLoc, MacroLoc;
402403

403404
while (true) {
404-
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
405+
const std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
405406
const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
406407
const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
407408

clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void UseOverrideCheck::registerMatchers(MatchFinder *Finder) {
5555
static SmallVector<Token, 16>
5656
parseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result) {
5757
const SourceManager &Sources = *Result.SourceManager;
58-
std::pair<FileID, unsigned> LocInfo =
58+
const std::pair<FileID, unsigned> LocInfo =
5959
Sources.getDecomposedLoc(Range.getBegin());
60-
StringRef File = Sources.getBufferData(LocInfo.first);
60+
const StringRef File = Sources.getBufferData(LocInfo.first);
6161
const char *TokenBegin = File.data() + LocInfo.second;
6262
Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
6363
Result.Context->getLangOpts(), File.begin(), TokenBegin,
@@ -103,12 +103,12 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
103103
Method->isOutOfLine())
104104
return;
105105

106-
bool HasVirtual = Method->isVirtualAsWritten();
107-
bool HasOverride = Method->getAttr<OverrideAttr>();
108-
bool HasFinal = Method->getAttr<FinalAttr>();
106+
const bool HasVirtual = Method->isVirtualAsWritten();
107+
const bool HasOverride = Method->getAttr<OverrideAttr>();
108+
const bool HasFinal = Method->getAttr<FinalAttr>();
109109

110-
bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
111-
unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
110+
const bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
111+
const unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
112112

113113
if ((!OnlyVirtualSpecified && KeywordCount == 1) ||
114114
(!HasVirtual && HasOverride && HasFinal && AllowOverrideAndFinal))
@@ -120,12 +120,12 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
120120
} else if (KeywordCount == 0) {
121121
Message = "annotate this function with '%0' or (rarely) '%1'";
122122
} else {
123-
StringRef Redundant =
123+
const StringRef Redundant =
124124
HasVirtual ? (HasOverride && HasFinal && !AllowOverrideAndFinal
125125
? "'virtual' and '%0' are"
126126
: "'virtual' is")
127127
: "'%0' is";
128-
StringRef Correct = HasFinal ? "'%1'" : "'%0'";
128+
const StringRef Correct = HasFinal ? "'%1'" : "'%0'";
129129

130130
Message = (llvm::Twine(Redundant) +
131131
" redundant since the function is already declared " + Correct)
@@ -135,7 +135,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
135135
auto Diag = diag(Method->getLocation(), Message)
136136
<< OverrideSpelling << FinalSpelling;
137137

138-
CharSourceRange FileRange = Lexer::makeFileCharRange(
138+
const CharSourceRange FileRange = Lexer::makeFileCharRange(
139139
CharSourceRange::getTokenRange(Method->getSourceRange()), Sources,
140140
getLangOpts());
141141

@@ -151,9 +151,9 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
151151
if (!HasFinal && !HasOverride) {
152152
SourceLocation InsertLoc;
153153
std::string ReplacementText = (OverrideSpelling + " ").str();
154-
SourceLocation MethodLoc = Method->getLocation();
154+
const SourceLocation MethodLoc = Method->getLocation();
155155

156-
for (Token T : Tokens) {
156+
for (const Token T : Tokens) {
157157
if (T.is(tok::kw___attribute) &&
158158
!Sources.isBeforeInTranslationUnit(T.getLocation(), MethodLoc)) {
159159
InsertLoc = T.getLocation();
@@ -164,7 +164,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
164164
if (Method->hasAttrs()) {
165165
for (const clang::Attr *A : Method->getAttrs()) {
166166
if (!A->isImplicit() && !A->isInherited()) {
167-
SourceLocation Loc =
167+
const SourceLocation Loc =
168168
Sources.getExpansionLoc(A->getRange().getBegin());
169169
if ((!InsertLoc.isValid() ||
170170
Sources.isBeforeInTranslationUnit(Loc, InsertLoc)) &&
@@ -221,13 +221,14 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
221221
}
222222

223223
if (HasFinal && HasOverride && !AllowOverrideAndFinal) {
224-
SourceLocation OverrideLoc = Method->getAttr<OverrideAttr>()->getLocation();
224+
const SourceLocation OverrideLoc =
225+
Method->getAttr<OverrideAttr>()->getLocation();
225226
Diag << FixItHint::CreateRemoval(
226227
CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));
227228
}
228229

229230
if (HasVirtual) {
230-
for (Token Tok : Tokens) {
231+
for (const Token Tok : Tokens) {
231232
if (Tok.is(tok::kw_virtual)) {
232233
std::optional<Token> NextToken =
233234
utils::lexer::findNextTokenIncludingComments(

clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ findLocksInCompoundStmt(const CompoundStmt *Block,
7474

7575
for (const Stmt *Stmt : Block->body()) {
7676
if (const auto *DS = dyn_cast<DeclStmt>(Stmt)) {
77-
llvm::SmallVector<const VarDecl *> LockGuards = getLockGuardsFromDecl(DS);
77+
const llvm::SmallVector<const VarDecl *> LockGuards =
78+
getLockGuardsFromDecl(DS);
7879

7980
if (!LockGuards.empty()) {
8081
CurrentLockGuardGroup.append(LockGuards);
@@ -176,7 +177,7 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {
176177

177178
void UseScopedLockCheck::check(const MatchFinder::MatchResult &Result) {
178179
if (const auto *DS = Result.Nodes.getNodeAs<DeclStmt>("lock-decl-single")) {
179-
llvm::SmallVector<const VarDecl *> Decls = getLockGuardsFromDecl(DS);
180+
const llvm::SmallVector<const VarDecl *> Decls = getLockGuardsFromDecl(DS);
180181
diagOnMultipleLocks({Decls}, Result);
181182
return;
182183
}

0 commit comments

Comments
 (0)