https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/170980
>From 43d9db4ce39754077e668554a76ba293b6702105 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Sat, 6 Dec 2025 15:03:07 +0300 Subject: [PATCH] [clang-tidy][NFC] Add google-readability-casting check to codebase --- clang-tools-extra/clang-tidy/.clang-tidy | 1 + .../clang-tidy/ClangTidyDiagnosticConsumer.cpp | 3 ++- .../clang-tidy/altera/StructPackAlignCheck.cpp | 15 ++++++--------- .../clang-tidy/altera/UnrollLoopsCheck.cpp | 18 ++++++++++-------- .../bugprone/SuspiciousMissingCommaCheck.cpp | 2 +- .../misc/ConfusableIdentifierCheck.cpp | 3 ++- .../misc/MisleadingBidirectionalCheck.cpp | 5 +++-- .../misc/MisleadingIdentifierCheck.cpp | 3 ++- .../clang-tidy/openmp/UseDefaultNoneCheck.cpp | 5 +++-- .../FunctionCognitiveComplexityCheck.cpp | 2 +- 10 files changed, 31 insertions(+), 26 deletions(-) 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; } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
