Skip to content

Commit 7d6740c

Browse files
authored
[clang-tidy] fix bugprone-too-small-loop-variable warnings (openthread#9321)
1 parent 94822ea commit 7d6740c

14 files changed

+20
-19
lines changed

.clang-tidy

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Checks: >
33
-*,
44
bugprone-argument-comment,
5+
bugprone-too-small-loop-variable,
56
google-explicit-constructor,
67
google-readability-casting,
78
misc-unused-using-decls,

examples/platforms/simulation/flash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void otPlatFlashInit(otInstance *aInstance)
8484

8585
if (create)
8686
{
87-
for (uint8_t index = 0; index < SWAP_NUM; index++)
87+
for (uint8_t index = 0; index < (uint8_t)SWAP_NUM; index++)
8888
{
8989
otPlatFlashErase(aInstance, index);
9090
}

examples/platforms/simulation/radio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ otError ProcessNodeIdFilter(void *aContext, uint8_t aArgsLength, char *aArgs[])
236236
break;
237237
}
238238

239-
for (uint16_t nodeId = 0; nodeId <= MAX_NETWORK_SIZE; nodeId++)
239+
for (uint16_t nodeId = 0; nodeId <= (uint16_t)MAX_NETWORK_SIZE; nodeId++)
240240
{
241241
if (FilterContainsId(nodeId))
242242
{

src/cli/cli.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5256,7 +5256,7 @@ void Interpreter::HandleMeshDiagDiscoverDone(otError aError, otMeshDiagRouterInf
52565256
{
52575257
OutputFormat(kIndentSize, "%u-links:{ ", linkQuality);
52585258

5259-
for (uint8_t id = 0; id < OT_ARRAY_LENGTH(aRouterInfo->mLinkQualities); id++)
5259+
for (uint8_t id = 0; id < static_cast<uint8_t>(OT_ARRAY_LENGTH(aRouterInfo->mLinkQualities)); id++)
52605260
{
52615261
if (aRouterInfo->mLinkQualities[id] == linkQuality)
52625262
{

src/cli/cli_dns.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ otError Dns::ParseDnsServiceMode(const Arg &aArg, otDnsServiceMode &aMode) const
496496
ExitNow();
497497
}
498498

499-
for (uint8_t index = 0; index < OT_ARRAY_LENGTH(kServiceModeStrings); index++)
499+
for (size_t index = 0; index < OT_ARRAY_LENGTH(kServiceModeStrings); index++)
500500
{
501501
if (aArg == kServiceModeStrings[index])
502502
{

src/cli/cli_mac_filter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ template <> otError MacFilter::Process<Cmd("addr")>(Arg aArgs[])
173173
"denylist", // (2) OT_MAC_FILTER_ADDRESS_MODE_DENYLIST
174174
};
175175

176-
for (uint8_t index = 0; index < OT_ARRAY_LENGTH(kModeCommands); index++)
176+
for (size_t index = 0; index < OT_ARRAY_LENGTH(kModeCommands); index++)
177177
{
178178
if (aArgs[0] == kModeCommands[index])
179179
{

src/core/meshcop/meshcop.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool JoinerPskd::operator==(const JoinerPskd &aOther) const
6464
{
6565
bool isEqual = true;
6666

67-
for (uint8_t i = 0; i < sizeof(m8); i++)
67+
for (size_t i = 0; i < sizeof(m8); i++)
6868
{
6969
if (m8[i] != aOther.m8[i])
7070
{

src/core/thread/network_diagnostic.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Error Server::AppendDiagTlv(uint8_t aTlvType, Message &aMessage)
340340

341341
tlv.Init();
342342

343-
for (uint8_t page = 0; page < sizeof(Radio::kSupportedChannelPages) * CHAR_BIT; page++)
343+
for (uint8_t page = 0; page < static_cast<uint8_t>(sizeof(Radio::kSupportedChannelPages) * CHAR_BIT); page++)
344344
{
345345
if (Radio::kSupportedChannelPages & (1 << page))
346346
{

tests/unit/test_cmd_line_parser.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void TestParsingHexStrings(void)
309309

310310
for (uint8_t testIter = 0; testIter <= 1; testIter++)
311311
{
312-
for (uint8_t segmentLen = 1; segmentLen <= sizeof(buffer); segmentLen++)
312+
for (size_t segmentLen = 1; segmentLen <= sizeof(buffer); segmentLen++)
313313
{
314314
if (testIter == 0)
315315
{
@@ -324,7 +324,7 @@ void TestParsingHexStrings(void)
324324

325325
len = segmentLen;
326326

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

329329
while (true)
330330
{

tests/unit/test_hkdf_sha256.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void TestHkdfSha256(void)
150150

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

153-
for (uint16_t i = test->mOutKeyLength + 1; i < sizeof(outKey); i++)
153+
for (size_t i = test->mOutKeyLength + 1; i < sizeof(outKey); i++)
154154
{
155155
VerifyOrQuit(outKey[i] == kFillByte, "HKDF-SHA-256 wrote beyond output key length");
156156
}

tests/unit/test_ip_address.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ bool CheckInterfaceId(const ot::Ip6::Address &aAddress1, const ot::Ip6::Address
366366

367367
bool matches = true;
368368

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

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

411411
address = allZeroAddress;
412412
address.SetPrefix(ip6Prefix);
413-
printf(" prefix-len:%-3d --> %s\n", prefixLength, address.ToString().AsCString());
413+
printf(" prefix-len:%-3zu --> %s\n", prefixLength, address.ToString().AsCString());
414414
VerifyOrQuit(CheckPrefix(address, prefix, prefixLength), "Prefix does not match after SetPrefix()");
415415
VerifyOrQuit(CheckInterfaceId(address, allZeroAddress, prefixLength),
416416
"SetPrefix changed bits beyond the prefix length");

tests/unit/test_mac_frame.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ void TestMacChannelMask(void)
442442
VerifyChannelMaskContent(mask1, allChannels, sizeof(allChannels));
443443

444444
// Test ChannelMask::RemoveChannel()
445-
for (uint8_t index = 0; index < sizeof(allChannels) - 1; index++)
445+
for (size_t index = 0; index < sizeof(allChannels) - 1; index++)
446446
{
447447
mask1.RemoveChannel(allChannels[index]);
448448
VerifyChannelMaskContent(mask1, &allChannels[index + 1], sizeof(allChannels) - 1 - index);

tests/unit/test_nat64.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void DumpMessageInHex(const char *prefix, const uint8_t *aBuf, size_t aBufLen)
5050
{
5151
// This function dumps all packets the output of this function can be imported to packet analyser for debugging.
5252
printf("%s", prefix);
53-
for (uint16_t i = 0; i < aBufLen; i++)
53+
for (size_t i = 0; i < aBufLen; i++)
5454
{
5555
printf("%02x", aBuf[i]);
5656
}
@@ -71,7 +71,7 @@ bool CheckMessage(const Message &aMessage, const uint8_t *aExpectedMessage, size
7171
if (!success)
7272
{
7373
printf("Expected Message\n");
74-
for (uint16_t i = 0; i < aExpectedMessageLen; i++)
74+
for (size_t i = 0; i < aExpectedMessageLen; i++)
7575
{
7676
printf("%02x%c", aExpectedMessage[i], " \n"[(i & 0xf) == 0xf]);
7777
}

tests/unit/test_toolchain.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,22 @@ void test_packed_alignment(void)
132132
packedStruct.mByte = 0xfe;
133133
packedStruct.mUint16 = 0xabcd;
134134

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

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

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

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

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

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

0 commit comments

Comments
 (0)