Skip to content

Commit 545c302

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (8/N) (#167123)
1 parent 01bea27 commit 545c302

22 files changed

+151
-144
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static std::optional<Token>
2727
findConstToRemove(const ParmVarDecl &Param,
2828
const MatchFinder::MatchResult &Result) {
2929

30-
CharSourceRange FileRange = Lexer::makeFileCharRange(
30+
const CharSourceRange FileRange = Lexer::makeFileCharRange(
3131
CharSourceRange::getTokenRange(getTypeRange(Param)),
3232
*Result.SourceManager, Result.Context->getLangOpts());
3333

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ void AvoidReturnWithVoidValueCheck::check(
4747
Result.Nodes.getNodeAs<CompoundStmt>("compound_parent");
4848
if (!StrictMode && !SurroundingBlock)
4949
return;
50-
DiagnosticBuilder Diag = diag(VoidReturn->getBeginLoc(),
51-
"return statement within a void function "
52-
"should not have a specified return value");
50+
const DiagnosticBuilder Diag = diag(
51+
VoidReturn->getBeginLoc(), "return statement within a void function "
52+
"should not have a specified return value");
5353
const SourceLocation SemicolonPos = utils::lexer::findNextTerminator(
5454
VoidReturn->getEndLoc(), *Result.SourceManager, getLangOpts());
5555
if (SemicolonPos.isInvalid())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct AvoidUnconditionalPreprocessorIfPPCallbacks : public PPCallbacks {
4040

4141
bool isImmutable(SourceManager &SM, const LangOptions &LangOpts,
4242
SourceRange ConditionRange) {
43-
SourceLocation Loc = ConditionRange.getBegin();
43+
const SourceLocation Loc = ConditionRange.getBegin();
4444
if (Loc.isMacroID())
4545
return false;
4646

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace clang::tidy::readability {
2020
static tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM,
2121
const LangOptions &LangOpts) {
2222
Token Tok;
23-
SourceLocation Beginning = Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
23+
const SourceLocation Beginning =
24+
Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
2425
const bool Invalid = Lexer::getRawToken(Beginning, Tok, SM, LangOpts);
2526
assert(!Invalid && "Expected a valid token.");
2627

@@ -38,7 +39,7 @@ forwardSkipWhitespaceAndComments(SourceLocation Loc, const SourceManager &SM,
3839
while (isWhitespace(*SM.getCharacterData(Loc)))
3940
Loc = Loc.getLocWithOffset(1);
4041

41-
tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
42+
const tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
4243
if (TokKind != tok::comment)
4344
return Loc;
4445

@@ -80,7 +81,8 @@ void BracesAroundStatementsCheck::check(
8081
} else if (const auto *S = Result.Nodes.getNodeAs<DoStmt>("do")) {
8182
checkStmt(Result, S->getBody(), S->getDoLoc(), S->getWhileLoc());
8283
} else if (const auto *S = Result.Nodes.getNodeAs<WhileStmt>("while")) {
83-
SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
84+
const SourceLocation StartLoc =
85+
findRParenLoc(S, SM, Context->getLangOpts());
8486
if (StartLoc.isInvalid())
8587
return;
8688
checkStmt(Result, S->getBody(), StartLoc);
@@ -89,12 +91,14 @@ void BracesAroundStatementsCheck::check(
8991
if (S->isConsteval())
9092
return;
9193

92-
SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
94+
const SourceLocation StartLoc =
95+
findRParenLoc(S, SM, Context->getLangOpts());
9396
if (StartLoc.isInvalid())
9497
return;
9598
if (ForceBracesStmts.erase(S))
9699
ForceBracesStmts.insert(S->getThen());
97-
bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
100+
const bool BracedIf =
101+
checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
98102
const Stmt *Else = S->getElse();
99103
if (Else && BracedIf)
100104
ForceBracesStmts.insert(Else);
@@ -125,15 +129,15 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
125129
return {};
126130
}
127131

128-
SourceLocation PastCondEndLoc =
132+
const SourceLocation PastCondEndLoc =
129133
Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, LangOpts);
130134
if (PastCondEndLoc.isInvalid())
131135
return {};
132136
SourceLocation RParenLoc =
133137
forwardSkipWhitespaceAndComments(PastCondEndLoc, SM, LangOpts);
134138
if (RParenLoc.isInvalid())
135139
return {};
136-
tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
140+
const tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
137141
if (TokKind != tok::r_paren)
138142
return {};
139143
return RParenLoc;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ findConstToRemove(const FunctionDecl *Def,
3232
// written in the source (for out-of-line declarations). A FunctionDecl's
3333
// "location" is the start of its name, so, when the name is unqualified, we
3434
// use `getLocation()`.
35-
SourceLocation NameBeginLoc = Def->getQualifier()
36-
? Def->getQualifierLoc().getBeginLoc()
37-
: Def->getLocation();
35+
const SourceLocation NameBeginLoc = Def->getQualifier()
36+
? Def->getQualifierLoc().getBeginLoc()
37+
: Def->getLocation();
3838
// Since either of the locs can be in a macro, use `makeFileCharRange` to be
3939
// sure that we have a consistent `CharSourceRange`, located entirely in the
4040
// source file.
41-
CharSourceRange FileRange = Lexer::makeFileCharRange(
41+
const CharSourceRange FileRange = Lexer::makeFileCharRange(
4242
CharSourceRange::getCharRange(Def->getBeginLoc(), NameBeginLoc),
4343
*Result.SourceManager, Result.Context->getLangOpts());
4444

@@ -118,12 +118,12 @@ void ConstReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
118118
(Def->getBeginLoc().isMacroID() || Def->getEndLoc().isMacroID()))
119119
return;
120120

121-
CheckResult CR = checkDef(Def, Result);
121+
const CheckResult CR = checkDef(Def, Result);
122122
{
123123
// Clang only supports one in-flight diagnostic at a time. So, delimit the
124124
// scope of `Diagnostic` to allow further diagnostics after the scope. We
125125
// use `getInnerLocStart` to get the start of the return type.
126-
DiagnosticBuilder Diagnostic =
126+
const DiagnosticBuilder Diagnostic =
127127
diag(Def->getInnerLocStart(),
128128
"return type %0 is 'const'-qualified at the top level, which may "
129129
"reduce code readability without improving const correctness")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
110110
Result.Nodes.getNodeAs<Expr>("negativeComparison");
111111
assert((!PositiveComparison || !NegativeComparison) &&
112112
"only one of PositiveComparison or NegativeComparison should be set");
113-
bool Negated = NegativeComparison != nullptr;
113+
const bool Negated = NegativeComparison != nullptr;
114114
const auto *Comparison = Negated ? NegativeComparison : PositiveComparison;
115115
const StringRef ContainsFunName =
116116
Result.Nodes.getNodeAs<CXXMethodDecl>("contains_fun")->getName();
@@ -121,7 +121,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
121121
<< ContainsFunName;
122122

123123
// Don't fix it if it's in a macro invocation. Leave fixing it to the user.
124-
SourceLocation FuncCallLoc = Comparison->getEndLoc();
124+
const SourceLocation FuncCallLoc = Comparison->getEndLoc();
125125
if (!FuncCallLoc.isValid() || FuncCallLoc.isMacroID())
126126
return;
127127

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
101101
else if (ACE)
102102
CE = ACE;
103103

104-
SourceRange SrcRange = CE->getSourceRange();
104+
const SourceRange SrcRange = CE->getSourceRange();
105105

106106
std::string ReplacementText{
107107
Lexer::getSourceText(CharSourceRange::getTokenRange(SrcRange),
@@ -116,7 +116,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
116116
else
117117
ReplacementText += ".data()";
118118

119-
FixItHint Hint =
119+
const FixItHint Hint =
120120
FixItHint::CreateReplacement(UO->getSourceRange(), ReplacementText);
121121
diag(UO->getBeginLoc(),
122122
"'data' should be used for accessing the data pointer instead of taking "

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
119119
const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
120120
assert(FTL);
121121

122-
SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
123-
FTL.getLocalRangeEnd()};
122+
const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
123+
FTL.getLocalRangeEnd()};
124124
// Inside Range, there might be other keywords and trailing return types.
125125
// Find the exact position of "const".
126-
StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
127-
size_t Offset = Text.find("const");
126+
const StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
127+
const size_t Offset = Text.find("const");
128128
if (Offset == StringRef::npos)
129129
return {};
130130

131-
SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
131+
const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
132132
return {Start, Start.getLocWithOffset(strlen("const") - 1)};
133133
}
134134

@@ -138,7 +138,7 @@ void ConvertMemberFunctionsToStaticCheck::check(
138138

139139
// TODO: For out-of-line declarations, don't modify the source if the header
140140
// is excluded by the -header-filter option.
141-
DiagnosticBuilder Diag =
141+
const DiagnosticBuilder Diag =
142142
diag(Definition->getLocation(), "method %0 can be made static")
143143
<< Definition;
144144

@@ -153,15 +153,15 @@ void ConvertMemberFunctionsToStaticCheck::check(
153153
if (Definition->isConst()) {
154154
// Make sure that we either remove 'const' on both declaration and
155155
// definition or emit no fix-it at all.
156-
SourceRange DefConst = getLocationOfConst(Definition->getTypeSourceInfo(),
157-
*Result.SourceManager,
158-
Result.Context->getLangOpts());
156+
const SourceRange DefConst = getLocationOfConst(
157+
Definition->getTypeSourceInfo(), *Result.SourceManager,
158+
Result.Context->getLangOpts());
159159

160160
if (DefConst.isInvalid())
161161
return;
162162

163163
if (Declaration != Definition) {
164-
SourceRange DeclConst = getLocationOfConst(
164+
const SourceRange DeclConst = getLocationOfConst(
165165
Declaration->getTypeSourceInfo(), *Result.SourceManager,
166166
Result.Context->getLangOpts());
167167

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ void DuplicateIncludeCallbacks::InclusionDirective(
8888
if (llvm::is_contained(Files.back(), FileName)) {
8989
// We want to delete the entire line, so make sure that [Start,End] covers
9090
// everything.
91-
SourceLocation Start =
91+
const SourceLocation Start =
9292
advanceBeyondCurrentLine(SM, HashLoc, -1).getLocWithOffset(-1);
93-
SourceLocation End =
93+
const SourceLocation End =
9494
advanceBeyondCurrentLine(SM, FilenameRange.getEnd(), 1);
9595
Check.diag(HashLoc, "duplicate include")
9696
<< FixItHint::CreateRemoval(SourceRange{Start, End});

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,21 @@ static void removeElseAndBrackets(DiagnosticBuilder &Diag, ASTContext &Context,
124124

125125
if (const auto *CS = dyn_cast<CompoundStmt>(Else)) {
126126
Diag << tooling::fixit::createRemoval(ElseLoc);
127-
SourceLocation LBrace = CS->getLBracLoc();
128-
SourceLocation RBrace = CS->getRBracLoc();
129-
SourceLocation RangeStart =
127+
const SourceLocation LBrace = CS->getLBracLoc();
128+
const SourceLocation RBrace = CS->getRBracLoc();
129+
const SourceLocation RangeStart =
130130
Remap(LBrace).getLocWithOffset(TokLen(LBrace) + 1);
131-
SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);
131+
const SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);
132132

133-
llvm::StringRef Repl = Lexer::getSourceText(
133+
const llvm::StringRef Repl = Lexer::getSourceText(
134134
CharSourceRange::getTokenRange(RangeStart, RangeEnd),
135135
Context.getSourceManager(), Context.getLangOpts());
136136
Diag << tooling::fixit::createReplacement(CS->getSourceRange(), Repl);
137137
} else {
138-
SourceLocation ElseExpandedLoc = Remap(ElseLoc);
139-
SourceLocation EndLoc = Remap(Else->getEndLoc());
138+
const SourceLocation ElseExpandedLoc = Remap(ElseLoc);
139+
const SourceLocation EndLoc = Remap(Else->getEndLoc());
140140

141-
llvm::StringRef Repl = Lexer::getSourceText(
141+
const llvm::StringRef Repl = Lexer::getSourceText(
142142
CharSourceRange::getTokenRange(
143143
ElseExpandedLoc.getLocWithOffset(TokLen(ElseLoc) + 1), EndLoc),
144144
Context.getSourceManager(), Context.getLangOpts());
@@ -186,8 +186,8 @@ static bool hasPreprocessorBranchEndBetweenLocations(
186186
const ElseAfterReturnCheck::ConditionalBranchMap &ConditionalBranchMap,
187187
const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLoc) {
188188

189-
SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
190-
SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
189+
const SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
190+
const SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
191191
if (!SM.isWrittenInSameFile(ExpandedStartLoc, ExpandedEndLoc))
192192
return false;
193193

@@ -239,14 +239,14 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
239239
const auto *Else = Result.Nodes.getNodeAs<Stmt>("else");
240240
const auto *OuterScope = Result.Nodes.getNodeAs<CompoundStmt>("cs");
241241
const auto *Interrupt = Result.Nodes.getNodeAs<Stmt>(InterruptingStr);
242-
SourceLocation ElseLoc = If->getElseLoc();
242+
const SourceLocation ElseLoc = If->getElseLoc();
243243

244244
if (hasPreprocessorBranchEndBetweenLocations(
245245
PPConditionals, *Result.SourceManager, Interrupt->getBeginLoc(),
246246
ElseLoc))
247247
return;
248248

249-
bool IsLastInScope = OuterScope->body_back() == If;
249+
const bool IsLastInScope = OuterScope->body_back() == If;
250250
const StringRef ControlFlowInterrupter = getControlFlowString(*Interrupt);
251251

252252
if (!IsLastInScope && containsDeclInScope(Else)) {
@@ -276,7 +276,7 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
276276
}
277277
const DeclStmt *VDeclStmt = If->getConditionVariableDeclStmt();
278278
const VarDecl *VDecl = If->getConditionVariable();
279-
std::string Repl =
279+
const std::string Repl =
280280
(tooling::fixit::getText(*VDeclStmt, *Result.Context) +
281281
llvm::StringRef(";\n") +
282282
tooling::fixit::getText(If->getIfLoc(), *Result.Context))

0 commit comments

Comments
 (0)