Index: clang-tidy/modernize/LoopConvertUtils.cpp =================================================================== --- clang-tidy/modernize/LoopConvertUtils.cpp +++ clang-tidy/modernize/LoopConvertUtils.cpp @@ -819,6 +819,14 @@ size_t Len = ContainerName.size(); if (Len > 1 && ContainerName.endswith(Style == NS_UpperCase ? "S" : "s")) { IteratorName = ContainerName.substr(0, Len - 1); + // E.g.: (auto thing : things) + if (!declarationExists(IteratorName)) + return IteratorName; + } + + if (Len > 2 && ContainerName.endswith(Style == NS_UpperCase ? "S_" : "s_")) { + IteratorName = ContainerName.substr(0, Len - 2); + // E.g.: (auto thing : things_) if (!declarationExists(IteratorName)) return IteratorName; } @@ -835,14 +843,17 @@ case NS_UpperCase: Elem = "ELEM"; } + // E.g.: (auto elem : container) if (!declarationExists(Elem)) return Elem; IteratorName = AppendWithStyle(ContainerName, OldIndex->getName()); + // E.g.: (auto container_i : container) if (!declarationExists(IteratorName)) return IteratorName; IteratorName = AppendWithStyle(ContainerName, Elem); + // E.g.: (auto container_elem : container) if (!declarationExists(IteratorName)) return IteratorName; Index: test/clang-tidy/modernize-loop-convert-lowercase.cpp =================================================================== --- test/clang-tidy/modernize-loop-convert-lowercase.cpp +++ test/clang-tidy/modernize-loop-convert-lowercase.cpp @@ -7,6 +7,7 @@ const int n = 10; int arr[n]; int nums[n]; +int nums_[n]; void naming() { for (int i = 0; i < n; ++i) { @@ -23,6 +24,13 @@ // CHECK-FIXES: for (auto & num : nums) // CHECK-FIXES-NEXT: printf("%d\n", num); + for (int i = 0; i < n; ++i) { + printf("%d\n", nums_[i]); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & num : nums_) + // CHECK-FIXES-NEXT: printf("%d\n", num); + int num = 0; for (int i = 0; i < n; ++i) { printf("%d\n", nums[i] + num); Index: test/clang-tidy/modernize-loop-convert-uppercase.cpp =================================================================== --- test/clang-tidy/modernize-loop-convert-uppercase.cpp +++ test/clang-tidy/modernize-loop-convert-uppercase.cpp @@ -7,6 +7,7 @@ const int N = 10; int ARR[N]; int NUMS[N]; +int NUMS_[N]; void naming() { for (int I = 0; I < N; ++I) { @@ -23,6 +24,13 @@ // CHECK-FIXES: for (auto & NUM : NUMS) // CHECK-FIXES-NEXT: printf("%d\n", NUM); + for (int I = 0; I < N; ++I) { + printf("%d\n", NUMS_[I]); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & NUM : NUMS_) + // CHECK-FIXES-NEXT: printf("%d\n", NUM); + int NUM = 0; for (int I = 0; I < N; ++I) { printf("%d\n", NUMS[I] + NUM);