Skip to content

Commit ff05da6

Browse files
authored
[clang-tidy][NFC] Add google-readability-casting check to codebase (#170980)
1 parent a805147 commit ff05da6

File tree

10 files changed

+31
-26
lines changed

10 files changed

+31
-26
lines changed

clang-tools-extra/clang-tidy/.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Checks: >
1313
cppcoreguidelines-missing-std-forward,
1414
cppcoreguidelines-rvalue-reference-param-not-moved,
1515
cppcoreguidelines-virtual-class-destructor,
16+
google-readability-casting,
1617
misc-const-correctness,
1718
modernize-*,
1819
-modernize-avoid-c-arrays,

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) {
526526
Builder << Qualifiers::fromOpaqueValue(Info.getRawArg(Index));
527527
break;
528528
case clang::DiagnosticsEngine::ak_qualtype:
529-
Builder << QualType::getFromOpaquePtr((void *)Info.getRawArg(Index));
529+
Builder << QualType::getFromOpaquePtr(
530+
reinterpret_cast<void *>(Info.getRawArg(Index)));
530531
break;
531532
case clang::DiagnosticsEngine::ak_declarationname:
532533
Builder << DeclarationName::getFromOpaqueInteger(Info.getRawArg(Index));

clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
6363
const QualType StructFieldTy = StructField->getType();
6464
if (StructFieldTy->isIncompleteType())
6565
return;
66-
const unsigned int StructFieldWidth =
67-
(unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
68-
.Width;
66+
const unsigned int StructFieldWidth = static_cast<unsigned int>(
67+
Result.Context->getTypeInfo(StructFieldTy.getTypePtr()).Width);
6968
FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
7069
// FIXME: Recommend a reorganization of the struct (sort by StructField
7170
// size, largest to smallest).
@@ -79,7 +78,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
7978
CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>(
8079
std::ceil(static_cast<float>(TotalBitSize) / CharSize), 1));
8180
const CharUnits MaxAlign = CharUnits::fromQuantity(
82-
std::ceil((float)Struct->getMaxAlignment() / CharSize));
81+
std::ceil(static_cast<float>(Struct->getMaxAlignment()) / CharSize));
8382
const CharUnits CurrAlign =
8483
Result.Context->getASTRecordLayout(Struct).getAlignment();
8584
const CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
@@ -99,8 +98,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
9998
diag(Struct->getLocation(),
10099
"accessing fields in struct %0 is inefficient due to padding; only "
101100
"needs %1 bytes but is using %2 bytes")
102-
<< Struct << (int)MinByteSize.getQuantity()
103-
<< (int)CurrSize.getQuantity()
101+
<< Struct << MinByteSize.getQuantity() << CurrSize.getQuantity()
104102
<< FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1),
105103
" __attribute__((packed))");
106104
diag(Struct->getLocation(),
@@ -112,8 +110,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
112110

113111
FixItHint FixIt;
114112
auto *Attribute = Struct->getAttr<AlignedAttr>();
115-
const std::string NewAlignQuantity =
116-
std::to_string((int)NewAlign.getQuantity());
113+
const std::string NewAlignQuantity = std::to_string(NewAlign.getQuantity());
117114
if (Attribute) {
118115
FixIt = FixItHint::CreateReplacement(
119116
Attribute->getRange(),
@@ -130,7 +127,7 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
130127
diag(Struct->getLocation(),
131128
"accessing fields in struct %0 is inefficient due to poor alignment; "
132129
"currently aligned to %1 bytes, but recommended alignment is %2 bytes")
133-
<< Struct << (int)CurrAlign.getQuantity() << NewAlignQuantity << FixIt;
130+
<< Struct << CurrAlign.getQuantity() << NewAlignQuantity << FixIt;
134131

135132
diag(Struct->getLocation(),
136133
"use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",

clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,22 @@ bool UnrollLoopsCheck::hasLargeNumIterations(const Stmt *Statement,
208208
return true;
209209
switch (Op->getOpcode()) {
210210
case (BO_AddAssign):
211-
Iterations = std::ceil(float(EndValue - InitValue) / ConstantValue);
211+
Iterations =
212+
std::ceil(static_cast<float>(EndValue - InitValue) / ConstantValue);
212213
break;
213214
case (BO_SubAssign):
214-
Iterations = std::ceil(float(InitValue - EndValue) / ConstantValue);
215+
Iterations =
216+
std::ceil(static_cast<float>(InitValue - EndValue) / ConstantValue);
215217
break;
216218
case (BO_MulAssign):
217-
Iterations =
218-
1 + ((std::log((double)EndValue) - std::log((double)InitValue)) /
219-
std::log((double)ConstantValue));
219+
Iterations = 1 + ((std::log(static_cast<double>(EndValue)) -
220+
std::log(static_cast<double>(InitValue))) /
221+
std::log(static_cast<double>(ConstantValue)));
220222
break;
221223
case (BO_DivAssign):
222-
Iterations =
223-
1 + ((std::log((double)InitValue) - std::log((double)EndValue)) /
224-
std::log((double)ConstantValue));
224+
Iterations = 1 + ((std::log(static_cast<double>(InitValue)) -
225+
std::log(static_cast<double>(EndValue))) /
226+
std::log(static_cast<double>(ConstantValue)));
225227
break;
226228
default:
227229
// All other operators are not handled; assume large bounds.

clang-tools-extra/clang-tidy/bugprone/SuspiciousMissingCommaCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void SuspiciousMissingCommaCheck::check(
116116

117117
// Warn only when concatenation is not common in this initializer list.
118118
// The current threshold is set to less than 1/5 of the string literals.
119-
if (double(Count) / Size > RatioThreshold)
119+
if (static_cast<double>(Count) / Size > RatioThreshold)
120120
return;
121121

122122
diag(ConcatenatedLiteral->getBeginLoc(),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ static llvm::SmallString<64U> skeleton(StringRef Name) {
8181
errs() << "Unicode conversion issue\n";
8282
break;
8383
}
84-
Skeleton.append((char *)BufferStart, (char *)IBuffer);
84+
Skeleton.append(reinterpret_cast<char *>(BufferStart),
85+
reinterpret_cast<char *>(IBuffer));
8586
}
8687
}
8788
return Skeleton;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ static bool containsMisleadingBidi(StringRef Buffer,
5252
}
5353
llvm::UTF32 CodePoint = 0;
5454
const llvm::ConversionResult Result = llvm::convertUTF8Sequence(
55-
(const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)Buffer.end(),
56-
&CodePoint, llvm::strictConversion);
55+
reinterpret_cast<const llvm::UTF8 **>(&CurPtr),
56+
reinterpret_cast<const llvm::UTF8 *>(Buffer.end()), &CodePoint,
57+
llvm::strictConversion);
5758

5859
// If conversion fails, utf-8 is designed so that we can just try next char.
5960
if (Result != llvm::conversionOK) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ static bool hasRTLCharacters(StringRef Buffer) {
125125
while (CurPtr < EndPtr) {
126126
llvm::UTF32 CodePoint = 0;
127127
const llvm::ConversionResult Result = llvm::convertUTF8Sequence(
128-
(const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)EndPtr, &CodePoint,
128+
reinterpret_cast<const llvm::UTF8 **>(&CurPtr),
129+
reinterpret_cast<const llvm::UTF8 *>(EndPtr), &CodePoint,
129130
llvm::strictConversion);
130131
if (Result != llvm::conversionOK)
131132
break;

clang-tools-extra/clang-tidy/openmp/UseDefaultNoneCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ void UseDefaultNoneCheck::check(const MatchFinder::MatchResult &Result) {
3737
"OpenMP directive '%0' specifies 'default(%1)' clause, consider using "
3838
"'default(none)' clause instead")
3939
<< getOpenMPDirectiveName(Directive->getDirectiveKind())
40-
<< getOpenMPSimpleClauseTypeName(Clause->getClauseKind(),
41-
unsigned(Clause->getDefaultKind()));
40+
<< getOpenMPSimpleClauseTypeName(
41+
Clause->getClauseKind(),
42+
llvm::to_underlying(Clause->getDefaultKind()));
4243
diag(Clause->getBeginLoc(), "existing 'default' clause specified here",
4344
DiagnosticIDs::Note);
4445
return;

clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ void FunctionCognitiveComplexityCheck::check(
557557
// Increase, on the other hand, can be 0.
558558

559559
diag(Detail.Loc, Msgs[MsgId], DiagnosticIDs::Note)
560-
<< (unsigned)Increase << (unsigned)Detail.Nesting << 1 + Detail.Nesting;
560+
<< Increase << Detail.Nesting << 1 + Detail.Nesting;
561561
}
562562
}
563563

0 commit comments

Comments
 (0)