Skip to content

Commit

Permalink
[clang-tidy] fix bugprone-too-small-loop-variable warnings (#9321)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhui authored Aug 1, 2023
1 parent 94822ea commit 7d6740c
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Checks: >
-*,
bugprone-argument-comment,
bugprone-too-small-loop-variable,
google-explicit-constructor,
google-readability-casting,
misc-unused-using-decls,
Expand Down
2 changes: 1 addition & 1 deletion examples/platforms/simulation/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void otPlatFlashInit(otInstance *aInstance)

if (create)
{
for (uint8_t index = 0; index < SWAP_NUM; index++)
for (uint8_t index = 0; index < (uint8_t)SWAP_NUM; index++)
{
otPlatFlashErase(aInstance, index);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/platforms/simulation/radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ otError ProcessNodeIdFilter(void *aContext, uint8_t aArgsLength, char *aArgs[])
break;
}

for (uint16_t nodeId = 0; nodeId <= MAX_NETWORK_SIZE; nodeId++)
for (uint16_t nodeId = 0; nodeId <= (uint16_t)MAX_NETWORK_SIZE; nodeId++)
{
if (FilterContainsId(nodeId))
{
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5256,7 +5256,7 @@ void Interpreter::HandleMeshDiagDiscoverDone(otError aError, otMeshDiagRouterInf
{
OutputFormat(kIndentSize, "%u-links:{ ", linkQuality);

for (uint8_t id = 0; id < OT_ARRAY_LENGTH(aRouterInfo->mLinkQualities); id++)
for (uint8_t id = 0; id < static_cast<uint8_t>(OT_ARRAY_LENGTH(aRouterInfo->mLinkQualities)); id++)
{
if (aRouterInfo->mLinkQualities[id] == linkQuality)
{
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cli_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ otError Dns::ParseDnsServiceMode(const Arg &aArg, otDnsServiceMode &aMode) const
ExitNow();
}

for (uint8_t index = 0; index < OT_ARRAY_LENGTH(kServiceModeStrings); index++)
for (size_t index = 0; index < OT_ARRAY_LENGTH(kServiceModeStrings); index++)
{
if (aArg == kServiceModeStrings[index])
{
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cli_mac_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ template <> otError MacFilter::Process<Cmd("addr")>(Arg aArgs[])
"denylist", // (2) OT_MAC_FILTER_ADDRESS_MODE_DENYLIST
};

for (uint8_t index = 0; index < OT_ARRAY_LENGTH(kModeCommands); index++)
for (size_t index = 0; index < OT_ARRAY_LENGTH(kModeCommands); index++)
{
if (aArgs[0] == kModeCommands[index])
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/meshcop/meshcop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bool JoinerPskd::operator==(const JoinerPskd &aOther) const
{
bool isEqual = true;

for (uint8_t i = 0; i < sizeof(m8); i++)
for (size_t i = 0; i < sizeof(m8); i++)
{
if (m8[i] != aOther.m8[i])
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/network_diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Error Server::AppendDiagTlv(uint8_t aTlvType, Message &aMessage)

tlv.Init();

for (uint8_t page = 0; page < sizeof(Radio::kSupportedChannelPages) * CHAR_BIT; page++)
for (uint8_t page = 0; page < static_cast<uint8_t>(sizeof(Radio::kSupportedChannelPages) * CHAR_BIT); page++)
{
if (Radio::kSupportedChannelPages & (1 << page))
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_cmd_line_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void TestParsingHexStrings(void)

for (uint8_t testIter = 0; testIter <= 1; testIter++)
{
for (uint8_t segmentLen = 1; segmentLen <= sizeof(buffer); segmentLen++)
for (size_t segmentLen = 1; segmentLen <= sizeof(buffer); segmentLen++)
{
if (testIter == 0)
{
Expand All @@ -324,7 +324,7 @@ void TestParsingHexStrings(void)

len = segmentLen;

printf("\"%s\" segLen:%d -> ", string, segmentLen);
printf("\"%s\" segLen:%zu -> ", string, segmentLen);

while (true)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_hkdf_sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void TestHkdfSha256(void)

VerifyOrQuit(memcmp(outKey, test->mOutKey, test->mOutKeyLength) == 0, "HKDF-SHA-256 failed");

for (uint16_t i = test->mOutKeyLength + 1; i < sizeof(outKey); i++)
for (size_t i = test->mOutKeyLength + 1; i < sizeof(outKey); i++)
{
VerifyOrQuit(outKey[i] == kFillByte, "HKDF-SHA-256 wrote beyond output key length");
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_ip_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ bool CheckInterfaceId(const ot::Ip6::Address &aAddress1, const ot::Ip6::Address

bool matches = true;

for (uint8_t bit = aPrefixLength; bit < sizeof(ot::Ip6::Address) * CHAR_BIT; bit++)
for (size_t bit = aPrefixLength; bit < sizeof(ot::Ip6::Address) * CHAR_BIT; bit++)
{
uint8_t index = bit / CHAR_BIT;
uint8_t mask = (0x80 >> (bit % CHAR_BIT));
Expand Down Expand Up @@ -403,14 +403,14 @@ void TestIp6AddressSetPrefix(void)
memcpy(address.mFields.m8, prefix, sizeof(address));
printf("Prefix is %s\n", address.ToString().AsCString());

for (uint8_t prefixLength = 0; prefixLength <= sizeof(ot::Ip6::Address) * CHAR_BIT; prefixLength++)
for (size_t prefixLength = 0; prefixLength <= sizeof(ot::Ip6::Address) * CHAR_BIT; prefixLength++)
{
ip6Prefix.Clear();
ip6Prefix.Set(prefix, prefixLength);

address = allZeroAddress;
address.SetPrefix(ip6Prefix);
printf(" prefix-len:%-3d --> %s\n", prefixLength, address.ToString().AsCString());
printf(" prefix-len:%-3zu --> %s\n", prefixLength, address.ToString().AsCString());
VerifyOrQuit(CheckPrefix(address, prefix, prefixLength), "Prefix does not match after SetPrefix()");
VerifyOrQuit(CheckInterfaceId(address, allZeroAddress, prefixLength),
"SetPrefix changed bits beyond the prefix length");
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_mac_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void TestMacChannelMask(void)
VerifyChannelMaskContent(mask1, allChannels, sizeof(allChannels));

// Test ChannelMask::RemoveChannel()
for (uint8_t index = 0; index < sizeof(allChannels) - 1; index++)
for (size_t index = 0; index < sizeof(allChannels) - 1; index++)
{
mask1.RemoveChannel(allChannels[index]);
VerifyChannelMaskContent(mask1, &allChannels[index + 1], sizeof(allChannels) - 1 - index);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_nat64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void DumpMessageInHex(const char *prefix, const uint8_t *aBuf, size_t aBufLen)
{
// This function dumps all packets the output of this function can be imported to packet analyser for debugging.
printf("%s", prefix);
for (uint16_t i = 0; i < aBufLen; i++)
for (size_t i = 0; i < aBufLen; i++)
{
printf("%02x", aBuf[i]);
}
Expand All @@ -71,7 +71,7 @@ bool CheckMessage(const Message &aMessage, const uint8_t *aExpectedMessage, size
if (!success)
{
printf("Expected Message\n");
for (uint16_t i = 0; i < aExpectedMessageLen; i++)
for (size_t i = 0; i < aExpectedMessageLen; i++)
{
printf("%02x%c", aExpectedMessage[i], " \n"[(i & 0xf) == 0xf]);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_toolchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@ void test_packed_alignment(void)
packedStruct.mByte = 0xfe;
packedStruct.mUint16 = 0xabcd;

for (uint16_t start = 0; start < sizeof(PackedStruct); start++)
for (size_t start = 0; start < sizeof(PackedStruct); start++)
{
uint8_t *ptr = &buffer[start];

memset(buffer, 0, sizeof(buffer));

*reinterpret_cast<PackedStruct *>(ptr) = packedStruct;

for (uint16_t i = 0; i < start; i++)
for (size_t i = 0; i < start; i++)
{
VerifyOrQuit(buffer[i] == 0, "OT_TOOL_PACKED alignment failed - pre-size write");
}

VerifyOrQuit(memcmp(ptr, packedStructBytes, sizeof(PackedStruct)) == 0, "OT_TOOL_PACKED alignment failed");

for (uint16_t i = start + sizeof(packedStruct); i < sizeof(buffer); i++)
for (size_t i = start + sizeof(packedStruct); i < sizeof(buffer); i++)
{
VerifyOrQuit(buffer[i] == 0, "OT_TOOL_PACKED alignment failed - post-size write");
}
Expand Down

0 comments on commit 7d6740c

Please sign in to comment.