Skip to content

Commit beb732a

Browse files
committed
[build] fix -Wimplicit-int-conversion errors with Clang 21
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.
1 parent 02aaf7c commit beb732a

4 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/core/net/checksum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void Checksum::WriteToMessage(uint16_t aOffset, Message &aMessage) const
8686

8787
if (checksum != 0xffff)
8888
{
89-
checksum = ~checksum;
89+
checksum = static_cast<uint16_t>(~checksum);
9090
}
9191

9292
checksum = BigEndian::HostSwap16(checksum);
@@ -241,7 +241,7 @@ void Checksum::UpdateIp4HeaderChecksum(Ip4::Header &aHeader)
241241

242242
aHeader.SetChecksum(0);
243243
checksum.AddData(reinterpret_cast<const uint8_t *>(&aHeader), sizeof(aHeader));
244-
aHeader.SetChecksum(~checksum.GetValue());
244+
aHeader.SetChecksum(static_cast<uint16_t>(~checksum.GetValue()));
245245
}
246246

247247
} // namespace ot

src/core/net/ip6_address.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool Prefix::ContainsPrefix(const NetworkPrefix &aSubPrefix) const
9898
void Prefix::Tidy(void)
9999
{
100100
uint8_t byteLength = GetBytesSize();
101-
uint8_t lastByteBitMask = ~(static_cast<uint8_t>(1 << (byteLength * 8 - mLength)) - 1);
101+
uint8_t lastByteBitMask = static_cast<uint8_t>(~(static_cast<uint8_t>(1 << (byteLength * 8 - mLength)) - 1));
102102

103103
if (byteLength != 0)
104104
{
@@ -375,7 +375,7 @@ void Address::CopyBits(uint8_t *aDst, const uint8_t *aSrc, uint8_t aNumBits)
375375
// ((0x80 >> 2) - 1) = (0b0010_0000 - 1) = 0b0001_1111
376376

377377
aDst[numBytes] &= mask;
378-
aDst[numBytes] |= (aSrc[numBytes] & ~mask);
378+
aDst[numBytes] |= (aSrc[numBytes] & static_cast<uint8_t>(~mask));
379379
}
380380
}
381381

src/core/thread/link_quality.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int8_t RssAverager::GetAverage(void) const
8585

8686
VerifyOrExit(mCount != 0, average = Radio::kInvalidRssi);
8787

88-
average = -static_cast<int8_t>(mAverage >> kPrecisionBitShift);
88+
average = static_cast<int8_t>(-static_cast<int8_t>(mAverage >> kPrecisionBitShift));
8989

9090
// Check for possible round up (e.g., average of -71.5 --> -72)
9191

src/core/thread/neighbor_table.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ Error NeighborTable::GetNextNeighborInfo(otNeighborInfoIterator &aIterator, Neig
230230

231231
// Negative iterator value gives the current index into mRouters array
232232

233-
for (index = -aIterator; index <= Mle::kMaxRouterId; index++)
233+
for (index = static_cast<int16_t>(-aIterator); index <= Mle::kMaxRouterId; index++)
234234
{
235235
Router *router = Get<RouterTable>().FindRouterById(static_cast<uint8_t>(index));
236236

@@ -239,12 +239,12 @@ Error NeighborTable::GetNextNeighborInfo(otNeighborInfoIterator &aIterator, Neig
239239
aNeighInfo.SetFrom(*router);
240240
aNeighInfo.mIsChild = false;
241241
index++;
242-
aIterator = -index;
242+
aIterator = static_cast<otNeighborInfoIterator>(-index);
243243
ExitNow();
244244
}
245245
}
246246

247-
aIterator = -index;
247+
aIterator = static_cast<otNeighborInfoIterator>(-index);
248248
error = kErrorNotFound;
249249

250250
exit:

0 commit comments

Comments
 (0)