[clang-tools-extra] [clang-tidy][NFC] Fix misc-const-correctness warnings (11/N) (PR #167128)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 8 03:42:08 PST 2025
https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/167128
None
>From 8c2d872b0ada153ca4327dbc4795c6597b4af616 Mon Sep 17 00:00:00 2001
From: Victor Baranov <bar.victor.2002 at gmail.com>
Date: Sat, 8 Nov 2025 14:41:14 +0300
Subject: [PATCH] [clang-tidy][NFC] Fix misc-const-correctness warnings (11/N)
---
.../modernize/UseDefaultMemberInitCheck.cpp | 10 ++--
.../UseDesignatedInitializersCheck.cpp | 2 +-
.../modernize/UseEqualsDefaultCheck.cpp | 10 ++--
.../modernize/UseEqualsDeleteCheck.cpp | 2 +-
.../UseIntegerSignComparisonCheck.cpp | 2 +-
.../modernize/UseNodiscardCheck.cpp | 4 +-
.../clang-tidy/modernize/UseNoexceptCheck.cpp | 6 +--
.../clang-tidy/modernize/UseNullptrCheck.cpp | 27 +++++-----
.../clang-tidy/modernize/UseOverrideCheck.cpp | 31 +++++------
.../modernize/UseScopedLockCheck.cpp | 5 +-
.../modernize/UseStdNumbersCheck.cpp | 2 +-
.../modernize/UseTrailingReturnTypeCheck.cpp | 51 ++++++++++---------
.../modernize/UseTransparentFunctorsCheck.cpp | 6 +--
.../modernize/UseUncaughtExceptionsCheck.cpp | 4 +-
.../clang-tidy/modernize/UseUsingCheck.cpp | 7 +--
15 files changed, 87 insertions(+), 82 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
index 0d2c3a79b9ece..cc6b7bfd4fd5b 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -163,7 +163,7 @@ static bool isZero(const Expr *E) {
case Stmt::IntegerLiteralClass:
return !cast<IntegerLiteral>(E)->getValue();
case Stmt::FloatingLiteralClass: {
- llvm::APFloat Value = cast<FloatingLiteral>(E)->getValue();
+ const llvm::APFloat Value = cast<FloatingLiteral>(E)->getValue();
return Value.isZero() && !Value.isNegative();
}
default:
@@ -297,16 +297,16 @@ void UseDefaultMemberInitCheck::checkDefaultInit(
}) > 1)
return;
- SourceLocation StartLoc = Field->getBeginLoc();
+ const SourceLocation StartLoc = Field->getBeginLoc();
if (StartLoc.isMacroID() && IgnoreMacros)
return;
- SourceLocation FieldEnd =
+ const SourceLocation FieldEnd =
Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
*Result.SourceManager, getLangOpts());
- SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
+ const SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
Init->getLParenLoc(), 0, *Result.SourceManager, getLangOpts());
- CharSourceRange InitRange =
+ const CharSourceRange InitRange =
CharSourceRange::getCharRange(LParenEnd, Init->getRParenLoc());
const Expr *InitExpression = Init->getInit();
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
index c1094b1fc194a..2cc3ce1f7a686 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
@@ -152,7 +152,7 @@ void UseDesignatedInitializersCheck::check(
if (IgnoreMacros && InitList->getBeginLoc().isMacroID())
return;
{
- DiagnosticBuilder Diag =
+ const DiagnosticBuilder Diag =
diag(InitList->getLBraceLoc(),
"use designated initializer list to initialize %0");
Diag << InitList->getType() << InitList->getSourceRange();
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
index d6ddbb69f7b0d..fde9c7323ce3c 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
@@ -200,7 +200,7 @@ static bool isCopyAssignmentAndCanBeDefaulted(ASTContext *Context,
/// Returns false if the body has any non-whitespace character.
static bool bodyEmpty(const ASTContext *Context, const CompoundStmt *Body) {
bool Invalid = false;
- StringRef Text = Lexer::getSourceText(
+ const StringRef Text = Lexer::getSourceText(
CharSourceRange::getCharRange(Body->getLBracLoc().getLocWithOffset(1),
Body->getRBracLoc()),
Context->getSourceManager(), Context->getLangOpts(), &Invalid);
@@ -306,8 +306,8 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
return;
// If there are comments inside the body, don't do the change.
- bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() ||
- bodyEmpty(Result.Context, Body);
+ const bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() ||
+ bodyEmpty(Result.Context, Body);
std::vector<FixItHint> RemoveInitializers;
unsigned MemberType = 0;
@@ -345,14 +345,14 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
Diag << MemberType;
if (ApplyFix) {
- SourceLocation UnifiedEnd = utils::lexer::getUnifiedEndLoc(
+ const SourceLocation UnifiedEnd = utils::lexer::getUnifiedEndLoc(
*Body, Result.Context->getSourceManager(),
Result.Context->getLangOpts());
// Skipping comments, check for a semicolon after Body->getSourceRange()
std::optional<Token> Token = utils::lexer::findNextTokenSkippingComments(
UnifiedEnd, Result.Context->getSourceManager(),
Result.Context->getLangOpts());
- StringRef Replacement =
+ const StringRef Replacement =
Token && Token->is(tok::semi) ? "= default" : "= default;";
Diag << FixItHint::CreateReplacement(Body->getSourceRange(), Replacement)
<< RemoveInitializers;
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEqualsDeleteCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEqualsDeleteCheck.cpp
index ab2d41a52040e..a19d2ecdad88d 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseEqualsDeleteCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseEqualsDeleteCheck.cpp
@@ -74,7 +74,7 @@ void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Func =
Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
- SourceLocation EndLoc = Lexer::getLocForEndOfToken(
+ const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
if (IgnoreMacros && Func->getLocation().isMacroID())
diff --git a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp
index 35320e83c5d4b..574cbea46124b 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp
@@ -146,7 +146,7 @@ void UseIntegerSignComparisonCheck::check(
R3.setBegin(Lexer::getLocForEndOfToken(
SubExprRHS->getEndLoc(), 0, *Result.SourceManager, getLangOpts()));
}
- DiagnosticBuilder Diag =
+ const DiagnosticBuilder Diag =
diag(BinaryOp->getBeginLoc(),
"comparison between 'signed' and 'unsigned' integers");
StringRef CmpNamespace;
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
index d22c99335d9bb..7e8d98241118a 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
@@ -110,11 +110,11 @@ void UseNodiscardCheck::registerMatchers(MatchFinder *Finder) {
void UseNodiscardCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>("no_discard");
// Don't make replacements if the location is invalid or in a macro.
- SourceLocation Loc = MatchedDecl->getLocation();
+ const SourceLocation Loc = MatchedDecl->getLocation();
if (Loc.isInvalid() || Loc.isMacroID())
return;
- SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
+ const SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
ASTContext &Context = *Result.Context;
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
index d1388dc6298e4..6bd5485abbac9 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
@@ -81,12 +81,12 @@ void UseNoexceptCheck::check(const MatchFinder::MatchResult &Result) {
assert(Range.isValid() && "Exception Source Range is invalid.");
- CharSourceRange CRange = Lexer::makeFileCharRange(
+ const CharSourceRange CRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(Range), *Result.SourceManager,
Result.Context->getLangOpts());
- bool IsNoThrow = FnTy->isNothrow();
- StringRef ReplacementStr =
+ const bool IsNoThrow = FnTy->isNothrow();
+ const StringRef ReplacementStr =
IsNoThrow ? NoexceptMacro.empty() ? "noexcept" : NoexceptMacro
: NoexceptMacro.empty()
? (DtorOrOperatorDel || UseNoexceptFalse) ? "noexcept(false)" : ""
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
index b6834c69204c2..928a00775fe12 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
@@ -92,12 +92,13 @@ static bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,
/// Returns true if and only if a replacement was made.
static void replaceWithNullptr(ClangTidyCheck &Check, SourceManager &SM,
SourceLocation StartLoc, SourceLocation EndLoc) {
- CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);
+ const CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);
// Add a space if nullptr follows an alphanumeric character. This happens
// whenever there is an c-style explicit cast to nullptr not surrounded by
// parentheses and right beside a return statement.
- SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);
- bool NeedsSpace = isAlphanumeric(*SM.getCharacterData(PreviousLocation));
+ const SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);
+ const bool NeedsSpace =
+ isAlphanumeric(*SM.getCharacterData(PreviousLocation));
Check.diag(Range.getBegin(), "use nullptr") << FixItHint::CreateReplacement(
Range, NeedsSpace ? " nullptr" : "nullptr");
}
@@ -136,7 +137,7 @@ class MacroArgUsageVisitor : public RecursiveASTVisitor<MacroArgUsageVisitor> {
}
bool TraverseStmt(Stmt *S) {
- bool VisitedPreviously = Visited;
+ const bool VisitedPreviously = Visited;
if (!RecursiveASTVisitor<MacroArgUsageVisitor>::TraverseStmt(S))
return false;
@@ -258,8 +259,8 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
// If the location comes from a macro body expansion, check to see if its
// coming from one of the allowed 'NULL' macros.
if (SM.isMacroArgExpansion(StartLoc) && SM.isMacroArgExpansion(EndLoc)) {
- SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
- FileLocEnd = SM.getFileLoc(EndLoc);
+ const SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
+ FileLocEnd = SM.getFileLoc(EndLoc);
SourceLocation ImmediateMacroArgLoc, MacroLoc;
// Skip NULL macros used in macro.
if (!getMacroAndArgLocations(StartLoc, ImmediateMacroArgLoc, MacroLoc) ||
@@ -274,7 +275,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
}
if (SM.isMacroBodyExpansion(StartLoc) && SM.isMacroBodyExpansion(EndLoc)) {
- StringRef OutermostMacroName =
+ const StringRef OutermostMacroName =
getOutermostMacroName(StartLoc, SM, Context.getLangOpts());
// Check to see if the user wants to replace the macro being expanded.
@@ -302,7 +303,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
/// Tests that all expansions of a macro arg, one of which expands to
/// result in \p CE, yield NullTo(Member)Pointer casts.
bool allArgUsesValid(const CastExpr *CE) {
- SourceLocation CastLoc = CE->getBeginLoc();
+ const SourceLocation CastLoc = CE->getBeginLoc();
// Step 1: Get location of macro arg and location of the macro the arg was
// provided to.
@@ -348,17 +349,17 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
// Find the location of the immediate macro expansion.
while (true) {
- std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);
+ const std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);
const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
- SourceLocation OldArgLoc = ArgLoc;
+ const SourceLocation OldArgLoc = ArgLoc;
ArgLoc = Expansion.getExpansionLocStart();
if (!Expansion.isMacroArgExpansion()) {
if (!MacroLoc.isFileID())
return false;
- StringRef Name =
+ const StringRef Name =
Lexer::getImmediateMacroName(OldArgLoc, SM, Context.getLangOpts());
return llvm::is_contained(NullMacros, Name);
}
@@ -371,7 +372,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
// If spelling location resides in the same FileID as macro expansion
// location, it means there is no inner macro.
- FileID MacroFID = SM.getFileID(MacroLoc);
+ const FileID MacroFID = SM.getFileID(MacroLoc);
if (SM.isInFileID(ArgLoc, MacroFID)) {
// Don't transform this case. If the characters that caused the
// null-conversion come from within a macro, they can't be changed.
@@ -401,7 +402,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
SourceLocation Loc = TestLoc, MacroLoc;
while (true) {
- std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
+ const std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
diff --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
index 6a19183737119..62a2de23147a7 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
@@ -55,9 +55,9 @@ void UseOverrideCheck::registerMatchers(MatchFinder *Finder) {
static SmallVector<Token, 16>
parseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result) {
const SourceManager &Sources = *Result.SourceManager;
- std::pair<FileID, unsigned> LocInfo =
+ const std::pair<FileID, unsigned> LocInfo =
Sources.getDecomposedLoc(Range.getBegin());
- StringRef File = Sources.getBufferData(LocInfo.first);
+ const StringRef File = Sources.getBufferData(LocInfo.first);
const char *TokenBegin = File.data() + LocInfo.second;
Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
Result.Context->getLangOpts(), File.begin(), TokenBegin,
@@ -103,12 +103,12 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
Method->isOutOfLine())
return;
- bool HasVirtual = Method->isVirtualAsWritten();
- bool HasOverride = Method->getAttr<OverrideAttr>();
- bool HasFinal = Method->getAttr<FinalAttr>();
+ const bool HasVirtual = Method->isVirtualAsWritten();
+ const bool HasOverride = Method->getAttr<OverrideAttr>();
+ const bool HasFinal = Method->getAttr<FinalAttr>();
- bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
- unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
+ const bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
+ const unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
if ((!OnlyVirtualSpecified && KeywordCount == 1) ||
(!HasVirtual && HasOverride && HasFinal && AllowOverrideAndFinal))
@@ -120,12 +120,12 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
} else if (KeywordCount == 0) {
Message = "annotate this function with '%0' or (rarely) '%1'";
} else {
- StringRef Redundant =
+ const StringRef Redundant =
HasVirtual ? (HasOverride && HasFinal && !AllowOverrideAndFinal
? "'virtual' and '%0' are"
: "'virtual' is")
: "'%0' is";
- StringRef Correct = HasFinal ? "'%1'" : "'%0'";
+ const StringRef Correct = HasFinal ? "'%1'" : "'%0'";
Message = (llvm::Twine(Redundant) +
" redundant since the function is already declared " + Correct)
@@ -135,7 +135,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
auto Diag = diag(Method->getLocation(), Message)
<< OverrideSpelling << FinalSpelling;
- CharSourceRange FileRange = Lexer::makeFileCharRange(
+ const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(Method->getSourceRange()), Sources,
getLangOpts());
@@ -151,9 +151,9 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
if (!HasFinal && !HasOverride) {
SourceLocation InsertLoc;
std::string ReplacementText = (OverrideSpelling + " ").str();
- SourceLocation MethodLoc = Method->getLocation();
+ const SourceLocation MethodLoc = Method->getLocation();
- for (Token T : Tokens) {
+ for (const Token T : Tokens) {
if (T.is(tok::kw___attribute) &&
!Sources.isBeforeInTranslationUnit(T.getLocation(), MethodLoc)) {
InsertLoc = T.getLocation();
@@ -164,7 +164,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
if (Method->hasAttrs()) {
for (const clang::Attr *A : Method->getAttrs()) {
if (!A->isImplicit() && !A->isInherited()) {
- SourceLocation Loc =
+ const SourceLocation Loc =
Sources.getExpansionLoc(A->getRange().getBegin());
if ((!InsertLoc.isValid() ||
Sources.isBeforeInTranslationUnit(Loc, InsertLoc)) &&
@@ -221,13 +221,14 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
}
if (HasFinal && HasOverride && !AllowOverrideAndFinal) {
- SourceLocation OverrideLoc = Method->getAttr<OverrideAttr>()->getLocation();
+ const SourceLocation OverrideLoc =
+ Method->getAttr<OverrideAttr>()->getLocation();
Diag << FixItHint::CreateRemoval(
CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));
}
if (HasVirtual) {
- for (Token Tok : Tokens) {
+ for (const Token Tok : Tokens) {
if (Tok.is(tok::kw_virtual)) {
std::optional<Token> NextToken =
utils::lexer::findNextTokenIncludingComments(
diff --git a/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
index 9bf316939e2d0..8849c331608f9 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
@@ -74,7 +74,8 @@ findLocksInCompoundStmt(const CompoundStmt *Block,
for (const Stmt *Stmt : Block->body()) {
if (const auto *DS = dyn_cast<DeclStmt>(Stmt)) {
- llvm::SmallVector<const VarDecl *> LockGuards = getLockGuardsFromDecl(DS);
+ const llvm::SmallVector<const VarDecl *> LockGuards =
+ getLockGuardsFromDecl(DS);
if (!LockGuards.empty()) {
CurrentLockGuardGroup.append(LockGuards);
@@ -176,7 +177,7 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {
void UseScopedLockCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *DS = Result.Nodes.getNodeAs<DeclStmt>("lock-decl-single")) {
- llvm::SmallVector<const VarDecl *> Decls = getLockGuardsFromDecl(DS);
+ const llvm::SmallVector<const VarDecl *> Decls = getLockGuardsFromDecl(DS);
diagOnMultipleLocks({Decls}, Result);
return;
}
diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
index 414aa86c5fbd2..47ff9ffd3f7b7 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp
@@ -307,7 +307,7 @@ UseStdNumbersCheck::UseStdNumbersCheck(const StringRef Name,
void UseStdNumbersCheck::registerMatchers(MatchFinder *const Finder) {
const auto Matches = MatchBuilder{DiffThreshold};
- std::vector<Matcher<clang::Stmt>> ConstantMatchers = {
+ const std::vector<Matcher<clang::Stmt>> ConstantMatchers = {
Matches.matchLog2Euler(), Matches.matchLog10Euler(),
Matches.matchEulerTopLevel(), Matches.matchEgamma(),
Matches.matchInvSqrtPi(), Matches.matchInvPi(),
diff --git a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
index d623ec402179b..3c828c4c37fe1 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
@@ -55,7 +55,7 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
bool visitUnqualName(StringRef UnqualName) {
// Check for collisions with function arguments.
- for (ParmVarDecl *Param : F.parameters())
+ for (const ParmVarDecl *Param : F.parameters())
if (const IdentifierInfo *Ident = Param->getIdentifier())
if (Ident->getName() == UnqualName) {
Collision = true;
@@ -126,7 +126,7 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
}
bool VisitDeclRefExpr(DeclRefExpr *S) {
- DeclarationName Name = S->getNameInfo().getName();
+ const DeclarationName Name = S->getNameInfo().getName();
return S->getQualifierLoc() || Name.isEmpty() || !Name.isIdentifier() ||
!visitUnqualName(Name.getAsIdentifierInfo()->getName());
}
@@ -159,14 +159,14 @@ static SourceLocation findTrailingReturnTypeSourceLocation(
const FunctionDecl &F, const FunctionTypeLoc &FTL, const ASTContext &Ctx,
const SourceManager &SM, const LangOptions &LangOpts) {
// We start with the location of the closing parenthesis.
- SourceRange ExceptionSpecRange = F.getExceptionSpecSourceRange();
+ const SourceRange ExceptionSpecRange = F.getExceptionSpecSourceRange();
if (ExceptionSpecRange.isValid())
return Lexer::getLocForEndOfToken(ExceptionSpecRange.getEnd(), 0, SM,
LangOpts);
// If the function argument list ends inside of a macro, it is dangerous to
// start lexing from here - bail out.
- SourceLocation ClosingParen = FTL.getRParenLoc();
+ const SourceLocation ClosingParen = FTL.getRParenLoc();
if (ClosingParen.isMacroID())
return {};
@@ -174,8 +174,8 @@ static SourceLocation findTrailingReturnTypeSourceLocation(
Lexer::getLocForEndOfToken(ClosingParen, 0, SM, LangOpts);
// Skip subsequent CV and ref qualifiers.
- std::pair<FileID, unsigned> Loc = SM.getDecomposedLoc(Result);
- StringRef File = SM.getBufferData(Loc.first);
+ const std::pair<FileID, unsigned> Loc = SM.getDecomposedLoc(Result);
+ const StringRef File = SM.getBufferData(Loc.first);
const char *TokenBegin = File.data() + Loc.second;
Lexer Lexer(SM.getLocForStartOfFile(Loc.first), LangOpts, File.begin(),
TokenBegin, File.end());
@@ -220,7 +220,7 @@ classifyToken(const FunctionDecl &F, Preprocessor &PP, Token Tok) {
Token End;
End.startToken();
End.setKind(tok::eof);
- SmallVector<Token, 2> Stream{Tok, End};
+ const SmallVector<Token, 2> Stream{Tok, End};
// FIXME: do not report these token to Preprocessor.TokenWatcher.
PP.EnterTokenStream(Stream, false, /*IsReinject=*/false);
@@ -230,8 +230,8 @@ classifyToken(const FunctionDecl &F, Preprocessor &PP, Token Tok) {
if (T.is(tok::eof))
break;
- bool Qual = isCvr(T);
- bool Spec = isSpecifier(T);
+ const bool Qual = isCvr(T);
+ const bool Spec = isSpecifier(T);
CT.IsQualifier &= Qual;
CT.IsSpecifier &= Spec;
ContainsQualifiers |= Qual;
@@ -252,12 +252,12 @@ classifyTokensBeforeFunctionName(const FunctionDecl &F, const ASTContext &Ctx,
const SourceManager &SM,
const LangOptions &LangOpts,
Preprocessor *PP) {
- SourceLocation BeginF = expandIfMacroId(F.getBeginLoc(), SM);
- SourceLocation BeginNameF = expandIfMacroId(F.getLocation(), SM);
+ const SourceLocation BeginF = expandIfMacroId(F.getBeginLoc(), SM);
+ const SourceLocation BeginNameF = expandIfMacroId(F.getLocation(), SM);
// Create tokens for everything before the name of the function.
- std::pair<FileID, unsigned> Loc = SM.getDecomposedLoc(BeginF);
- StringRef File = SM.getBufferData(Loc.first);
+ const std::pair<FileID, unsigned> Loc = SM.getDecomposedLoc(BeginF);
+ const StringRef File = SM.getBufferData(Loc.first);
const char *TokenBegin = File.data() + Loc.second;
Lexer Lexer(SM.getLocForStartOfFile(Loc.first), LangOpts, File.begin(),
TokenBegin, File.end());
@@ -369,9 +369,9 @@ static SourceLocation findLambdaTrailingReturnInsertLoc(
else
ParamEndLoc = Method->getParametersSourceRange().getEnd();
- std::pair<FileID, unsigned> ParamEndLocInfo =
+ const std::pair<FileID, unsigned> ParamEndLocInfo =
SM.getDecomposedLoc(ParamEndLoc);
- StringRef Buffer = SM.getBufferData(ParamEndLocInfo.first);
+ const StringRef Buffer = SM.getBufferData(ParamEndLocInfo.first);
Lexer Lexer(SM.getLocForStartOfFile(ParamEndLocInfo.first), LangOpts,
Buffer.begin(), Buffer.data() + ParamEndLocInfo.second,
@@ -421,11 +421,11 @@ static void keepSpecifiers(std::string &ReturnType, std::string &Auto,
return;
// Find specifiers, remove them from the return type, add them to 'auto'.
- unsigned int ReturnTypeBeginOffset =
+ const unsigned int ReturnTypeBeginOffset =
SM.getDecomposedLoc(ReturnTypeCVRange.getBegin()).second;
- size_t InitialAutoLength = Auto.size();
+ const size_t InitialAutoLength = Auto.size();
unsigned int DeletedChars = 0;
- for (ClassifiedToken CT : *MaybeTokens) {
+ for (const ClassifiedToken CT : *MaybeTokens) {
if (SM.isBeforeInTranslationUnit(CT.T.getLocation(),
ReturnTypeCVRange.getBegin()) ||
SM.isBeforeInTranslationUnit(ReturnTypeCVRange.getEnd(),
@@ -436,10 +436,11 @@ static void keepSpecifiers(std::string &ReturnType, std::string &Auto,
// Add the token to 'auto' and remove it from the return type, including
// any whitespace following the token.
- unsigned int TOffset = SM.getDecomposedLoc(CT.T.getLocation()).second;
+ const unsigned int TOffset = SM.getDecomposedLoc(CT.T.getLocation()).second;
assert(TOffset >= ReturnTypeBeginOffset &&
"Token location must be after the beginning of the return type");
- unsigned int TOffsetInRT = TOffset - ReturnTypeBeginOffset - DeletedChars;
+ const unsigned int TOffsetInRT =
+ TOffset - ReturnTypeBeginOffset - DeletedChars;
unsigned int TLengthWithWS = CT.T.getLength();
while (TOffsetInRT + TLengthWithWS < ReturnType.size() &&
llvm::isSpace(ReturnType[TOffsetInRT + TLengthWithWS]))
@@ -548,7 +549,7 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
- SourceLocation InsertionLoc =
+ const SourceLocation InsertionLoc =
findTrailingReturnTypeSourceLocation(*F, FTL, Ctx, SM, LangOpts);
if (InsertionLoc.isInvalid()) {
diag(F->getLocation(), ErrorMessageOnFunction);
@@ -558,7 +559,7 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
// Using the declared return type via F->getDeclaredReturnType().getAsString()
// discards user formatting and order of const, volatile, type, whitespace,
// space before & ... .
- SourceRange ReturnTypeCVRange = findReturnTypeAndCVSourceRange(
+ const SourceRange ReturnTypeCVRange = findReturnTypeAndCVSourceRange(
*F, FTL.getReturnLoc(), Ctx, SM, LangOpts, PP);
if (ReturnTypeCVRange.isInvalid()) {
diag(F->getLocation(), ErrorMessageOnFunction);
@@ -580,13 +581,13 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
- SourceLocation ReturnTypeEnd =
+ const SourceLocation ReturnTypeEnd =
Lexer::getLocForEndOfToken(ReturnTypeCVRange.getEnd(), 0, SM, LangOpts);
- StringRef CharAfterReturnType = Lexer::getSourceText(
+ const StringRef CharAfterReturnType = Lexer::getSourceText(
CharSourceRange::getCharRange(ReturnTypeEnd,
ReturnTypeEnd.getLocWithOffset(1)),
SM, LangOpts);
- bool NeedSpaceAfterAuto =
+ const bool NeedSpaceAfterAuto =
CharAfterReturnType.empty() || !llvm::isSpace(CharAfterReturnType[0]);
std::string Auto = NeedSpaceAfterAuto ? "auto " : "auto";
diff --git a/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp
index 03ecec9bd175b..e3672f84a3a5c 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp
@@ -96,7 +96,7 @@ void UseTransparentFunctorsCheck::check(
FunctorParentType->template_arguments()[ArgNum];
if (Arg.getKind() != TemplateArgument::Type)
continue;
- QualType ParentArgType = Arg.getAsType();
+ const QualType ParentArgType = Arg.getAsType();
if (ParentArgType->isRecordType() &&
ParentArgType->getAsCXXRecordDecl() ==
Functor->getAsType()->getAsCXXRecordDecl())
@@ -105,13 +105,13 @@ void UseTransparentFunctorsCheck::check(
// Functor is a default template argument.
if (ArgNum == FunctorParentType->template_arguments().size())
return;
- TemplateArgumentLoc FunctorLoc = FunctorParentLoc.getArgLoc(ArgNum);
+ const TemplateArgumentLoc FunctorLoc = FunctorParentLoc.getArgLoc(ArgNum);
auto FunctorTypeLoc = getInnerTypeLocAs<TemplateSpecializationTypeLoc>(
FunctorLoc.getTypeSourceInfo()->getTypeLoc());
if (FunctorTypeLoc.isNull())
return;
- SourceLocation ReportLoc = FunctorLoc.getLocation();
+ const SourceLocation ReportLoc = FunctorLoc.getLocation();
if (ReportLoc.isInvalid())
return;
diag(ReportLoc, Message) << FuncClass->getName()
diff --git a/clang-tools-extra/clang-tidy/modernize/UseUncaughtExceptionsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUncaughtExceptionsCheck.cpp
index eef9d39800360..08c40d4554488 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUncaughtExceptionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUncaughtExceptionsCheck.cpp
@@ -15,7 +15,7 @@ using namespace clang::ast_matchers;
namespace clang::tidy::modernize {
void UseUncaughtExceptionsCheck::registerMatchers(MatchFinder *Finder) {
- std::string MatchText = "::std::uncaught_exception";
+ const std::string MatchText = "::std::uncaught_exception";
// Using declaration: warning and fix-it.
Finder->addMatcher(
@@ -78,7 +78,7 @@ void UseUncaughtExceptionsCheck::check(const MatchFinder::MatchResult &Result) {
*Result.SourceManager, getLangOpts());
Text.consume_back("()");
- int TextLength = Text.size();
+ const int TextLength = Text.size();
if (WarnOnly) {
return;
diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index 72673753e6c60..38b368957c5d5 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -114,7 +114,7 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
if (ExternCDecl && IgnoreExternC)
return;
- SourceLocation StartLoc = MatchedDecl->getBeginLoc();
+ const SourceLocation StartLoc = MatchedDecl->getBeginLoc();
if (StartLoc.isMacroID() && IgnoreMacros)
return;
@@ -172,7 +172,7 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
.str(),
ExtraReference.str()};
}();
- StringRef Name = MatchedDecl->getName();
+ const StringRef Name = MatchedDecl->getName();
SourceRange ReplaceRange = MatchedDecl->getSourceRange();
// typedefs with multiple comma-separated definitions produce multiple
@@ -223,7 +223,8 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
- std::string Replacement = (Using + Name + " = " + Type + QualifierStr).str();
+ const std::string Replacement =
+ (Using + Name + " = " + Type + QualifierStr).str();
Diag << FixItHint::CreateReplacement(ReplaceRange, Replacement);
}
} // namespace clang::tidy::modernize
More information about the cfe-commits
mailing list