Skip to content

Commit ace77c2

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (9/N) (#167124)
1 parent 5896a25 commit ace77c2

21 files changed

+130
-123
lines changed

clang-tools-extra/clang-tidy/readability/NamedParameterCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void NamedParameterCheck::check(const MatchFinder::MatchResult &Result) {
7979
// void foo(int /*unused*/)
8080
const char *Begin = SM.getCharacterData(Parm->getBeginLoc());
8181
const char *End = SM.getCharacterData(Parm->getLocation());
82-
StringRef Data(Begin, End - Begin);
82+
const StringRef Data(Begin, End - Begin);
8383
if (Data.contains("/*"))
8484
continue;
8585

@@ -104,15 +104,15 @@ void NamedParameterCheck::check(const MatchFinder::MatchResult &Result) {
104104
if (M && M->size_overridden_methods() > 0) {
105105
const ParmVarDecl *OtherParm =
106106
(*M->begin_overridden_methods())->getParamDecl(P.second);
107-
StringRef Name = OtherParm->getName();
107+
const StringRef Name = OtherParm->getName();
108108
if (!Name.empty())
109109
NewName = Name;
110110
}
111111

112112
// If the definition has a named parameter use that name.
113113
if (Definition) {
114114
const ParmVarDecl *DefParm = Definition->getParamDecl(P.second);
115-
StringRef Name = DefParm->getName();
115+
const StringRef Name = DefParm->getName();
116116
if (!Name.empty())
117117
NewName = Name;
118118
}

clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ getNamespaceNameAsWritten(SourceLocation &Loc, const SourceManager &Sources,
7070
--Nesting;
7171
} else if (Nesting == 0) {
7272
if (T->is(tok::raw_identifier)) {
73-
StringRef ID = T->getRawIdentifier();
73+
const StringRef ID = T->getRawIdentifier();
7474
if (ID != "namespace")
7575
Result.append(std::string(ID));
7676
if (ID == "inline")
@@ -96,13 +96,13 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
9696

9797
// Don't require closing comments for namespaces spanning less than certain
9898
// number of lines.
99-
unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
100-
unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
99+
const unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
100+
const unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
101101
if (EndLine - StartLine + 1 <= ShortNamespaceLines)
102102
return;
103103

104104
// Find next token after the namespace closing brace.
105-
SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
105+
const SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
106106
ND->getRBraceLoc(), /*Offset=*/0, Sources, getLangOpts());
107107
SourceLocation Loc = AfterRBrace;
108108
SourceLocation LBraceLoc = ND->getBeginLoc();
@@ -137,7 +137,8 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
137137
if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc))
138138
return;
139139

140-
bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine;
140+
const bool NextTokenIsOnSameLine =
141+
Sources.getSpellingLineNumber(Loc) == EndLine;
141142
// If we insert a line comment before the token in the same line, we need
142143
// to insert a line break.
143144
bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
@@ -148,11 +149,12 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
148149

149150
// Try to find existing namespace closing comment on the same line.
150151
if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
151-
StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
152+
const StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
152153
SmallVector<StringRef, 7> Groups;
153154
if (NamespaceCommentPattern.match(Comment, &Groups)) {
154-
StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
155-
StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";
155+
const StringRef NamespaceNameInComment =
156+
Groups.size() > 5 ? Groups[5] : "";
157+
const StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";
156158

157159
if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
158160
(*NamespaceNameAsWritten == NamespaceNameInComment &&
@@ -186,7 +188,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
186188
// multi-line or there may be other tokens behind it.
187189
}
188190

189-
std::string NamespaceNameForDiag =
191+
const std::string NamespaceNameForDiag =
190192
ND->isAnonymousNamespace()
191193
? "anonymous namespace"
192194
: ("namespace '" + *NamespaceNameAsWritten + "'");
@@ -203,7 +205,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
203205
Fix.append("\n");
204206

205207
// Place diagnostic at an old comment, or closing brace if we did not have it.
206-
SourceLocation DiagLoc =
208+
const SourceLocation DiagLoc =
207209
OldCommentRange.getBegin() != OldCommentRange.getEnd()
208210
? OldCommentRange.getBegin()
209211
: ND->getRBraceLoc();

clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void NonConstParameterCheck::diagnoseNonConstParameters() {
155155
dyn_cast_or_null<const FunctionDecl>(Par->getParentFunctionOrMethod());
156156
if (!Function)
157157
continue;
158-
unsigned Index = Par->getFunctionScopeIndex();
158+
const unsigned Index = Par->getFunctionScopeIndex();
159159
for (FunctionDecl *FnDecl : Function->redecls()) {
160160
if (FnDecl->getNumParams() <= Index)
161161
continue;

clang-tools-extra/clang-tidy/readability/OperatorsRepresentationCheck.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static StringRef getOperatorSpelling(SourceLocation Loc, ASTContext &Context) {
2323
if (Loc.isInvalid())
2424
return {};
2525

26-
SourceManager &SM = Context.getSourceManager();
26+
const SourceManager &SM = Context.getSourceManager();
2727

2828
Loc = SM.getSpellingLoc(Loc);
2929
if (Loc.isInvalid())
@@ -41,7 +41,7 @@ AST_MATCHER_P2(BinaryOperator, hasInvalidBinaryOperatorRepresentation,
4141
if (Node.getOpcode() != Kind || ExpectedRepresentation.empty())
4242
return false;
4343

44-
StringRef Spelling =
44+
const StringRef Spelling =
4545
getOperatorSpelling(Node.getOperatorLoc(), Finder->getASTContext());
4646
return !Spelling.empty() && Spelling != ExpectedRepresentation;
4747
}
@@ -52,7 +52,7 @@ AST_MATCHER_P2(UnaryOperator, hasInvalidUnaryOperatorRepresentation,
5252
if (Node.getOpcode() != Kind || ExpectedRepresentation.empty())
5353
return false;
5454

55-
StringRef Spelling =
55+
const StringRef Spelling =
5656
getOperatorSpelling(Node.getOperatorLoc(), Finder->getASTContext());
5757
return !Spelling.empty() && Spelling != ExpectedRepresentation;
5858
}
@@ -63,7 +63,7 @@ AST_MATCHER_P2(CXXOperatorCallExpr, hasInvalidOverloadedOperatorRepresentation,
6363
if (Node.getOperator() != Kind || ExpectedRepresentation.empty())
6464
return false;
6565

66-
StringRef Spelling =
66+
const StringRef Spelling =
6767
getOperatorSpelling(Node.getOperatorLoc(), Finder->getASTContext());
6868
return !Spelling.empty() && Spelling != ExpectedRepresentation;
6969
}
@@ -297,9 +297,9 @@ void OperatorsRepresentationCheck::check(
297297
if (TokenRange.isInvalid())
298298
return;
299299

300-
StringRef Spelling = Lexer::getSourceText(TokenRange, *Result.SourceManager,
301-
Result.Context->getLangOpts());
302-
StringRef TranslatedSpelling = translate(Spelling);
300+
const StringRef Spelling = Lexer::getSourceText(
301+
TokenRange, *Result.SourceManager, Result.Context->getLangOpts());
302+
const StringRef TranslatedSpelling = translate(Spelling);
303303

304304
if (TranslatedSpelling.empty())
305305
return;
@@ -312,7 +312,7 @@ void OperatorsRepresentationCheck::check(
312312
SourceRepresentation = "a traditional";
313313
TargetRepresentation = "an alternative";
314314

315-
StringRef SpellingEx = Lexer::getSourceText(
315+
const StringRef SpellingEx = Lexer::getSourceText(
316316
CharSourceRange::getCharRange(
317317
TokenRange.getBegin().getLocWithOffset(-1),
318318
TokenRange.getBegin().getLocWithOffset(Spelling.size() + 1U)),

clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ findQualToken(const VarDecl *Decl, Qualifier Qual,
4444
SourceLocation BeginLoc = Decl->getQualifierLoc().getBeginLoc();
4545
if (BeginLoc.isInvalid())
4646
BeginLoc = Decl->getBeginLoc();
47-
SourceLocation EndLoc = Decl->getLocation();
47+
const SourceLocation EndLoc = Decl->getLocation();
4848

49-
CharSourceRange FileRange = Lexer::makeFileCharRange(
49+
const CharSourceRange FileRange = Lexer::makeFileCharRange(
5050
CharSourceRange::getCharRange(BeginLoc, EndLoc), *Result.SourceManager,
5151
Result.Context->getLangOpts());
5252

5353
if (FileRange.isInvalid())
5454
return std::nullopt;
5555

56-
tok::TokenKind Tok = Qual == Qualifier::Const ? tok::kw_const
57-
: Qual == Qualifier::Volatile ? tok::kw_volatile
58-
: tok::kw_restrict;
56+
const tok::TokenKind Tok = Qual == Qualifier::Const ? tok::kw_const
57+
: Qual == Qualifier::Volatile ? tok::kw_volatile
58+
: tok::kw_restrict;
5959

6060
return utils::lexer::getQualifyingToken(Tok, FileRange, *Result.Context,
6161
*Result.SourceManager);
@@ -90,13 +90,13 @@ mergeReplacementRange(SourceRange &TypeSpecifier, const Token &ConstToken) {
9090
}
9191

9292
static bool isPointerConst(QualType QType) {
93-
QualType Pointee = QType->getPointeeType();
93+
const QualType Pointee = QType->getPointeeType();
9494
assert(!Pointee.isNull() && "can't have a null Pointee");
9595
return Pointee.isConstQualified();
9696
}
9797

9898
static bool isAutoPointerConst(QualType QType) {
99-
QualType Pointee =
99+
const QualType Pointee =
100100
cast<AutoType>(QType->getPointeeType().getTypePtr())->desugar();
101101
assert(!Pointee.isNull() && "can't have a null Pointee");
102102
return Pointee.isConstQualified();
@@ -222,33 +222,34 @@ void QualifiedAutoCheck::check(const MatchFinder::MatchResult &Result) {
222222
if (Var->getLocation() == TypeSpecifier.getEnd().getLocWithOffset(1))
223223
TypeSpecifier.setEnd(TypeSpecifier.getEnd().getLocWithOffset(1));
224224

225-
CharSourceRange FixItRange = CharSourceRange::getCharRange(TypeSpecifier);
225+
const CharSourceRange FixItRange =
226+
CharSourceRange::getCharRange(TypeSpecifier);
226227
if (FixItRange.isInvalid())
227228
return;
228229

229230
SourceLocation FixitLoc = FixItRange.getBegin();
230-
for (SourceRange &Range : RemoveQualifiersRange) {
231+
for (const SourceRange &Range : RemoveQualifiersRange) {
231232
if (Range.getBegin() < FixitLoc)
232233
FixitLoc = Range.getBegin();
233234
}
234235

235-
std::string ReplStr = [&] {
236-
llvm::StringRef PtrConst = isPointerConst(Var->getType()) ? "const " : "";
237-
llvm::StringRef LocalConst = IsLocalConst ? "const " : "";
238-
llvm::StringRef LocalVol = IsLocalVolatile ? "volatile " : "";
239-
llvm::StringRef LocalRestrict = IsLocalRestrict ? "__restrict " : "";
236+
const std::string ReplStr = [&] {
237+
const StringRef PtrConst = isPointerConst(Var->getType()) ? "const " : "";
238+
const StringRef LocalConst = IsLocalConst ? "const " : "";
239+
const StringRef LocalVol = IsLocalVolatile ? "volatile " : "";
240+
const StringRef LocalRestrict = IsLocalRestrict ? "__restrict " : "";
240241
return (PtrConst + "auto *" + LocalConst + LocalVol + LocalRestrict)
241242
.str();
242243
}();
243244

244-
DiagnosticBuilder Diag =
245+
const DiagnosticBuilder Diag =
245246
diag(FixitLoc,
246247
"'%select{|const }0%select{|volatile }1%select{|__restrict }2auto "
247248
"%3' can be declared as '%4%3'")
248249
<< IsLocalConst << IsLocalVolatile << IsLocalRestrict << Var->getName()
249250
<< ReplStr;
250251

251-
for (SourceRange &Range : RemoveQualifiersRange) {
252+
for (const SourceRange &Range : RemoveQualifiersRange) {
252253
Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange(Range));
253254
}
254255

@@ -285,7 +286,7 @@ void QualifiedAutoCheck::check(const MatchFinder::MatchResult &Result) {
285286
if (TypeSpec->isInvalid() || TypeSpec->getBegin().isMacroID() ||
286287
TypeSpec->getEnd().isMacroID())
287288
return;
288-
SourceLocation InsertPos = TypeSpec->getBegin();
289+
const SourceLocation InsertPos = TypeSpec->getBegin();
289290
diag(InsertPos,
290291
"'auto *%select{|const }0%select{|volatile }1%2' can be declared as "
291292
"'const auto *%select{|const }0%select{|volatile }1%2'")
@@ -307,7 +308,7 @@ void QualifiedAutoCheck::check(const MatchFinder::MatchResult &Result) {
307308
if (TypeSpec->isInvalid() || TypeSpec->getBegin().isMacroID() ||
308309
TypeSpec->getEnd().isMacroID())
309310
return;
310-
SourceLocation InsertPos = TypeSpec->getBegin();
311+
const SourceLocation InsertPos = TypeSpec->getBegin();
311312
diag(InsertPos, "'auto &%0' can be declared as 'const auto &%0'")
312313
<< Var->getName() << FixItHint::CreateInsertion(InsertPos, "const ");
313314
}

clang-tools-extra/clang-tidy/readability/RedundantAccessSpecifiersCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void RedundantAccessSpecifiersCheck::check(
4343
LastASDecl = ASDecl;
4444

4545
if (CheckFirstDeclaration) {
46-
AccessSpecifier DefaultSpecifier =
46+
const AccessSpecifier DefaultSpecifier =
4747
MatchedDecl->isClass() ? AS_private : AS_public;
4848
if (ASDecl->getAccess() == DefaultSpecifier) {
4949
diag(ASDecl->getLocation(),

clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ static bool areTypesEqual(QualType S, QualType D) {
2525
if (TS != TD)
2626
return false;
2727

28-
QualType PtrS = S->getPointeeType();
29-
QualType PtrD = D->getPointeeType();
28+
const QualType PtrS = S->getPointeeType();
29+
const QualType PtrD = D->getPointeeType();
3030

3131
if (!PtrS.isNull() && !PtrD.isNull())
3232
return areTypesEqual(PtrS, PtrD);

clang-tools-extra/clang-tidy/readability/RedundantControlFlowCheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) {
5050

5151
void RedundantControlFlowCheck::checkRedundantReturn(
5252
const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
53-
CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
53+
const CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
5454
if (const auto *Return = dyn_cast<ReturnStmt>(*Last))
5555
issueDiagnostic(Result, Block, Return->getSourceRange(),
5656
RedundantReturnDiag);
5757
}
5858

5959
void RedundantControlFlowCheck::checkRedundantContinue(
6060
const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
61-
CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
61+
const CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
6262
if (const auto *Continue = dyn_cast<ContinueStmt>(*Last))
6363
issueDiagnostic(Result, Block, Continue->getSourceRange(),
6464
RedundantContinueDiag);
@@ -67,11 +67,12 @@ void RedundantControlFlowCheck::checkRedundantContinue(
6767
void RedundantControlFlowCheck::issueDiagnostic(
6868
const MatchFinder::MatchResult &Result, const CompoundStmt *const Block,
6969
const SourceRange &StmtRange, const char *const Diag) {
70-
SourceManager &SM = *Result.SourceManager;
70+
const SourceManager &SM = *Result.SourceManager;
7171
if (isLocationInMacroExpansion(SM, StmtRange.getBegin()))
7272
return;
7373

74-
CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
74+
const CompoundStmt::const_reverse_body_iterator Previous =
75+
++Block->body_rbegin();
7576
SourceLocation Start;
7677
if (Previous != Block->body_rend())
7778
Start = Lexer::findLocationAfterToken(

clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
7979
}
8080
}
8181

82-
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
82+
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
8383
D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
8484
{
8585
auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;

clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ AST_POLYMORPHIC_MATCHER_P(isInternalLinkage,
5252
static SourceLocation getInlineTokenLocation(SourceRange RangeLocation,
5353
const SourceManager &Sources,
5454
const LangOptions &LangOpts) {
55-
SourceLocation Loc = RangeLocation.getBegin();
55+
const SourceLocation Loc = RangeLocation.getBegin();
5656
if (Loc.isMacroID())
5757
return {};
5858

@@ -106,7 +106,7 @@ template <typename T>
106106
void RedundantInlineSpecifierCheck::handleMatchedDecl(
107107
const T *MatchedDecl, const SourceManager &Sources,
108108
const MatchFinder::MatchResult &Result, StringRef Message) {
109-
SourceLocation Loc = getInlineTokenLocation(
109+
const SourceLocation Loc = getInlineTokenLocation(
110110
MatchedDecl->getSourceRange(), Sources, Result.Context->getLangOpts());
111111
if (Loc.isValid())
112112
diag(Loc, Message) << MatchedDecl << FixItHint::CreateRemoval(Loc);

0 commit comments

Comments
 (0)