Skip to content

Commit 72f0471

Browse files
clang-tidy: apply modernize-loop-convert over a set of files (project-chip#23443)
* Modernize using: ``` ./scripts/run-clang-tidy-on-compile-commands.py --checks modernize-loop-convert --compile-database out/linux-x64-chip-tool-clang/compile_commands.json --export-fixes out/fixes.xml fix ``` * Fix using: ``` ./scripts/run-clang-tidy-on-compile-commands.py --checks modernize-loop-convert --compile-database out/linux-x64-all-clusters-clang/compile_commands.json --export-fixes out/fixes.xml fix ``` * Modernize using: ``` ./scripts/run-clang-tidy-on-compile-commands.py --checks modernize-loop-convert --compile-database out/linux-x64-tests-clang/compile_commands.json --export-fixes out/fixes.xml fix ``` * Restyle and remove unused variable * Remove more unused variables for array sizes * Restyle * Start some manual renames of iteration values ... automated tool is quite silly * More manual updates for naming of things. Code should be cleaner now * Restyle * Enforce lowercase naming for cdSigningKey iterator variable * One more pass for cleanups Co-authored-by: Andrei Litvin <[email protected]>
1 parent 63a4f27 commit 72f0471

33 files changed

+155
-200
lines changed

examples/chip-tool/commands/common/CHIPCommand.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ void CHIPCommand::MaybeTearDownStack()
166166
// since the CHIP thread and event queue have been stopped, preventing any thread
167167
// races.
168168
//
169-
for (auto it = mCommissioners.begin(); it != mCommissioners.end(); it++)
169+
for (auto & commissioner : mCommissioners)
170170
{
171-
ShutdownCommissioner(it->first);
171+
ShutdownCommissioner(commissioner.first);
172172
}
173173

174174
StopTracing();

examples/chip-tool/commands/common/Command.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ bool Command::InitArguments(int argc, char ** argv)
4242
size_t argvExtraArgsCount = (size_t) argc;
4343
size_t mandatoryArgsCount = 0;
4444
size_t optionalArgsCount = 0;
45-
for (size_t i = 0; i < mArgs.size(); i++)
45+
for (auto & arg : mArgs)
4646
{
47-
if (mArgs[i].isOptional())
47+
if (arg.isOptional())
4848
{
4949
optionalArgsCount++;
5050
}
@@ -877,9 +877,8 @@ void ResetOptionalArg(const Argument & arg)
877877

878878
void Command::ResetArguments()
879879
{
880-
for (size_t i = 0; i < mArgs.size(); i++)
880+
for (const auto & arg : mArgs)
881881
{
882-
const Argument arg = mArgs[i];
883882
const ArgumentType type = arg.type;
884883
if (arg.isOptional())
885884
{

src/app/BufferedReadCallback.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ CHIP_ERROR BufferedReadCallback::GenerateListTLV(TLV::ScopedBufferTLVReader & aR
6565
// To avoid that, a single contiguous buffer is the best likely approach for now.
6666
//
6767
uint32_t totalBufSize = 0;
68-
for (size_t i = 0; i < mBufferedList.size(); i++)
68+
for (const auto & packetBuffer : mBufferedList)
6969
{
70-
totalBufSize += mBufferedList[i]->TotalLength();
70+
totalBufSize += packetBuffer->TotalLength();
7171
}
7272

7373
//

src/app/clusters/general-diagnostics-server/general-diagnostics-server.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ bool IsTestEventTriggerEnabled()
5858

5959
bool IsByteSpanAllZeros(const ByteSpan & byteSpan)
6060
{
61-
for (auto * it = byteSpan.begin(); it != byteSpan.end(); ++it)
61+
for (unsigned char it : byteSpan)
6262
{
63-
if (*it != 0)
63+
if (it != 0)
6464
{
6565
return false;
6666
}

src/app/tests/TestAttributeValueEncoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ void TestEncodeFabricScoped(nlTestSuite * aSuite, void * aContext)
261261

262262
// We tried to encode three items, however, the encoder should only put the item with matching fabric index into the final list.
263263
CHIP_ERROR err = test.encoder.EncodeList([items](const auto & encoder) -> CHIP_ERROR {
264-
for (size_t i = 0; i < 3; i++)
264+
for (const auto & item : items)
265265
{
266-
ReturnErrorOnFailure(encoder.Encode(items[i]));
266+
ReturnErrorOnFailure(encoder.Encode(item));
267267
}
268268
return CHIP_NO_ERROR;
269269
});

src/app/tests/TestPendingNotificationMap.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ void TestAddRemove(nlTestSuite * aSuite, void * aContext)
8080
pendingMap.RemoveAllEntriesForNode(chip::ScopedNodeId());
8181
uint8_t expectedEntryIndecies[] = { 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
8282
iter = pendingMap.begin();
83-
for (size_t i = 0; i < sizeof(expectedEntryIndecies); i++)
83+
for (uint8_t ch : expectedEntryIndecies)
8484
{
8585
PendingNotificationEntry entry = *iter;
86-
NL_TEST_ASSERT(aSuite, entry.mBindingEntryId == expectedEntryIndecies[i]);
86+
NL_TEST_ASSERT(aSuite, entry.mBindingEntryId == ch);
8787
++iter;
8888
}
8989
NL_TEST_ASSERT(aSuite, iter == pendingMap.end());

src/app/tests/TestReadInteraction.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1225,11 +1225,11 @@ void TestReadInteraction::TestSetDirtyBetweenChunks(nlTestSuite * apSuite, void
12251225
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
12261226

12271227
chip::app::AttributePathParams attributePathParams[2];
1228-
for (int i = 0; i < 2; i++)
1228+
for (auto & attributePathParam : attributePathParams)
12291229
{
1230-
attributePathParams[i].mEndpointId = Test::kMockEndpoint3;
1231-
attributePathParams[i].mClusterId = Test::MockClusterId(2);
1232-
attributePathParams[i].mAttributeId = Test::MockAttributeId(4);
1230+
attributePathParam.mEndpointId = Test::kMockEndpoint3;
1231+
attributePathParam.mClusterId = Test::MockClusterId(2);
1232+
attributePathParam.mAttributeId = Test::MockAttributeId(4);
12331233
}
12341234

12351235
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
@@ -2213,10 +2213,10 @@ void TestReadInteraction::TestReadShutdown(nlTestSuite * apSuite, void * apConte
22132213
//
22142214
// Allocate a number of clients
22152215
//
2216-
for (int i = 0; i < 4; i++)
2216+
for (auto & client : pClients)
22172217
{
2218-
pClients[i] = Platform::New<app::ReadClient>(engine, &ctx.GetExchangeManager(), delegate,
2219-
chip::app::ReadClient::InteractionType::Subscribe);
2218+
client = Platform::New<app::ReadClient>(engine, &ctx.GetExchangeManager(), delegate,
2219+
chip::app::ReadClient::InteractionType::Subscribe);
22202220
}
22212221

22222222
//

src/app/tests/suites/pics/PICSBooleanExpressionParser.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ void PICSBooleanExpressionParser::Tokenize(std::string & expression, std::vector
3939

4040
std::string s;
4141

42-
for (size_t i = 0; i < expression.size(); i++)
42+
for (char c : expression)
4343
{
44-
char c = expression[i];
4544
switch (c)
4645
{
4746
case ' ':

src/ble/BleLayer.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,11 @@ CHIP_ERROR BleTransportCapabilitiesRequestMessage::Decode(const PacketBufferHand
203203
VerifyOrReturnError(CAPABILITIES_MSG_CHECK_BYTE_1 == chip::Encoding::Read8(p), BLE_ERROR_INVALID_MESSAGE);
204204
VerifyOrReturnError(CAPABILITIES_MSG_CHECK_BYTE_2 == chip::Encoding::Read8(p), BLE_ERROR_INVALID_MESSAGE);
205205

206-
for (size_t i = 0; i < kCapabilitiesRequestSupportedVersionsLength; i++)
206+
static_assert(kCapabilitiesRequestSupportedVersionsLength == sizeof(msg.mSupportedProtocolVersions),
207+
"Expected capability sizes and storage must match");
208+
for (unsigned char & version : msg.mSupportedProtocolVersions)
207209
{
208-
msg.mSupportedProtocolVersions[i] = chip::Encoding::Read8(p);
210+
version = chip::Encoding::Read8(p);
209211
}
210212

211213
msg.mMtu = chip::Encoding::LittleEndian::Read16(p);

src/controller/tests/TestWriteChunking.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ void TestWriteChunking::TestBadChunking(nlTestSuite * apSuite, void * apContext)
289289
app::WriteClient writeClient(&ctx.GetExchangeManager(), &writeCallback, Optional<uint16_t>::Missing());
290290

291291
ByteSpan list[kTestListLength];
292-
for (uint8_t j = 0; j < kTestListLength; j++)
292+
for (auto & item : list)
293293
{
294-
list[j] = ByteSpan(sByteSpanData, static_cast<uint32_t>(i));
294+
item = ByteSpan(sByteSpanData, static_cast<uint32_t>(i));
295295
}
296296

297297
err = writeClient.EncodeAttribute(attributePath, app::DataModel::List<ByteSpan>(list, kTestListLength));

src/controller/tests/data_model/TestRead.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,11 @@ void TestReadInteraction::TestReadSubscribeAttributeResponseWithCache(nlTestSuit
427427
chip::app::ClusterStateCache cache(delegate);
428428

429429
chip::app::EventPathParams eventPathParams[100];
430-
for (uint32_t index = 0; index < 100; index++)
430+
for (auto & eventPathParam : eventPathParams)
431431
{
432-
eventPathParams[index].mEndpointId = Test::kMockEndpoint3;
433-
eventPathParams[index].mClusterId = Test::MockClusterId(2);
434-
eventPathParams[index].mEventId = 0;
432+
eventPathParam.mEndpointId = Test::kMockEndpoint3;
433+
eventPathParam.mClusterId = Test::MockClusterId(2);
434+
eventPathParam.mEventId = 0;
435435
}
436436

437437
chip::app::ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());

src/credentials/CHIPCert.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,9 @@ ChipDN::~ChipDN() {}
616616

617617
void ChipDN::Clear()
618618
{
619-
for (uint8_t i = 0; i < CHIP_CONFIG_CERT_MAX_RDN_ATTRIBUTES; i++)
619+
for (auto & dn : rdn)
620620
{
621-
rdn[i].Clear();
621+
dn.Clear();
622622
}
623623
}
624624

src/credentials/attestation_verifier/DefaultDeviceAttestationVerifier.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,11 @@ CHIP_ERROR CsaCdKeysTrustStore::AddTrustedKey(const ByteSpan & derCertBytes)
645645
CHIP_ERROR CsaCdKeysTrustStore::LookupVerifyingKey(const ByteSpan & kid, Crypto::P256PublicKey & outPubKey) const
646646
{
647647
// First, search for the well known keys
648-
for (size_t keyIdx = 0; keyIdx < gCdSigningKeys.size(); keyIdx++)
648+
for (auto & cdSigningKey : gCdSigningKeys)
649649
{
650-
if (kid.data_equal(gCdSigningKeys[keyIdx].mKid))
650+
if (kid.data_equal(cdSigningKey.mKid))
651651
{
652-
outPubKey = gCdSigningKeys[keyIdx].mPubkey;
652+
outPubKey = cdSigningKey.mPubkey;
653653
return CHIP_NO_ERROR;
654654
}
655655
}

src/credentials/tests/TestCertificationDeclaration.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,10 @@ static constexpr TestCase sTestCases[] = {
262262
ByteSpan(sTestCMS_CDContent02), ByteSpan(sTestCMS_SignedMessage02) },
263263
};
264264

265-
static constexpr size_t sNumTestCases = ArraySize(sTestCases);
266-
267265
static void TestCD_EncodeDecode(nlTestSuite * inSuite, void * inContext)
268266
{
269-
for (size_t i = 0; i < sNumTestCases; i++)
267+
for (const auto & testCase : sTestCases)
270268
{
271-
const TestCase & testCase = sTestCases[i];
272-
273269
uint8_t encodedCertElemBuf[kCertificationElements_TLVEncodedMaxLength];
274270
MutableByteSpan encodedCDPayload(encodedCertElemBuf);
275271

@@ -379,10 +375,8 @@ static void TestCD_CMSSignAndVerify(nlTestSuite * inSuite, void * inContext)
379375

380376
static void TestCD_CMSVerifyAndExtract(nlTestSuite * inSuite, void * inContext)
381377
{
382-
for (size_t i = 0; i < sNumTestCases; i++)
378+
for (const auto & testCase : sTestCases)
383379
{
384-
const TestCase & testCase = sTestCases[i];
385-
386380
// Verify using signer P256PublicKey
387381
ByteSpan cdContentOut;
388382
NL_TEST_ASSERT(inSuite,
@@ -412,10 +406,8 @@ static void TestCD_CMSVerifyAndExtract(nlTestSuite * inSuite, void * inContext)
412406

413407
static void TestCD_CertificationElementsDecoder(nlTestSuite * inSuite, void * inContext)
414408
{
415-
for (size_t i = 0; i < sNumTestCases; i++)
409+
for (const auto & testCase : sTestCases)
416410
{
417-
const TestCase & testCase = sTestCases[i];
418-
419411
uint8_t encodedCertElemBuf[kCertificationElements_TLVEncodedMaxLength];
420412
MutableByteSpan encodedCDPayload(encodedCertElemBuf);
421413

src/credentials/tests/TestChipCert.cpp

+7-17
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,18 @@ static void TestChipCert_CertValidation(nlTestSuite * inSuite, void * inContext)
428428
{ TestCert::kNode01_01, sGenTBSHashFlag, sNullLoadFlag } } },
429429
};
430430
// clang-format on
431-
static const size_t sNumValidationTestCases = ArraySize(sValidationTestCases);
432431

433-
for (unsigned i = 0; i < sNumValidationTestCases; i++)
432+
for (const auto & testCase : sValidationTestCases)
434433
{
435434
const ChipCertificateData * resultCert = nullptr;
436-
const ValidationTestCase & testCase = sValidationTestCases[i];
437-
438-
err = certSet.Init(kMaxCertsPerTestCase);
435+
err = certSet.Init(kMaxCertsPerTestCase);
439436
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
440437

441-
for (size_t i2 = 0; i2 < kMaxCertsPerTestCase; i2++)
438+
for (auto inputCert : testCase.InputCerts)
442439
{
443-
if (testCase.InputCerts[i2].Type != TestCert::kNone)
440+
if (inputCert.Type != TestCert::kNone)
444441
{
445-
err = LoadTestCert(certSet, testCase.InputCerts[i2].Type, testCase.InputCerts[i2].LoadFlags,
446-
testCase.InputCerts[i2].DecodeFlags);
442+
err = LoadTestCert(certSet, inputCert.Type, inputCert.LoadFlags, inputCert.DecodeFlags);
447443
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
448444
}
449445
}
@@ -1137,11 +1133,8 @@ static void TestChipCert_CertType(nlTestSuite * inSuite, void * inContext)
11371133
{ TestCert::kNode02_02, kCertType_Node },
11381134
};
11391135
// clang-format on
1140-
static const size_t sNumTestCases = ArraySize(sTestCases);
1141-
1142-
for (unsigned i = 0; i < sNumTestCases; i++)
1136+
for (const auto & testCase : sTestCases)
11431137
{
1144-
const TestCase & testCase = sTestCases[i];
11451138
uint8_t certType;
11461139

11471140
err = certSet.Init(1);
@@ -1186,11 +1179,8 @@ static void TestChipCert_CertId(nlTestSuite * inSuite, void * inContext)
11861179
{ TestCert::kNode02_02, 0xDEDEDEDE00020002 },
11871180
};
11881181
// clang-format on
1189-
static const size_t sNumTestCases = ArraySize(sTestCases);
1190-
1191-
for (unsigned i = 0; i < sNumTestCases; i++)
1182+
for (const auto & testCase : sTestCases)
11921183
{
1193-
const TestCase & testCase = sTestCases[i];
11941184
uint64_t chipId;
11951185

11961186
err = certSet.Init(certData, 1);

src/crypto/tests/CHIPCryptoPALTest.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ const AesCtrTestEntry theAesCtrTestVector[] = {
204204
}
205205
};
206206

207-
constexpr size_t kAesCtrTestVectorSize = sizeof(theAesCtrTestVector) / sizeof(theAesCtrTestVector[0]);
208-
209207
constexpr size_t KEY_LENGTH = Crypto::kAES_CCM128_Key_Length;
210208
constexpr size_t NONCE_LENGTH = Crypto::kAES_CCM128_Nonce_Length;
211209

@@ -249,14 +247,13 @@ static void TestAES_CTR_128CryptTestVectors(nlTestSuite * inSuite, void * inCont
249247
{
250248
HeapChecker heapChecker(inSuite);
251249
int numOfTestsRan = 0;
252-
for (size_t vectorIndex = 0; vectorIndex < kAesCtrTestVectorSize; vectorIndex++)
250+
for (const auto & vector : theAesCtrTestVector)
253251
{
254-
const AesCtrTestEntry * vector = &theAesCtrTestVector[vectorIndex];
255-
if (vector->plaintextLen > 0)
252+
if (vector.plaintextLen > 0)
256253
{
257254
numOfTestsRan++;
258-
TestAES_CTR_128_Encrypt(inSuite, vector);
259-
TestAES_CTR_128_Decrypt(inSuite, vector);
255+
TestAES_CTR_128_Encrypt(inSuite, &vector);
256+
TestAES_CTR_128_Decrypt(inSuite, &vector);
260257
}
261258
}
262259
NL_TEST_ASSERT(inSuite, numOfTestsRan > 0);

src/crypto/tests/TestGroupOperationalCredentials.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,19 @@ struct GroupKeySetTestEntry theGroupKeySetTestVector[] = {
8080
},
8181
};
8282

83-
const uint16_t theGroupKeySetTestVectorLength = sizeof(theGroupKeySetTestVector) / sizeof(theGroupKeySetTestVector[0]);
84-
8583
void TestDeriveGroupOperationalCredentials(nlTestSuite * apSuite, void * apContext)
8684
{
8785
GroupOperationalCredentials opCreds;
8886

89-
for (unsigned i = 0; i < theGroupKeySetTestVectorLength; i++)
87+
for (const auto & testVector : theGroupKeySetTestVector)
9088
{
91-
const ByteSpan epochKey(theGroupKeySetTestVector[i].epochKey, KEY_LENGTH);
89+
const ByteSpan epochKey(testVector.epochKey, KEY_LENGTH);
9290
NL_TEST_ASSERT(apSuite,
9391
CHIP_NO_ERROR == Crypto::DeriveGroupOperationalCredentials(epochKey, kCompressedFabricId1, opCreds));
9492

95-
NL_TEST_ASSERT(apSuite, opCreds.hash == theGroupKeySetTestVector[i].groupKeys->hash);
96-
NL_TEST_ASSERT(apSuite,
97-
0 == memcmp(opCreds.encryption_key, theGroupKeySetTestVector[i].groupKeys->encryption_key, KEY_LENGTH));
98-
NL_TEST_ASSERT(apSuite, 0 == memcmp(opCreds.privacy_key, theGroupKeySetTestVector[i].groupKeys->privacy_key, KEY_LENGTH));
93+
NL_TEST_ASSERT(apSuite, opCreds.hash == testVector.groupKeys->hash);
94+
NL_TEST_ASSERT(apSuite, 0 == memcmp(opCreds.encryption_key, testVector.groupKeys->encryption_key, KEY_LENGTH));
95+
NL_TEST_ASSERT(apSuite, 0 == memcmp(opCreds.privacy_key, testVector.groupKeys->privacy_key, KEY_LENGTH));
9996
}
10097
}
10198

src/lib/address_resolve/AddressResolve_DefaultImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ void Resolver::ReArmTimer()
292292
System::Clock::Timestamp now = mTimeSource.GetMonotonicTimestamp();
293293

294294
System::Clock::Timeout nextTimeout = kInvalidTimeout;
295-
for (auto it = mActiveLookups.begin(); it != mActiveLookups.end(); it++)
295+
for (auto & activeLookup : mActiveLookups)
296296
{
297-
System::Clock::Timeout timeout = it->NextEventTimeout(now);
297+
System::Clock::Timeout timeout = activeLookup.NextEventTimeout(now);
298298

299299
if (timeout < nextTimeout)
300300
{

0 commit comments

Comments
 (0)