-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[clang-tidy][NFC] Fix misc-const-correctness warnings (8/N) #167123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+151
−144
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang-tidy Author: Baranov Victor (vbvictor) ChangesPatch is 54.13 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/167123.diff 22 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
index 02fe913ee7918..4031b73da3f7f 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
@@ -27,7 +27,7 @@ static std::optional<Token>
findConstToRemove(const ParmVarDecl &Param,
const MatchFinder::MatchResult &Result) {
- CharSourceRange FileRange = Lexer::makeFileCharRange(
+ const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(getTypeRange(Param)),
*Result.SourceManager, Result.Context->getLangOpts());
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
index 40a4fa114681e..2b31281bb4a63 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
@@ -47,9 +47,9 @@ void AvoidReturnWithVoidValueCheck::check(
Result.Nodes.getNodeAs<CompoundStmt>("compound_parent");
if (!StrictMode && !SurroundingBlock)
return;
- DiagnosticBuilder Diag = diag(VoidReturn->getBeginLoc(),
- "return statement within a void function "
- "should not have a specified return value");
+ const DiagnosticBuilder Diag = diag(
+ VoidReturn->getBeginLoc(), "return statement within a void function "
+ "should not have a specified return value");
const SourceLocation SemicolonPos = utils::lexer::findNextTerminator(
VoidReturn->getEndLoc(), *Result.SourceManager, getLangOpts());
if (SemicolonPos.isInvalid())
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp b/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp
index c53c70667dbbc..631bb14753163 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp
@@ -40,7 +40,7 @@ struct AvoidUnconditionalPreprocessorIfPPCallbacks : public PPCallbacks {
bool isImmutable(SourceManager &SM, const LangOptions &LangOpts,
SourceRange ConditionRange) {
- SourceLocation Loc = ConditionRange.getBegin();
+ const SourceLocation Loc = ConditionRange.getBegin();
if (Loc.isMacroID())
return false;
diff --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
index 1952e14d1fc3d..2b55bb819da9c 100644
--- a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
@@ -20,7 +20,8 @@ namespace clang::tidy::readability {
static tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM,
const LangOptions &LangOpts) {
Token Tok;
- SourceLocation Beginning = Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
+ const SourceLocation Beginning =
+ Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
const bool Invalid = Lexer::getRawToken(Beginning, Tok, SM, LangOpts);
assert(!Invalid && "Expected a valid token.");
@@ -38,7 +39,7 @@ forwardSkipWhitespaceAndComments(SourceLocation Loc, const SourceManager &SM,
while (isWhitespace(*SM.getCharacterData(Loc)))
Loc = Loc.getLocWithOffset(1);
- tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
+ const tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
if (TokKind != tok::comment)
return Loc;
@@ -80,7 +81,8 @@ void BracesAroundStatementsCheck::check(
} else if (const auto *S = Result.Nodes.getNodeAs<DoStmt>("do")) {
checkStmt(Result, S->getBody(), S->getDoLoc(), S->getWhileLoc());
} else if (const auto *S = Result.Nodes.getNodeAs<WhileStmt>("while")) {
- SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
+ const SourceLocation StartLoc =
+ findRParenLoc(S, SM, Context->getLangOpts());
if (StartLoc.isInvalid())
return;
checkStmt(Result, S->getBody(), StartLoc);
@@ -89,12 +91,14 @@ void BracesAroundStatementsCheck::check(
if (S->isConsteval())
return;
- SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
+ const SourceLocation StartLoc =
+ findRParenLoc(S, SM, Context->getLangOpts());
if (StartLoc.isInvalid())
return;
if (ForceBracesStmts.erase(S))
ForceBracesStmts.insert(S->getThen());
- bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
+ const bool BracedIf =
+ checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
const Stmt *Else = S->getElse();
if (Else && BracedIf)
ForceBracesStmts.insert(Else);
@@ -125,7 +129,7 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
return {};
}
- SourceLocation PastCondEndLoc =
+ const SourceLocation PastCondEndLoc =
Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, LangOpts);
if (PastCondEndLoc.isInvalid())
return {};
@@ -133,7 +137,7 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
forwardSkipWhitespaceAndComments(PastCondEndLoc, SM, LangOpts);
if (RParenLoc.isInvalid())
return {};
- tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
+ const tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
if (TokKind != tok::r_paren)
return {};
return RParenLoc;
diff --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
index 6ccd933ff4c21..cfdf0e9c4a331 100644
--- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
@@ -32,13 +32,13 @@ findConstToRemove(const FunctionDecl *Def,
// written in the source (for out-of-line declarations). A FunctionDecl's
// "location" is the start of its name, so, when the name is unqualified, we
// use `getLocation()`.
- SourceLocation NameBeginLoc = Def->getQualifier()
- ? Def->getQualifierLoc().getBeginLoc()
- : Def->getLocation();
+ const SourceLocation NameBeginLoc = Def->getQualifier()
+ ? Def->getQualifierLoc().getBeginLoc()
+ : Def->getLocation();
// Since either of the locs can be in a macro, use `makeFileCharRange` to be
// sure that we have a consistent `CharSourceRange`, located entirely in the
// source file.
- CharSourceRange FileRange = Lexer::makeFileCharRange(
+ const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getCharRange(Def->getBeginLoc(), NameBeginLoc),
*Result.SourceManager, Result.Context->getLangOpts());
@@ -118,12 +118,12 @@ void ConstReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
(Def->getBeginLoc().isMacroID() || Def->getEndLoc().isMacroID()))
return;
- CheckResult CR = checkDef(Def, Result);
+ const CheckResult CR = checkDef(Def, Result);
{
// Clang only supports one in-flight diagnostic at a time. So, delimit the
// scope of `Diagnostic` to allow further diagnostics after the scope. We
// use `getInnerLocStart` to get the start of the return type.
- DiagnosticBuilder Diagnostic =
+ const DiagnosticBuilder Diagnostic =
diag(Def->getInnerLocStart(),
"return type %0 is 'const'-qualified at the top level, which may "
"reduce code readability without improving const correctness")
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
index 850ef86c85b17..a3405524553d4 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
@@ -110,7 +110,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
Result.Nodes.getNodeAs<Expr>("negativeComparison");
assert((!PositiveComparison || !NegativeComparison) &&
"only one of PositiveComparison or NegativeComparison should be set");
- bool Negated = NegativeComparison != nullptr;
+ const bool Negated = NegativeComparison != nullptr;
const auto *Comparison = Negated ? NegativeComparison : PositiveComparison;
const StringRef ContainsFunName =
Result.Nodes.getNodeAs<CXXMethodDecl>("contains_fun")->getName();
@@ -121,7 +121,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
<< ContainsFunName;
// Don't fix it if it's in a macro invocation. Leave fixing it to the user.
- SourceLocation FuncCallLoc = Comparison->getEndLoc();
+ const SourceLocation FuncCallLoc = Comparison->getEndLoc();
if (!FuncCallLoc.isValid() || FuncCallLoc.isMacroID())
return;
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
index 11756d10a8221..a5a6e3ce7af01 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
@@ -101,7 +101,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
else if (ACE)
CE = ACE;
- SourceRange SrcRange = CE->getSourceRange();
+ const SourceRange SrcRange = CE->getSourceRange();
std::string ReplacementText{
Lexer::getSourceText(CharSourceRange::getTokenRange(SrcRange),
@@ -116,7 +116,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
else
ReplacementText += ".data()";
- FixItHint Hint =
+ const FixItHint Hint =
FixItHint::CreateReplacement(UO->getSourceRange(), ReplacementText);
diag(UO->getBeginLoc(),
"'data' should be used for accessing the data pointer instead of taking "
diff --git a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
index 6da4cf7c6bf94..24fd08b28857a 100644
--- a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
@@ -118,16 +118,16 @@ static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
assert(FTL);
- SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
- FTL.getLocalRangeEnd()};
+ const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
+ FTL.getLocalRangeEnd()};
// Inside Range, there might be other keywords and trailing return types.
// Find the exact position of "const".
- StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
- size_t Offset = Text.find("const");
+ const StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
+ const size_t Offset = Text.find("const");
if (Offset == StringRef::npos)
return {};
- SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
+ const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
return {Start, Start.getLocWithOffset(strlen("const") - 1)};
}
@@ -137,7 +137,7 @@ void ConvertMemberFunctionsToStatic::check(
// TODO: For out-of-line declarations, don't modify the source if the header
// is excluded by the -header-filter option.
- DiagnosticBuilder Diag =
+ const DiagnosticBuilder Diag =
diag(Definition->getLocation(), "method %0 can be made static")
<< Definition;
@@ -152,15 +152,15 @@ void ConvertMemberFunctionsToStatic::check(
if (Definition->isConst()) {
// Make sure that we either remove 'const' on both declaration and
// definition or emit no fix-it at all.
- SourceRange DefConst = getLocationOfConst(Definition->getTypeSourceInfo(),
- *Result.SourceManager,
- Result.Context->getLangOpts());
+ const SourceRange DefConst = getLocationOfConst(
+ Definition->getTypeSourceInfo(), *Result.SourceManager,
+ Result.Context->getLangOpts());
if (DefConst.isInvalid())
return;
if (Declaration != Definition) {
- SourceRange DeclConst = getLocationOfConst(
+ const SourceRange DeclConst = getLocationOfConst(
Declaration->getTypeSourceInfo(), *Result.SourceManager,
Result.Context->getLangOpts());
diff --git a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
index 0237c057afed5..cc9ae471a926d 100644
--- a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
@@ -88,9 +88,9 @@ void DuplicateIncludeCallbacks::InclusionDirective(
if (llvm::is_contained(Files.back(), FileName)) {
// We want to delete the entire line, so make sure that [Start,End] covers
// everything.
- SourceLocation Start =
+ const SourceLocation Start =
advanceBeyondCurrentLine(SM, HashLoc, -1).getLocWithOffset(-1);
- SourceLocation End =
+ const SourceLocation End =
advanceBeyondCurrentLine(SM, FilenameRange.getEnd(), 1);
Check.diag(HashLoc, "duplicate include")
<< FixItHint::CreateRemoval(SourceRange{Start, End});
diff --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
index 6399e7d99a9c7..a420c5653cfe8 100644
--- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
@@ -124,21 +124,21 @@ static void removeElseAndBrackets(DiagnosticBuilder &Diag, ASTContext &Context,
if (const auto *CS = dyn_cast<CompoundStmt>(Else)) {
Diag << tooling::fixit::createRemoval(ElseLoc);
- SourceLocation LBrace = CS->getLBracLoc();
- SourceLocation RBrace = CS->getRBracLoc();
- SourceLocation RangeStart =
+ const SourceLocation LBrace = CS->getLBracLoc();
+ const SourceLocation RBrace = CS->getRBracLoc();
+ const SourceLocation RangeStart =
Remap(LBrace).getLocWithOffset(TokLen(LBrace) + 1);
- SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);
+ const SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);
- llvm::StringRef Repl = Lexer::getSourceText(
+ const llvm::StringRef Repl = Lexer::getSourceText(
CharSourceRange::getTokenRange(RangeStart, RangeEnd),
Context.getSourceManager(), Context.getLangOpts());
Diag << tooling::fixit::createReplacement(CS->getSourceRange(), Repl);
} else {
- SourceLocation ElseExpandedLoc = Remap(ElseLoc);
- SourceLocation EndLoc = Remap(Else->getEndLoc());
+ const SourceLocation ElseExpandedLoc = Remap(ElseLoc);
+ const SourceLocation EndLoc = Remap(Else->getEndLoc());
- llvm::StringRef Repl = Lexer::getSourceText(
+ const llvm::StringRef Repl = Lexer::getSourceText(
CharSourceRange::getTokenRange(
ElseExpandedLoc.getLocWithOffset(TokLen(ElseLoc) + 1), EndLoc),
Context.getSourceManager(), Context.getLangOpts());
@@ -186,8 +186,8 @@ static bool hasPreprocessorBranchEndBetweenLocations(
const ElseAfterReturnCheck::ConditionalBranchMap &ConditionalBranchMap,
const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLoc) {
- SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
- SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
+ const SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
+ const SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
if (!SM.isWrittenInSameFile(ExpandedStartLoc, ExpandedEndLoc))
return false;
@@ -239,14 +239,14 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Else = Result.Nodes.getNodeAs<Stmt>("else");
const auto *OuterScope = Result.Nodes.getNodeAs<CompoundStmt>("cs");
const auto *Interrupt = Result.Nodes.getNodeAs<Stmt>(InterruptingStr);
- SourceLocation ElseLoc = If->getElseLoc();
+ const SourceLocation ElseLoc = If->getElseLoc();
if (hasPreprocessorBranchEndBetweenLocations(
PPConditionals, *Result.SourceManager, Interrupt->getBeginLoc(),
ElseLoc))
return;
- bool IsLastInScope = OuterScope->body_back() == If;
+ const bool IsLastInScope = OuterScope->body_back() == If;
const StringRef ControlFlowInterrupter = getControlFlowString(*Interrupt);
if (!IsLastInScope && containsDeclInScope(Else)) {
@@ -276,7 +276,7 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
}
const DeclStmt *VDeclStmt = If->getConditionVariableDeclStmt();
const VarDecl *VDecl = If->getConditionVariable();
- std::string Repl =
+ const std::string Repl =
(tooling::fixit::getText(*VDeclStmt, *Result.Context) +
llvm::StringRef(";\n") +
tooling::fixit::getText(If->getIfLoc(), *Result.Context))
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
index a2a5c3e10ee07..049ad759b834c 100644
--- a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -75,7 +75,7 @@ static void cleanInitialValue(DiagnosticBuilder &Diag,
namespace {
AST_MATCHER(EnumDecl, isMacro) {
- SourceLocation Loc = Node.getBeginLoc();
+ const SourceLocation Loc = Node.getBeginLoc();
return Loc.isMacroID();
}
@@ -165,7 +165,7 @@ void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("inconsistent")) {
- DiagnosticBuilder Diag =
+ const DiagnosticBuilder Diag =
diag(
Enum->getBeginLoc(),
"initial values in enum '%0' are not consistent, consider explicit "
diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
index 4791df037d77d..ccac645892948 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
@@ -229,14 +229,14 @@ class FunctionASTVisitor final
bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) {
++CurrentNestingLevel;
- bool ShouldContinue = Base::TraverseStmt(Node);
+ const bool ShouldContinue = Base::TraverseStmt(Node);
--CurrentNestingLevel;
return ShouldContinue;
}
bool traverseDeclWithIncreasedNestingLevel(Decl *Node) {
++CurrentNestingLevel;
- bool ShouldContinue = Base::TraverseDecl(Node);
+ const bool ShouldContinue = Base::TraverseDecl(Node);
--CurrentNestingLevel;
return ShouldContinue;
}
@@ -336,7 +336,7 @@ class FunctionASTVisitor final
// Record the operator that we are currently processing and traverse it.
CurrentBinaryOperator = Op->getOpcode();
- bool ShouldContinue = Base::TraverseBinaryOperator(Op);
+ const bool ShouldContinue = Base::TraverseBinaryOperator(Op);
// And restore the previous binary operator, which might be nonexistent.
CurrentBinaryOperator = BinOpCopy;
@@ -354,7 +354,7 @@ class FunctionASTVisitor final
// Else, do add [uninitialized] frame to the stack, and traverse call.
BinaryOperatorsStack.emplace();
- bool ShouldContinue = Base::TraverseCallExpr(Node);
+ const bool ShouldContinue = Base::TraverseCallExpr(Node);
// And remove the top frame.
...
[truncated]
|
Member
|
@llvm/pr-subscribers-clang-tools-extra Author: Baranov Victor (vbvictor) ChangesPatch is 54.13 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/167123.diff 22 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
index 02fe913ee7918..4031b73da3f7f 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
@@ -27,7 +27,7 @@ static std::optional<Token>
findConstToRemove(const ParmVarDecl &Param,
const MatchFinder::MatchResult &Result) {
- CharSourceRange FileRange = Lexer::makeFileCharRange(
+ const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(getTypeRange(Param)),
*Result.SourceManager, Result.Context->getLangOpts());
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
index 40a4fa114681e..2b31281bb4a63 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidReturnWithVoidValueCheck.cpp
@@ -47,9 +47,9 @@ void AvoidReturnWithVoidValueCheck::check(
Result.Nodes.getNodeAs<CompoundStmt>("compound_parent");
if (!StrictMode && !SurroundingBlock)
return;
- DiagnosticBuilder Diag = diag(VoidReturn->getBeginLoc(),
- "return statement within a void function "
- "should not have a specified return value");
+ const DiagnosticBuilder Diag = diag(
+ VoidReturn->getBeginLoc(), "return statement within a void function "
+ "should not have a specified return value");
const SourceLocation SemicolonPos = utils::lexer::findNextTerminator(
VoidReturn->getEndLoc(), *Result.SourceManager, getLangOpts());
if (SemicolonPos.isInvalid())
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp b/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp
index c53c70667dbbc..631bb14753163 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp
@@ -40,7 +40,7 @@ struct AvoidUnconditionalPreprocessorIfPPCallbacks : public PPCallbacks {
bool isImmutable(SourceManager &SM, const LangOptions &LangOpts,
SourceRange ConditionRange) {
- SourceLocation Loc = ConditionRange.getBegin();
+ const SourceLocation Loc = ConditionRange.getBegin();
if (Loc.isMacroID())
return false;
diff --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
index 1952e14d1fc3d..2b55bb819da9c 100644
--- a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
@@ -20,7 +20,8 @@ namespace clang::tidy::readability {
static tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM,
const LangOptions &LangOpts) {
Token Tok;
- SourceLocation Beginning = Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
+ const SourceLocation Beginning =
+ Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
const bool Invalid = Lexer::getRawToken(Beginning, Tok, SM, LangOpts);
assert(!Invalid && "Expected a valid token.");
@@ -38,7 +39,7 @@ forwardSkipWhitespaceAndComments(SourceLocation Loc, const SourceManager &SM,
while (isWhitespace(*SM.getCharacterData(Loc)))
Loc = Loc.getLocWithOffset(1);
- tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
+ const tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
if (TokKind != tok::comment)
return Loc;
@@ -80,7 +81,8 @@ void BracesAroundStatementsCheck::check(
} else if (const auto *S = Result.Nodes.getNodeAs<DoStmt>("do")) {
checkStmt(Result, S->getBody(), S->getDoLoc(), S->getWhileLoc());
} else if (const auto *S = Result.Nodes.getNodeAs<WhileStmt>("while")) {
- SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
+ const SourceLocation StartLoc =
+ findRParenLoc(S, SM, Context->getLangOpts());
if (StartLoc.isInvalid())
return;
checkStmt(Result, S->getBody(), StartLoc);
@@ -89,12 +91,14 @@ void BracesAroundStatementsCheck::check(
if (S->isConsteval())
return;
- SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
+ const SourceLocation StartLoc =
+ findRParenLoc(S, SM, Context->getLangOpts());
if (StartLoc.isInvalid())
return;
if (ForceBracesStmts.erase(S))
ForceBracesStmts.insert(S->getThen());
- bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
+ const bool BracedIf =
+ checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
const Stmt *Else = S->getElse();
if (Else && BracedIf)
ForceBracesStmts.insert(Else);
@@ -125,7 +129,7 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
return {};
}
- SourceLocation PastCondEndLoc =
+ const SourceLocation PastCondEndLoc =
Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, LangOpts);
if (PastCondEndLoc.isInvalid())
return {};
@@ -133,7 +137,7 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
forwardSkipWhitespaceAndComments(PastCondEndLoc, SM, LangOpts);
if (RParenLoc.isInvalid())
return {};
- tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
+ const tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
if (TokKind != tok::r_paren)
return {};
return RParenLoc;
diff --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
index 6ccd933ff4c21..cfdf0e9c4a331 100644
--- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
@@ -32,13 +32,13 @@ findConstToRemove(const FunctionDecl *Def,
// written in the source (for out-of-line declarations). A FunctionDecl's
// "location" is the start of its name, so, when the name is unqualified, we
// use `getLocation()`.
- SourceLocation NameBeginLoc = Def->getQualifier()
- ? Def->getQualifierLoc().getBeginLoc()
- : Def->getLocation();
+ const SourceLocation NameBeginLoc = Def->getQualifier()
+ ? Def->getQualifierLoc().getBeginLoc()
+ : Def->getLocation();
// Since either of the locs can be in a macro, use `makeFileCharRange` to be
// sure that we have a consistent `CharSourceRange`, located entirely in the
// source file.
- CharSourceRange FileRange = Lexer::makeFileCharRange(
+ const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getCharRange(Def->getBeginLoc(), NameBeginLoc),
*Result.SourceManager, Result.Context->getLangOpts());
@@ -118,12 +118,12 @@ void ConstReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
(Def->getBeginLoc().isMacroID() || Def->getEndLoc().isMacroID()))
return;
- CheckResult CR = checkDef(Def, Result);
+ const CheckResult CR = checkDef(Def, Result);
{
// Clang only supports one in-flight diagnostic at a time. So, delimit the
// scope of `Diagnostic` to allow further diagnostics after the scope. We
// use `getInnerLocStart` to get the start of the return type.
- DiagnosticBuilder Diagnostic =
+ const DiagnosticBuilder Diagnostic =
diag(Def->getInnerLocStart(),
"return type %0 is 'const'-qualified at the top level, which may "
"reduce code readability without improving const correctness")
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
index 850ef86c85b17..a3405524553d4 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
@@ -110,7 +110,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
Result.Nodes.getNodeAs<Expr>("negativeComparison");
assert((!PositiveComparison || !NegativeComparison) &&
"only one of PositiveComparison or NegativeComparison should be set");
- bool Negated = NegativeComparison != nullptr;
+ const bool Negated = NegativeComparison != nullptr;
const auto *Comparison = Negated ? NegativeComparison : PositiveComparison;
const StringRef ContainsFunName =
Result.Nodes.getNodeAs<CXXMethodDecl>("contains_fun")->getName();
@@ -121,7 +121,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
<< ContainsFunName;
// Don't fix it if it's in a macro invocation. Leave fixing it to the user.
- SourceLocation FuncCallLoc = Comparison->getEndLoc();
+ const SourceLocation FuncCallLoc = Comparison->getEndLoc();
if (!FuncCallLoc.isValid() || FuncCallLoc.isMacroID())
return;
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
index 11756d10a8221..a5a6e3ce7af01 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
@@ -101,7 +101,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
else if (ACE)
CE = ACE;
- SourceRange SrcRange = CE->getSourceRange();
+ const SourceRange SrcRange = CE->getSourceRange();
std::string ReplacementText{
Lexer::getSourceText(CharSourceRange::getTokenRange(SrcRange),
@@ -116,7 +116,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
else
ReplacementText += ".data()";
- FixItHint Hint =
+ const FixItHint Hint =
FixItHint::CreateReplacement(UO->getSourceRange(), ReplacementText);
diag(UO->getBeginLoc(),
"'data' should be used for accessing the data pointer instead of taking "
diff --git a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
index 6da4cf7c6bf94..24fd08b28857a 100644
--- a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
@@ -118,16 +118,16 @@ static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
assert(FTL);
- SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
- FTL.getLocalRangeEnd()};
+ const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
+ FTL.getLocalRangeEnd()};
// Inside Range, there might be other keywords and trailing return types.
// Find the exact position of "const".
- StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
- size_t Offset = Text.find("const");
+ const StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
+ const size_t Offset = Text.find("const");
if (Offset == StringRef::npos)
return {};
- SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
+ const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
return {Start, Start.getLocWithOffset(strlen("const") - 1)};
}
@@ -137,7 +137,7 @@ void ConvertMemberFunctionsToStatic::check(
// TODO: For out-of-line declarations, don't modify the source if the header
// is excluded by the -header-filter option.
- DiagnosticBuilder Diag =
+ const DiagnosticBuilder Diag =
diag(Definition->getLocation(), "method %0 can be made static")
<< Definition;
@@ -152,15 +152,15 @@ void ConvertMemberFunctionsToStatic::check(
if (Definition->isConst()) {
// Make sure that we either remove 'const' on both declaration and
// definition or emit no fix-it at all.
- SourceRange DefConst = getLocationOfConst(Definition->getTypeSourceInfo(),
- *Result.SourceManager,
- Result.Context->getLangOpts());
+ const SourceRange DefConst = getLocationOfConst(
+ Definition->getTypeSourceInfo(), *Result.SourceManager,
+ Result.Context->getLangOpts());
if (DefConst.isInvalid())
return;
if (Declaration != Definition) {
- SourceRange DeclConst = getLocationOfConst(
+ const SourceRange DeclConst = getLocationOfConst(
Declaration->getTypeSourceInfo(), *Result.SourceManager,
Result.Context->getLangOpts());
diff --git a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
index 0237c057afed5..cc9ae471a926d 100644
--- a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
@@ -88,9 +88,9 @@ void DuplicateIncludeCallbacks::InclusionDirective(
if (llvm::is_contained(Files.back(), FileName)) {
// We want to delete the entire line, so make sure that [Start,End] covers
// everything.
- SourceLocation Start =
+ const SourceLocation Start =
advanceBeyondCurrentLine(SM, HashLoc, -1).getLocWithOffset(-1);
- SourceLocation End =
+ const SourceLocation End =
advanceBeyondCurrentLine(SM, FilenameRange.getEnd(), 1);
Check.diag(HashLoc, "duplicate include")
<< FixItHint::CreateRemoval(SourceRange{Start, End});
diff --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
index 6399e7d99a9c7..a420c5653cfe8 100644
--- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
@@ -124,21 +124,21 @@ static void removeElseAndBrackets(DiagnosticBuilder &Diag, ASTContext &Context,
if (const auto *CS = dyn_cast<CompoundStmt>(Else)) {
Diag << tooling::fixit::createRemoval(ElseLoc);
- SourceLocation LBrace = CS->getLBracLoc();
- SourceLocation RBrace = CS->getRBracLoc();
- SourceLocation RangeStart =
+ const SourceLocation LBrace = CS->getLBracLoc();
+ const SourceLocation RBrace = CS->getRBracLoc();
+ const SourceLocation RangeStart =
Remap(LBrace).getLocWithOffset(TokLen(LBrace) + 1);
- SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);
+ const SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);
- llvm::StringRef Repl = Lexer::getSourceText(
+ const llvm::StringRef Repl = Lexer::getSourceText(
CharSourceRange::getTokenRange(RangeStart, RangeEnd),
Context.getSourceManager(), Context.getLangOpts());
Diag << tooling::fixit::createReplacement(CS->getSourceRange(), Repl);
} else {
- SourceLocation ElseExpandedLoc = Remap(ElseLoc);
- SourceLocation EndLoc = Remap(Else->getEndLoc());
+ const SourceLocation ElseExpandedLoc = Remap(ElseLoc);
+ const SourceLocation EndLoc = Remap(Else->getEndLoc());
- llvm::StringRef Repl = Lexer::getSourceText(
+ const llvm::StringRef Repl = Lexer::getSourceText(
CharSourceRange::getTokenRange(
ElseExpandedLoc.getLocWithOffset(TokLen(ElseLoc) + 1), EndLoc),
Context.getSourceManager(), Context.getLangOpts());
@@ -186,8 +186,8 @@ static bool hasPreprocessorBranchEndBetweenLocations(
const ElseAfterReturnCheck::ConditionalBranchMap &ConditionalBranchMap,
const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLoc) {
- SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
- SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
+ const SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
+ const SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
if (!SM.isWrittenInSameFile(ExpandedStartLoc, ExpandedEndLoc))
return false;
@@ -239,14 +239,14 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Else = Result.Nodes.getNodeAs<Stmt>("else");
const auto *OuterScope = Result.Nodes.getNodeAs<CompoundStmt>("cs");
const auto *Interrupt = Result.Nodes.getNodeAs<Stmt>(InterruptingStr);
- SourceLocation ElseLoc = If->getElseLoc();
+ const SourceLocation ElseLoc = If->getElseLoc();
if (hasPreprocessorBranchEndBetweenLocations(
PPConditionals, *Result.SourceManager, Interrupt->getBeginLoc(),
ElseLoc))
return;
- bool IsLastInScope = OuterScope->body_back() == If;
+ const bool IsLastInScope = OuterScope->body_back() == If;
const StringRef ControlFlowInterrupter = getControlFlowString(*Interrupt);
if (!IsLastInScope && containsDeclInScope(Else)) {
@@ -276,7 +276,7 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
}
const DeclStmt *VDeclStmt = If->getConditionVariableDeclStmt();
const VarDecl *VDecl = If->getConditionVariable();
- std::string Repl =
+ const std::string Repl =
(tooling::fixit::getText(*VDeclStmt, *Result.Context) +
llvm::StringRef(";\n") +
tooling::fixit::getText(If->getIfLoc(), *Result.Context))
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
index a2a5c3e10ee07..049ad759b834c 100644
--- a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -75,7 +75,7 @@ static void cleanInitialValue(DiagnosticBuilder &Diag,
namespace {
AST_MATCHER(EnumDecl, isMacro) {
- SourceLocation Loc = Node.getBeginLoc();
+ const SourceLocation Loc = Node.getBeginLoc();
return Loc.isMacroID();
}
@@ -165,7 +165,7 @@ void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("inconsistent")) {
- DiagnosticBuilder Diag =
+ const DiagnosticBuilder Diag =
diag(
Enum->getBeginLoc(),
"initial values in enum '%0' are not consistent, consider explicit "
diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
index 4791df037d77d..ccac645892948 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
@@ -229,14 +229,14 @@ class FunctionASTVisitor final
bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) {
++CurrentNestingLevel;
- bool ShouldContinue = Base::TraverseStmt(Node);
+ const bool ShouldContinue = Base::TraverseStmt(Node);
--CurrentNestingLevel;
return ShouldContinue;
}
bool traverseDeclWithIncreasedNestingLevel(Decl *Node) {
++CurrentNestingLevel;
- bool ShouldContinue = Base::TraverseDecl(Node);
+ const bool ShouldContinue = Base::TraverseDecl(Node);
--CurrentNestingLevel;
return ShouldContinue;
}
@@ -336,7 +336,7 @@ class FunctionASTVisitor final
// Record the operator that we are currently processing and traverse it.
CurrentBinaryOperator = Op->getOpcode();
- bool ShouldContinue = Base::TraverseBinaryOperator(Op);
+ const bool ShouldContinue = Base::TraverseBinaryOperator(Op);
// And restore the previous binary operator, which might be nonexistent.
CurrentBinaryOperator = BinOpCopy;
@@ -354,7 +354,7 @@ class FunctionASTVisitor final
// Else, do add [uninitialized] frame to the stack, and traverse call.
BinaryOperatorsStack.emplace();
- bool ShouldContinue = Base::TraverseCallExpr(Node);
+ const bool ShouldContinue = Base::TraverseCallExpr(Node);
// And remove the top frame.
...
[truncated]
|
localspook
approved these changes
Nov 8, 2025
EugeneZelenko
approved these changes
Nov 8, 2025
vinay-deshmukh
pushed a commit
to vinay-deshmukh/llvm-project
that referenced
this pull request
Nov 8, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.