-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy][NFC] Add google-readability-casting check to codebase #170980
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
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 @llvm/pr-subscribers-clang-tools-extra Author: Baranov Victor (vbvictor) ChangesFull diff: https://github.com/llvm/llvm-project/pull/170980.diff 10 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/.clang-tidy b/clang-tools-extra/clang-tidy/.clang-tidy
index 576b4a7b8443e..302a1b3f57941 100644
--- a/clang-tools-extra/clang-tidy/.clang-tidy
+++ b/clang-tools-extra/clang-tidy/.clang-tidy
@@ -9,6 +9,7 @@ Checks: >
-bugprone-narrowing-conversions,
-bugprone-unchecked-optional-access,
-bugprone-unused-return-value,
+ google-readability-casting,
misc-const-correctness,
modernize-*,
-modernize-avoid-c-arrays,
diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
index a8fd499e45c92..16a4d13b9aadb 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -526,7 +526,8 @@ void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) {
Builder << Qualifiers::fromOpaqueValue(Info.getRawArg(Index));
break;
case clang::DiagnosticsEngine::ak_qualtype:
- Builder << QualType::getFromOpaquePtr((void *)Info.getRawArg(Index));
+ Builder << QualType::getFromOpaquePtr(
+ reinterpret_cast<void *>(Info.getRawArg(Index)));
break;
case clang::DiagnosticsEngine::ak_declarationname:
Builder << DeclarationName::getFromOpaqueInteger(Info.getRawArg(Index));
diff --git a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
index 2b1312c8967da..d90305d14c48d 100644
--- a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
@@ -63,9 +63,8 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
const QualType StructFieldTy = StructField->getType();
if (StructFieldTy->isIncompleteType())
return;
- const unsigned int StructFieldWidth =
- (unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
- .Width;
+ const unsigned int StructFieldWidth = static_cast<unsigned int>(
+ Result.Context->getTypeInfo(StructFieldTy.getTypePtr()).Width);
FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
// FIXME: Recommend a reorganization of the struct (sort by StructField
// size, largest to smallest).
@@ -79,7 +78,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>(
std::ceil(static_cast<float>(TotalBitSize) / CharSize), 1));
const CharUnits MaxAlign = CharUnits::fromQuantity(
- std::ceil((float)Struct->getMaxAlignment() / CharSize));
+ std::ceil(static_cast<float>(Struct->getMaxAlignment()) / CharSize));
const CharUnits CurrAlign =
Result.Context->getASTRecordLayout(Struct).getAlignment();
const CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
@@ -99,8 +98,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
diag(Struct->getLocation(),
"accessing fields in struct %0 is inefficient due to padding; only "
"needs %1 bytes but is using %2 bytes")
- << Struct << (int)MinByteSize.getQuantity()
- << (int)CurrSize.getQuantity()
+ << Struct << MinByteSize.getQuantity() << CurrSize.getQuantity()
<< FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1),
" __attribute__((packed))");
diag(Struct->getLocation(),
@@ -112,8 +110,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
FixItHint FixIt;
auto *Attribute = Struct->getAttr<AlignedAttr>();
- const std::string NewAlignQuantity =
- std::to_string((int)NewAlign.getQuantity());
+ const std::string NewAlignQuantity = std::to_string(NewAlign.getQuantity());
if (Attribute) {
FixIt = FixItHint::CreateReplacement(
Attribute->getRange(),
@@ -130,7 +127,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
diag(Struct->getLocation(),
"accessing fields in struct %0 is inefficient due to poor alignment; "
"currently aligned to %1 bytes, but recommended alignment is %2 bytes")
- << Struct << (int)CurrAlign.getQuantity() << NewAlignQuantity << FixIt;
+ << Struct << CurrAlign.getQuantity() << NewAlignQuantity << FixIt;
diag(Struct->getLocation(),
"use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
diff --git a/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp b/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp
index b0cd4cdb41e91..c759e5b5ee7bd 100644
--- a/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp
@@ -208,20 +208,22 @@ bool UnrollLoopsCheck::hasLargeNumIterations(const Stmt *Statement,
return true;
switch (Op->getOpcode()) {
case (BO_AddAssign):
- Iterations = std::ceil(float(EndValue - InitValue) / ConstantValue);
+ Iterations =
+ std::ceil(static_cast<float>(EndValue - InitValue) / ConstantValue);
break;
case (BO_SubAssign):
- Iterations = std::ceil(float(InitValue - EndValue) / ConstantValue);
+ Iterations =
+ std::ceil(static_cast<float>(InitValue - EndValue) / ConstantValue);
break;
case (BO_MulAssign):
- Iterations =
- 1 + ((std::log((double)EndValue) - std::log((double)InitValue)) /
- std::log((double)ConstantValue));
+ Iterations = 1 + ((std::log(static_cast<double>(EndValue)) -
+ std::log(static_cast<double>(InitValue))) /
+ std::log(static_cast<double>(ConstantValue)));
break;
case (BO_DivAssign):
- Iterations =
- 1 + ((std::log((double)InitValue) - std::log((double)EndValue)) /
- std::log((double)ConstantValue));
+ Iterations = 1 + ((std::log(static_cast<double>(InitValue)) -
+ std::log(static_cast<double>(EndValue))) /
+ std::log(static_cast<double>(ConstantValue)));
break;
default:
// All other operators are not handled; assume large bounds.
diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMissingCommaCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMissingCommaCheck.cpp
index cf8bc9794d9ce..4f0d819d2147b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMissingCommaCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMissingCommaCheck.cpp
@@ -116,7 +116,7 @@ void SuspiciousMissingCommaCheck::check(
// Warn only when concatenation is not common in this initializer list.
// The current threshold is set to less than 1/5 of the string literals.
- if (double(Count) / Size > RatioThreshold)
+ if (static_cast<double>(Count) / Size > RatioThreshold)
return;
diag(ConcatenatedLiteral->getBeginLoc(),
diff --git a/clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
index 411e16c87b1c8..418b8ae40ea54 100644
--- a/clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
@@ -81,7 +81,8 @@ static llvm::SmallString<64U> skeleton(StringRef Name) {
errs() << "Unicode conversion issue\n";
break;
}
- Skeleton.append((char *)BufferStart, (char *)IBuffer);
+ Skeleton.append(reinterpret_cast<char *>(BufferStart),
+ reinterpret_cast<char *>(IBuffer));
}
}
return Skeleton;
diff --git a/clang-tools-extra/clang-tidy/misc/MisleadingBidirectionalCheck.cpp b/clang-tools-extra/clang-tidy/misc/MisleadingBidirectionalCheck.cpp
index 4807567710f2d..8a10f70c12f93 100644
--- a/clang-tools-extra/clang-tidy/misc/MisleadingBidirectionalCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/MisleadingBidirectionalCheck.cpp
@@ -52,8 +52,9 @@ static bool containsMisleadingBidi(StringRef Buffer,
}
llvm::UTF32 CodePoint = 0;
const llvm::ConversionResult Result = llvm::convertUTF8Sequence(
- (const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)Buffer.end(),
- &CodePoint, llvm::strictConversion);
+ reinterpret_cast<const llvm::UTF8 **>(&CurPtr),
+ reinterpret_cast<const llvm::UTF8 *>(Buffer.end()), &CodePoint,
+ llvm::strictConversion);
// If conversion fails, utf-8 is designed so that we can just try next char.
if (Result != llvm::conversionOK) {
diff --git a/clang-tools-extra/clang-tidy/misc/MisleadingIdentifierCheck.cpp b/clang-tools-extra/clang-tidy/misc/MisleadingIdentifierCheck.cpp
index 335fffc5d47af..9c0de87f1419b 100644
--- a/clang-tools-extra/clang-tidy/misc/MisleadingIdentifierCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/MisleadingIdentifierCheck.cpp
@@ -125,7 +125,8 @@ static bool hasRTLCharacters(StringRef Buffer) {
while (CurPtr < EndPtr) {
llvm::UTF32 CodePoint = 0;
const llvm::ConversionResult Result = llvm::convertUTF8Sequence(
- (const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)EndPtr, &CodePoint,
+ reinterpret_cast<const llvm::UTF8 **>(&CurPtr),
+ reinterpret_cast<const llvm::UTF8 *>(EndPtr), &CodePoint,
llvm::strictConversion);
if (Result != llvm::conversionOK)
break;
diff --git a/clang-tools-extra/clang-tidy/openmp/UseDefaultNoneCheck.cpp b/clang-tools-extra/clang-tidy/openmp/UseDefaultNoneCheck.cpp
index d02ab728547ae..5bd842be67ef4 100644
--- a/clang-tools-extra/clang-tidy/openmp/UseDefaultNoneCheck.cpp
+++ b/clang-tools-extra/clang-tidy/openmp/UseDefaultNoneCheck.cpp
@@ -37,8 +37,9 @@ void UseDefaultNoneCheck::check(const MatchFinder::MatchResult &Result) {
"OpenMP directive '%0' specifies 'default(%1)' clause, consider using "
"'default(none)' clause instead")
<< getOpenMPDirectiveName(Directive->getDirectiveKind())
- << getOpenMPSimpleClauseTypeName(Clause->getClauseKind(),
- unsigned(Clause->getDefaultKind()));
+ << getOpenMPSimpleClauseTypeName(
+ Clause->getClauseKind(),
+ llvm::to_underlying(Clause->getDefaultKind()));
diag(Clause->getBeginLoc(), "existing 'default' clause specified here",
DiagnosticIDs::Note);
return;
diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
index b42d8a750290e..a966f1f6e24c5 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
@@ -557,7 +557,7 @@ void FunctionCognitiveComplexityCheck::check(
// Increase, on the other hand, can be 0.
diag(Detail.Loc, Msgs[MsgId], DiagnosticIDs::Note)
- << (unsigned)Increase << (unsigned)Detail.Nesting << 1 + Detail.Nesting;
+ << Increase << Detail.Nesting << 1 + Detail.Nesting;
}
}
|
EugeneZelenko
approved these changes
Dec 6, 2025
zeyi2
approved these changes
Dec 6, 2025
localspook
approved these changes
Dec 6, 2025
honeygoyal
pushed a commit
to honeygoyal/llvm-project
that referenced
this pull request
Dec 9, 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.