[clang-tidy][NFC] Add google-readability-casting check to codebase (#170980)

This commit is contained in:
Baranov Victor 2025-12-06 18:28:06 +03:00 committed by GitHub
parent a805147ac1
commit ff05da6042
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 31 additions and 26 deletions

View File

@ -13,6 +13,7 @@ Checks: >
cppcoreguidelines-missing-std-forward,
cppcoreguidelines-rvalue-reference-param-not-moved,
cppcoreguidelines-virtual-class-destructor,
google-readability-casting,
misc-const-correctness,
modernize-*,
-modernize-avoid-c-arrays,

View File

@ -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));

View File

@ -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",

View File

@ -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.

View File

@ -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(),

View File

@ -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;

View File

@ -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) {

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}