[build] fix -Wimplicit-int-conversion errors with Clang 21 (#12775)

C++ promotes narrow integer types to int before applying ~ or unary -,
so the result is always int even when the variable being assigned to is
narrower. Clang accepted this silently for years due to a bug in its
range tracking (LLVM #126846, fixed March 2025); Clang 21, now included
in the latest Mac OS for example, correctly flags these as errors.

Add <static_cast> to the destination type at each affected spot.
This commit is contained in:
Suvesh Pratapa
2026-03-26 16:32:55 -05:00
committed by GitHub
parent 274c47ab10
commit cc31b64cc1
4 changed files with 8 additions and 8 deletions
+2 -2
View File
@@ -86,7 +86,7 @@ void Checksum::WriteToMessage(uint16_t aOffset, Message &aMessage) const
if (checksum != 0xffff) if (checksum != 0xffff)
{ {
checksum = ~checksum; checksum = static_cast<uint16_t>(~checksum);
} }
checksum = BigEndian::HostSwap16(checksum); checksum = BigEndian::HostSwap16(checksum);
@@ -241,7 +241,7 @@ void Checksum::UpdateIp4HeaderChecksum(Ip4::Header &aHeader)
aHeader.SetChecksum(0); aHeader.SetChecksum(0);
checksum.AddData(reinterpret_cast<const uint8_t *>(&aHeader), sizeof(aHeader)); checksum.AddData(reinterpret_cast<const uint8_t *>(&aHeader), sizeof(aHeader));
aHeader.SetChecksum(~checksum.GetValue()); aHeader.SetChecksum(static_cast<uint16_t>(~checksum.GetValue()));
} }
} // namespace ot } // namespace ot
+2 -2
View File
@@ -98,7 +98,7 @@ bool Prefix::ContainsPrefix(const NetworkPrefix &aSubPrefix) const
void Prefix::Tidy(void) void Prefix::Tidy(void)
{ {
uint8_t byteLength = GetBytesSize(); uint8_t byteLength = GetBytesSize();
uint8_t lastByteBitMask = ~(static_cast<uint8_t>(1 << (byteLength * 8 - mLength)) - 1); uint8_t lastByteBitMask = static_cast<uint8_t>(~(static_cast<uint8_t>(1 << (byteLength * 8 - mLength)) - 1));
if (byteLength != 0) if (byteLength != 0)
{ {
@@ -375,7 +375,7 @@ void Address::CopyBits(uint8_t *aDst, const uint8_t *aSrc, uint8_t aNumBits)
// ((0x80 >> 2) - 1) = (0b0010_0000 - 1) = 0b0001_1111 // ((0x80 >> 2) - 1) = (0b0010_0000 - 1) = 0b0001_1111
aDst[numBytes] &= mask; aDst[numBytes] &= mask;
aDst[numBytes] |= (aSrc[numBytes] & ~mask); aDst[numBytes] |= (aSrc[numBytes] & static_cast<uint8_t>(~mask));
} }
} }
+1 -1
View File
@@ -85,7 +85,7 @@ int8_t RssAverager::GetAverage(void) const
VerifyOrExit(mCount != 0, average = Radio::kInvalidRssi); VerifyOrExit(mCount != 0, average = Radio::kInvalidRssi);
average = -static_cast<int8_t>(mAverage >> kPrecisionBitShift); average = static_cast<int8_t>(-static_cast<int8_t>(mAverage >> kPrecisionBitShift));
// Check for possible round up (e.g., average of -71.5 --> -72) // Check for possible round up (e.g., average of -71.5 --> -72)
+3 -3
View File
@@ -230,7 +230,7 @@ Error NeighborTable::GetNextNeighborInfo(otNeighborInfoIterator &aIterator, Neig
// Negative iterator value gives the current index into mRouters array // Negative iterator value gives the current index into mRouters array
for (index = -aIterator; index <= Mle::kMaxRouterId; index++) for (index = static_cast<int16_t>(-aIterator); index <= Mle::kMaxRouterId; index++)
{ {
Router *router = Get<RouterTable>().FindRouterById(static_cast<uint8_t>(index)); Router *router = Get<RouterTable>().FindRouterById(static_cast<uint8_t>(index));
@@ -239,12 +239,12 @@ Error NeighborTable::GetNextNeighborInfo(otNeighborInfoIterator &aIterator, Neig
aNeighInfo.SetFrom(*router); aNeighInfo.SetFrom(*router);
aNeighInfo.mIsChild = false; aNeighInfo.mIsChild = false;
index++; index++;
aIterator = -index; aIterator = static_cast<otNeighborInfoIterator>(-index);
ExitNow(); ExitNow();
} }
} }
aIterator = -index; aIterator = static_cast<otNeighborInfoIterator>(-index);
error = kErrorNotFound; error = kErrorNotFound;
exit: exit: