Skip to content

Commit 6aaa9cb

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (6/N) (#167056)
1 parent e3a6792 commit 6aaa9cb

21 files changed

+78
-75
lines changed

clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
140140

141141
CharSourceRange ReplaceRange = getReplaceRange(CastExpr);
142142

143-
bool FnToFnCast =
143+
const bool FnToFnCast =
144144
IsFunction(SourceTypeAsWritten) && IsFunction(DestTypeAsWritten);
145145

146146
const bool ConstructorCast = !CastExpr->getTypeAsWritten().hasQualifiers() &&
@@ -239,8 +239,8 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
239239
return;
240240
}
241241
if (DestType->isReferenceType()) {
242-
QualType Dest = DestType.getNonReferenceType();
243-
QualType Source = SourceType.getNonReferenceType();
242+
const QualType Dest = DestType.getNonReferenceType();
243+
const QualType Source = SourceType.getNonReferenceType();
244244
if (Source == Dest.withConst() ||
245245
SourceType.getNonReferenceType() == DestType.getNonReferenceType()) {
246246
ReplaceWithNamedCast("const_cast");

clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ using namespace clang::ast_matchers;
2222
namespace clang::tidy::google::objc {
2323

2424
static bool isMessageExpressionInsideMacro(const ObjCMessageExpr *Expr) {
25-
SourceLocation ReceiverLocation = Expr->getReceiverRange().getBegin();
25+
const SourceLocation ReceiverLocation = Expr->getReceiverRange().getBegin();
2626
if (ReceiverLocation.isMacroID())
2727
return true;
2828

29-
SourceLocation SelectorLocation = Expr->getSelectorStartLoc();
29+
const SourceLocation SelectorLocation = Expr->getSelectorStartLoc();
3030
if (SelectorLocation.isMacroID())
3131
return true;
3232

@@ -58,7 +58,7 @@ static bool isInitMethodAvailable(const ObjCInterfaceDecl *ClassDecl) {
5858
static StringRef getReceiverString(SourceRange ReceiverRange,
5959
const SourceManager &SM,
6060
const LangOptions &LangOpts) {
61-
CharSourceRange CharRange = Lexer::makeFileCharRange(
61+
const CharSourceRange CharRange = Lexer::makeFileCharRange(
6262
CharSourceRange::getTokenRange(ReceiverRange), SM, LangOpts);
6363
return Lexer::getSourceText(CharRange, SM, LangOpts);
6464
}
@@ -77,13 +77,13 @@ static FixItHint getCallFixItHint(const ObjCMessageExpr *Expr,
7777
if (FoundClassFactory != ClassToFactoryMethodMap.end()) {
7878
StringRef ClassName = FoundClassFactory->first;
7979
StringRef FactorySelector = FoundClassFactory->second;
80-
std::string NewCall =
80+
const std::string NewCall =
8181
std::string(llvm::formatv("[{0} {1}]", ClassName, FactorySelector));
8282
return FixItHint::CreateReplacement(Expr->getSourceRange(), NewCall);
8383
}
8484

8585
if (isInitMethodAvailable(Expr->getReceiverInterface())) {
86-
std::string NewCall =
86+
const std::string NewCall =
8787
std::string(llvm::formatv("[[{0} alloc] init]", Receiver));
8888
return FixItHint::CreateReplacement(Expr->getSourceRange(), NewCall);
8989
}

clang-tools-extra/clang-tidy/google/AvoidThrowingObjCExceptionCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void AvoidThrowingObjCExceptionCheck::check(
4040
// If the match location was in a macro, check if the macro was in a system
4141
// header.
4242
if (SourceLoc.isMacroID()) {
43-
SourceManager &SM = *Result.SourceManager;
43+
const SourceManager &SM = *Result.SourceManager;
4444
auto MacroLoc = SM.getImmediateMacroCallerLoc(SourceLoc);
4545

4646
// Matches in system header macros should be ignored.

clang-tools-extra/clang-tidy/google/AvoidUnderscoreInGoogletestNameCheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ class AvoidUnderscoreInGoogletestNameCallback : public PPCallbacks {
3939
void MacroExpands(const Token &MacroNameToken,
4040
const MacroDefinition &MacroDefinition, SourceRange Range,
4141
const MacroArgs *Args) override {
42-
IdentifierInfo *NameIdentifierInfo = MacroNameToken.getIdentifierInfo();
42+
const IdentifierInfo *NameIdentifierInfo =
43+
MacroNameToken.getIdentifierInfo();
4344
if (!NameIdentifierInfo)
4445
return;
45-
StringRef MacroName = NameIdentifierInfo->getName();
46+
const StringRef MacroName = NameIdentifierInfo->getName();
4647
if (!isGoogletestTestMacro(MacroName) || !Args ||
4748
Args->getNumMacroArguments() < 2)
4849
return;
4950
const Token *TestSuiteNameToken = Args->getUnexpArgument(0);
5051
const Token *TestNameToken = Args->getUnexpArgument(1);
5152
if (!TestSuiteNameToken || !TestNameToken)
5253
return;
53-
std::string TestSuiteNameMaybeDisabled =
54+
const std::string TestSuiteNameMaybeDisabled =
5455
PP->getSpelling(*TestSuiteNameToken);
5556
StringRef TestSuiteName = TestSuiteNameMaybeDisabled;
5657
TestSuiteName.consume_front(KDisabledTestPrefix);
@@ -60,7 +61,7 @@ class AvoidUnderscoreInGoogletestNameCallback : public PPCallbacks {
6061
"Googletest FAQ")
6162
<< TestSuiteName;
6263

63-
std::string TestNameMaybeDisabled = PP->getSpelling(*TestNameToken);
64+
const std::string TestNameMaybeDisabled = PP->getSpelling(*TestNameToken);
6465
StringRef TestName = TestNameMaybeDisabled;
6566
TestName.consume_front(KDisabledTestPrefix);
6667
if (TestName.contains('_'))

clang-tools-extra/clang-tidy/google/ExplicitConstructorCheck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ static SourceRange findToken(const SourceManager &Sources,
3939
bool (*Pred)(const Token &)) {
4040
if (StartLoc.isMacroID() || EndLoc.isMacroID())
4141
return {};
42-
FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
43-
StringRef Buf = Sources.getBufferData(File);
42+
const FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
43+
const StringRef Buf = Sources.getBufferData(File);
4444
const char *StartChar = Sources.getCharacterData(StartLoc);
4545
Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
4646
Lex.SetCommentRetentionState(true);
@@ -88,7 +88,7 @@ void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
8888
Result.Nodes.getNodeAs<CXXConversionDecl>("conversion")) {
8989
if (Conversion->isOutOfLine())
9090
return;
91-
SourceLocation Loc = Conversion->getLocation();
91+
const SourceLocation Loc = Conversion->getLocation();
9292
// Ignore all macros until we learn to ignore specific ones (e.g. used in
9393
// gmock to define matchers).
9494
if (Loc.isMacroID())
@@ -105,15 +105,15 @@ void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
105105

106106
const ExplicitSpecifier ExplicitSpec = Ctor->getExplicitSpecifier();
107107

108-
bool TakesInitializerList = isStdInitializerList(
108+
const bool TakesInitializerList = isStdInitializerList(
109109
Ctor->getParamDecl(0)->getType().getNonReferenceType());
110110
if (ExplicitSpec.isExplicit() &&
111111
(Ctor->isCopyOrMoveConstructor() || TakesInitializerList)) {
112112
auto IsKwExplicit = [](const Token &Tok) {
113113
return Tok.is(tok::raw_identifier) &&
114114
Tok.getRawIdentifier() == "explicit";
115115
};
116-
SourceRange ExplicitTokenRange =
116+
const SourceRange ExplicitTokenRange =
117117
findToken(*Result.SourceManager, getLangOpts(),
118118
Ctor->getOuterLocStart(), Ctor->getEndLoc(), IsKwExplicit);
119119
StringRef ConstructorDescription;
@@ -149,7 +149,7 @@ void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
149149

150150
const bool SingleArgument =
151151
Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack();
152-
SourceLocation Loc = Ctor->getLocation();
152+
const SourceLocation Loc = Ctor->getLocation();
153153
auto Diag =
154154
diag(Loc, ExplicitExpr ? WithExpressionWarningMessage
155155
: NoExpressionWarningMessage)

clang-tools-extra/clang-tidy/google/FunctionNamingCheck.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static std::string validFunctionNameRegex(bool RequirePrefix) {
3333
// If a prefix is required, the regex checks for a capital letter followed by
3434
// another capital letter or number that is part of the prefix and another
3535
// capital letter or number that begins the name following the prefix.
36-
std::string FunctionNameMatcher =
36+
const std::string FunctionNameMatcher =
3737
std::string(RequirePrefix ? "[A-Z][A-Z0-9]+" : "") + "[A-Z][a-zA-Z0-9]*";
3838
return std::string("::(") + FunctionNameMatcher + ")$";
3939
}
@@ -48,13 +48,13 @@ static FixItHint generateFixItHint(const FunctionDecl *Decl) {
4848
if (Decl->getStorageClass() != SC_Static)
4949
return {};
5050

51-
StringRef Name = Decl->getName();
51+
const StringRef Name = Decl->getName();
5252
std::string NewName = Decl->getName().str();
5353

5454
size_t Index = 0;
5555
bool AtWordBoundary = true;
5656
while (Index < NewName.size()) {
57-
char Ch = NewName[Index];
57+
const char Ch = NewName[Index];
5858
if (isalnum(Ch)) {
5959
// Capitalize the first letter after every word boundary.
6060
if (AtWordBoundary) {
@@ -101,7 +101,7 @@ void FunctionNamingCheck::registerMatchers(MatchFinder *Finder) {
101101
void FunctionNamingCheck::check(const MatchFinder::MatchResult &Result) {
102102
const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("function");
103103

104-
bool IsGlobal = MatchedDecl->getStorageClass() != SC_Static;
104+
const bool IsGlobal = MatchedDecl->getStorageClass() != SC_Static;
105105
diag(MatchedDecl->getLocation(),
106106
"%select{static function|function in global namespace}1 named %0 must "
107107
"%select{be in|have an appropriate prefix followed by}1 Pascal case as "

clang-tools-extra/clang-tidy/google/GlobalVariableDeclarationCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ static FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
3030
return {};
3131
}
3232

33-
char FC = Decl->getName()[0];
33+
const char FC = Decl->getName()[0];
3434
if (!llvm::isAlpha(FC) || Decl->getName().size() == 1) {
3535
// No fix available if first character is not alphabetical character, or it
3636
// is a single-character variable, since it is difficult to determine the
3737
// proper fix in this case. Users should create a proper variable name by
3838
// their own.
3939
return {};
4040
}
41-
char SC = Decl->getName()[1];
41+
const char SC = Decl->getName()[1];
4242
if ((FC == 'k' || FC == 'g') && !llvm::isAlpha(SC)) {
4343
// No fix available if the prefix is correct but the second character is
4444
// not alphabetical, since it is difficult to determine the proper fix in

clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
103103

104104
void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
105105
auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl");
106-
SourceLocation Loc = TL.getBeginLoc();
106+
const SourceLocation Loc = TL.getBeginLoc();
107107

108108
// Look through qualification.
109109
if (auto QualLoc = TL.getAs<QualifiedTypeLoc>())
@@ -113,7 +113,7 @@ void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
113113
if (!BuiltinLoc)
114114
return;
115115

116-
Token Tok = getTokenAtLoc(Loc, Result, *IdentTable);
116+
const Token Tok = getTokenAtLoc(Loc, Result, *IdentTable);
117117
// Ensure the location actually points to one of the builting integral type
118118
// names we're interested in. Otherwise, we might be getting this match from
119119
// implicit code (e.g. an implicit assignment operator of a class containing
@@ -164,7 +164,7 @@ void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
164164
!isAsciiIdentifierContinue(Data[Port.size()]))
165165
return;
166166

167-
std::string Replacement =
167+
const std::string Replacement =
168168
((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
169169
TypeSuffix)
170170
.str();

clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@ class TodoCommentCheck::TodoCommentHandler : public CommentHandler {
2020
TodoMatch("^// *TODO *(\\(.*\\))?:?( )?(.*)$") {}
2121

2222
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
23-
StringRef Text =
23+
const StringRef Text =
2424
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
2525
PP.getSourceManager(), PP.getLangOpts());
2626

2727
SmallVector<StringRef, 4> Matches;
2828
if (!TodoMatch.match(Text, &Matches))
2929
return false;
3030

31-
StringRef Username = Matches[1];
32-
StringRef Comment = Matches[3];
31+
const StringRef Username = Matches[1];
32+
const StringRef Comment = Matches[3];
3333

3434
if (!Username.empty())
3535
return false;
3636

37-
std::string NewText = ("// TODO(" + Twine(User) + "): " + Comment).str();
37+
const std::string NewText =
38+
("// TODO(" + Twine(User) + "): " + Comment).str();
3839

3940
Check.diag(Range.getBegin(), "missing username/bug in TODO")
4041
<< FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range),

clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void UnnamedNamespaceInHeaderCheck::registerMatchers(
2828
void UnnamedNamespaceInHeaderCheck::check(
2929
const MatchFinder::MatchResult &Result) {
3030
const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
31-
SourceLocation Loc = N->getBeginLoc();
31+
const SourceLocation Loc = N->getBeginLoc();
3232
if (!Loc.isValid())
3333
return;
3434

0 commit comments

Comments
 (0)