Skip to content

Commit a9301d8

Browse files
authored
[clang-tidy][NFC] Fix misc-const-correctness warnings (7/N) (#167058)
1 parent 6c84dd7 commit a9301d8

16 files changed

+94
-88
lines changed

clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ static llvm::SmallString<64U> skeleton(StringRef Name) {
5656

5757
const char *Prev = Curr;
5858
UTF32 CodePoint = 0;
59-
ConversionResult Result = convertUTF8Sequence(
59+
const ConversionResult Result = convertUTF8Sequence(
6060
reinterpret_cast<const UTF8 **>(&Curr),
6161
reinterpret_cast<const UTF8 *>(End), &CodePoint, strictConversion);
6262
if (Result != conversionOK) {
6363
errs() << "Unicode conversion issue\n";
6464
break;
6565
}
6666

67-
StringRef Key(Prev, Curr - Prev);
67+
const StringRef Key(Prev, Curr - Prev);
6868
auto *Where = llvm::lower_bound(ConfusableEntries, CodePoint,
6969
[](decltype(ConfusableEntries[0]) X,
7070
UTF32 Y) { return X.codepoint < Y; });
@@ -183,7 +183,7 @@ void ConfusableIdentifierCheck::addDeclToCheck(const NamedDecl *ND,
183183
if (!NDII)
184184
return;
185185

186-
StringRef NDName = NDII->getName();
186+
const StringRef NDName = NDII->getName();
187187
if (NDName.empty())
188188
return;
189189

@@ -216,7 +216,7 @@ void ConfusableIdentifierCheck::onEndOfTranslationUnit() {
216216
// the same skeleton.
217217
for (const IdentifierInfo *II : Idents) {
218218
for (auto [OuterND, OuterParent] : NameToDecls[II]) {
219-
for (Entry Inner : DeclsWithinContext[OuterParent]) {
219+
for (const Entry Inner : DeclsWithinContext[OuterParent]) {
220220
// Don't complain if the identifiers are the same.
221221
if (OuterND->getIdentifier() == Inner.ND->getIdentifier())
222222
continue;

clang-tools-extra/clang-tidy/misc/ConfusableTable/BuildConfusableTable.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int argc, char *argv[]) {
2626

2727
std::vector<std::pair<llvm::UTF32, SmallVector<llvm::UTF32>>> Entries;
2828
SmallVector<StringRef> Values;
29-
for (StringRef Line : Lines) {
29+
for (const StringRef Line : Lines) {
3030
if (Line.starts_with("#"))
3131
continue;
3232

@@ -37,14 +37,14 @@ int main(int argc, char *argv[]) {
3737
return 2;
3838
}
3939

40-
llvm::StringRef From = Values[0].trim();
40+
const llvm::StringRef From = Values[0].trim();
4141
llvm::UTF32 CodePoint = 0;
4242
From.getAsInteger(16, CodePoint);
4343

4444
SmallVector<llvm::UTF32> To;
4545
SmallVector<StringRef> ToN;
4646
Values[1].split(ToN, ' ', -1, false);
47-
for (StringRef ToI : ToN) {
47+
for (const StringRef ToI : ToN) {
4848
llvm::UTF32 ToCodePoint = 0;
4949
ToI.trim().getAsInteger(16, ToCodePoint);
5050
To.push_back(ToCodePoint);
@@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
5656
}
5757
llvm::sort(Entries);
5858

59-
unsigned LargestValue =
59+
const unsigned LargestValue =
6060
llvm::max_element(Entries, [](const auto &Entry0, const auto &Entry1) {
6161
return Entry0.second.size() < Entry1.second.size();
6262
})->second.size();

clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ AST_MATCHER_P(Stmt, forEachPrevStmt, ast_matchers::internal::Matcher<Stmt>,
5656
// Matches the expression awaited by the `co_await`.
5757
AST_MATCHER_P(CoawaitExpr, awaitable, ast_matchers::internal::Matcher<Expr>,
5858
InnerMatcher) {
59-
if (Expr *E = Node.getOperand())
59+
if (const Expr *E = Node.getOperand())
6060
return InnerMatcher.matches(*E, Finder, Builder);
6161
return false;
6262
}

clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
9393
}
9494
}
9595

96-
bool IsFullSpec = FD->getTemplateSpecializationKind() != TSK_Undeclared;
96+
const bool IsFullSpec =
97+
FD->getTemplateSpecializationKind() != TSK_Undeclared;
9798
diag(FD->getLocation(),
9899
"%select{function|full function template specialization}0 %1 defined "
99100
"in a header file; function definitions in header files can lead to "

clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {
200200
Unused.push_back(&I);
201201
}
202202

203-
llvm::StringRef Code = SM->getBufferData(SM->getMainFileID());
203+
const llvm::StringRef Code = SM->getBufferData(SM->getMainFileID());
204204
auto FileStyle =
205205
format::getStyle(format::DefaultFormatStyle, getCurrentMainFile(),
206206
format::DefaultFallbackStyle, Code,
@@ -220,22 +220,22 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {
220220
}
221221

222222
if (MissingIncludes) {
223-
tooling::HeaderIncludes HeaderIncludes(getCurrentMainFile(), Code,
224-
FileStyle->IncludeStyle);
223+
const tooling::HeaderIncludes HeaderIncludes(getCurrentMainFile(), Code,
224+
FileStyle->IncludeStyle);
225225
// Deduplicate insertions when running in bulk fix mode.
226226
llvm::StringSet<> InsertedHeaders{};
227227
for (const auto &Inc : Missing) {
228-
std::string Spelling = include_cleaner::spellHeader(
228+
const std::string Spelling = include_cleaner::spellHeader(
229229
{Inc.Missing, PP->getHeaderSearchInfo(), MainFile});
230-
bool Angled = llvm::StringRef{Spelling}.starts_with("<");
230+
const bool Angled = llvm::StringRef{Spelling}.starts_with("<");
231231
// We might suggest insertion of an existing include in edge cases, e.g.,
232232
// include is present in a PP-disabled region, or spelling of the header
233233
// turns out to be the same as one of the unresolved includes in the
234234
// main file.
235235
if (auto Replacement = HeaderIncludes.insert(
236236
llvm::StringRef{Spelling}.trim("\"<>"), Angled,
237237
tooling::IncludeDirective::Include)) {
238-
DiagnosticBuilder DB =
238+
const DiagnosticBuilder DB =
239239
diag(SM->getSpellingLoc(Inc.SymRef.RefLocation),
240240
"no header providing \"%0\" is directly included")
241241
<< Inc.SymRef.Target.name();

clang-tools-extra/clang-tidy/misc/MisleadingBidirectional.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ static bool containsMisleadingBidi(StringRef Buffer,
4040
//
4141
// Warn if we end up with an unclosed context.
4242
while (CurPtr < Buffer.end()) {
43-
unsigned char C = *CurPtr;
43+
const unsigned char C = *CurPtr;
4444
if (isASCII(C)) {
4545
++CurPtr;
46-
bool IsParagrapSep =
46+
const bool IsParagrapSep =
4747
(C == 0xA || C == 0xD || (0x1C <= C && C <= 0x1E) || C == 0x85);
48-
bool IsSegmentSep = (C == 0x9 || C == 0xB || C == 0x1F);
48+
const bool IsSegmentSep = (C == 0x9 || C == 0xB || C == 0x1F);
4949
if (IsParagrapSep || IsSegmentSep)
5050
BidiContexts.clear();
5151
continue;
5252
}
5353
llvm::UTF32 CodePoint = 0;
54-
llvm::ConversionResult Result = llvm::convertUTF8Sequence(
54+
const llvm::ConversionResult Result = llvm::convertUTF8Sequence(
5555
(const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)Buffer.end(),
5656
&CodePoint, llvm::strictConversion);
5757

@@ -94,7 +94,7 @@ class MisleadingBidirectionalCheck::MisleadingBidirectionalHandler
9494

9595
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
9696
// FIXME: check that we are in a /* */ comment
97-
StringRef Text =
97+
const StringRef Text =
9898
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
9999
PP.getSourceManager(), PP.getLangOpts());
100100

@@ -124,7 +124,7 @@ void MisleadingBidirectionalCheck::registerPPCallbacks(
124124
void MisleadingBidirectionalCheck::check(
125125
const ast_matchers::MatchFinder::MatchResult &Result) {
126126
if (const auto *SL = Result.Nodes.getNodeAs<StringLiteral>("strlit")) {
127-
StringRef Literal = SL->getBytes();
127+
const StringRef Literal = SL->getBytes();
128128
if (containsMisleadingBidi(Literal, false))
129129
diag(SL->getBeginLoc(), "string literal contains misleading "
130130
"bidirectional Unicode characters");

clang-tools-extra/clang-tidy/misc/MisleadingIdentifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static bool hasRTLCharacters(StringRef Buffer) {
124124
const char *EndPtr = Buffer.end();
125125
while (CurPtr < EndPtr) {
126126
llvm::UTF32 CodePoint = 0;
127-
llvm::ConversionResult Result = llvm::convertUTF8Sequence(
127+
const llvm::ConversionResult Result = llvm::convertUTF8Sequence(
128128
(const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)EndPtr, &CodePoint,
129129
llvm::strictConversion);
130130
if (Result != llvm::conversionOK)
@@ -144,9 +144,9 @@ MisleadingIdentifierCheck::~MisleadingIdentifierCheck() = default;
144144
void MisleadingIdentifierCheck::check(
145145
const ast_matchers::MatchFinder::MatchResult &Result) {
146146
if (const auto *ND = Result.Nodes.getNodeAs<NamedDecl>("nameddecl")) {
147-
IdentifierInfo *II = ND->getIdentifier();
147+
const IdentifierInfo *II = ND->getIdentifier();
148148
if (II) {
149-
StringRef NDName = II->getName();
149+
const StringRef NDName = II->getName();
150150
if (hasRTLCharacters(NDName))
151151
diag(ND->getBeginLoc(), "identifier has right-to-left codepoints");
152152
}

clang-tools-extra/clang-tidy/misc/MisplacedConstCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ static QualType guessAlternateQualification(ASTContext &Context, QualType QT) {
4040
Qualifiers Quals = QT.getLocalQualifiers();
4141
Quals.removeConst();
4242

43-
QualType NewQT = Context.getPointerType(
43+
const QualType NewQT = Context.getPointerType(
4444
QualType(QT->getPointeeType().getTypePtr(), Qualifiers::Const));
4545
return NewQT.withCVRQualifiers(Quals.getCVRQualifiers());
4646
}
4747

4848
void MisplacedConstCheck::check(const MatchFinder::MatchResult &Result) {
4949
const auto *Var = Result.Nodes.getNodeAs<ValueDecl>("decl");
5050
ASTContext &Ctx = *Result.Context;
51-
QualType CanQT = Var->getType().getCanonicalType();
51+
const QualType CanQT = Var->getType().getCanonicalType();
5252

5353
SourceLocation AliasLoc;
5454
const char *AliasType = nullptr;

clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ AST_MATCHER(FunctionDecl, isPlacementOverload) {
5151
return true;
5252

5353
const auto *FPT = Node.getType()->castAs<FunctionProtoType>();
54-
ASTContext &Ctx = Node.getASTContext();
54+
const ASTContext &Ctx = Node.getASTContext();
5555
if (Ctx.getLangOpts().SizedDeallocation &&
5656
ASTContext::hasSameType(FPT->getParamType(1), Ctx.getSizeType()))
5757
return false;

clang-tools-extra/clang-tidy/misc/NoRecursionCheck.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ template <typename T, unsigned SmallSize> class SmartSmallSetVector {
122122
}
123123
// Set time!
124124
// Note that this must be after `populateSet()` might have been called.
125-
bool SetInsertionSucceeded = Set.insert(V).second;
125+
const bool SetInsertionSucceeded = Set.insert(V).second;
126126
(void)SetInsertionSucceeded;
127127
assert(SetInsertionSucceeded && "We did check that no such value existed");
128128
return true;
@@ -132,7 +132,7 @@ template <typename T, unsigned SmallSize> class SmartSmallSetVector {
132132
/// Insert a new element into the SmartSmallSetVector.
133133
/// \returns true if the element was inserted into the SmartSmallSetVector.
134134
bool insert(const T &X) {
135-
bool Result = setInsert(X);
135+
const bool Result = setInsert(X);
136136
if (Result)
137137
Vector.push_back(X);
138138
return Result;
@@ -200,8 +200,8 @@ void NoRecursionCheck::handleSCC(ArrayRef<CallGraphNode *> SCC) {
200200
assert(!SCC.empty() && "Empty SCC does not make sense.");
201201

202202
// First of all, call out every strongly connected function.
203-
for (CallGraphNode *N : SCC) {
204-
FunctionDecl *D = N->getDefinition();
203+
for (const CallGraphNode *N : SCC) {
204+
const FunctionDecl *D = N->getDefinition();
205205
diag(D->getLocation(), "function %0 is within a recursive call chain") << D;
206206
}
207207

@@ -224,7 +224,8 @@ void NoRecursionCheck::handleSCC(ArrayRef<CallGraphNode *> SCC) {
224224
assert(CyclicCallStack.size() >= 2 && "Cycle requires at least 2 frames");
225225

226226
// Which function we decided to be the entry point that lead to the recursion?
227-
FunctionDecl *CycleEntryFn = CyclicCallStack.front().Callee->getDefinition();
227+
const FunctionDecl *CycleEntryFn =
228+
CyclicCallStack.front().Callee->getDefinition();
228229
// And now, for ease of understanding, let's print the call sequence that
229230
// forms the cycle in question.
230231
diag(CycleEntryFn->getLocation(),
@@ -233,8 +234,8 @@ void NoRecursionCheck::handleSCC(ArrayRef<CallGraphNode *> SCC) {
233234
<< CycleEntryFn;
234235
for (int CurFrame = 1, NumFrames = CyclicCallStack.size();
235236
CurFrame != NumFrames; ++CurFrame) {
236-
CallGraphNode::CallRecord PrevNode = CyclicCallStack[CurFrame - 1];
237-
CallGraphNode::CallRecord CurrNode = CyclicCallStack[CurFrame];
237+
const CallGraphNode::CallRecord PrevNode = CyclicCallStack[CurFrame - 1];
238+
const CallGraphNode::CallRecord CurrNode = CyclicCallStack[CurFrame];
238239

239240
Decl *PrevDecl = PrevNode.Callee->getDecl();
240241
Decl *CurrDecl = CurrNode.Callee->getDecl();

0 commit comments

Comments
 (0)