Index: clang-tidy/modernize/LoopConvertCheck.h =================================================================== --- clang-tidy/modernize/LoopConvertCheck.h +++ clang-tidy/modernize/LoopConvertCheck.h @@ -65,6 +65,7 @@ std::unique_ptr TUInfo; Confidence::Level MinConfidence; + VariableNamer::NamingStyle NamingStyle; }; } // namespace modernize Index: clang-tidy/modernize/LoopConvertCheck.cpp =================================================================== --- clang-tidy/modernize/LoopConvertCheck.cpp +++ clang-tidy/modernize/LoopConvertCheck.cpp @@ -406,11 +406,21 @@ Options.get("MinConfidence", "reasonable")) .Case("safe", Confidence::CL_Safe) .Case("risky", Confidence::CL_Risky) - .Default(Confidence::CL_Reasonable)) {} + .Default(Confidence::CL_Reasonable)), + NamingStyle(StringSwitch( + Options.get("NamingStyle", "CamelCase")) + .Case("camelBack", VariableNamer::NS_CamelBack) + .Case("lower_case", VariableNamer::NS_LowerCase) + .Case("UPPER_CASE", VariableNamer::NS_UpperCase) + .Default(VariableNamer::NS_CamelCase)) {} void LoopConvertCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { SmallVector Confs{"risky", "reasonable", "safe"}; Options.store(Opts, "MinConfidence", Confs[static_cast(MinConfidence)]); + + SmallVector Styles{"camelBack", "CamelCase", "lower_case", + "UPPER_CASE"}; + Options.store(Opts, "NamingStyle", Styles[static_cast(NamingStyle)]); } void LoopConvertCheck::registerMatchers(MatchFinder *Finder) { @@ -462,7 +472,7 @@ } else { VariableNamer Namer(&TUInfo->getGeneratedDecls(), &TUInfo->getParentFinder().getStmtToParentStmtMap(), - Loop, IndexVar, MaybeContainer, Context); + Loop, IndexVar, MaybeContainer, Context, NamingStyle); VarName = Namer.createIndexName(); // First, replace all usages of the array subscript expression with our new // variable. Index: clang-tidy/modernize/LoopConvertUtils.h =================================================================== --- clang-tidy/modernize/LoopConvertUtils.h +++ clang-tidy/modernize/LoopConvertUtils.h @@ -414,14 +414,22 @@ /// index, if they exist. class VariableNamer { public: + // Supported naming styles. + enum NamingStyle { + NS_CamelBack, + NS_CamelCase, + NS_LowerCase, + NS_UpperCase, + }; + VariableNamer(StmtGeneratedVarNameMap *GeneratedDecls, const StmtParentMap *ReverseAST, const clang::Stmt *SourceStmt, const clang::VarDecl *OldIndex, const clang::VarDecl *TheContainer, - const clang::ASTContext *Context) + const clang::ASTContext *Context, NamingStyle Style) : GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST), SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer), - Context(Context) {} + Context(Context), Style(Style) {} /// \brief Generate a new index name. /// @@ -437,10 +445,12 @@ const clang::VarDecl *OldIndex; const clang::VarDecl *TheContainer; const clang::ASTContext *Context; + const NamingStyle Style; // Determine whether or not a declaration that would conflict with Symbol // exists in an outer context or in any statement contained in SourceStmt. bool declarationExists(llvm::StringRef Symbol); + }; } // namespace modernize Index: clang-tidy/modernize/LoopConvertUtils.cpp =================================================================== --- clang-tidy/modernize/LoopConvertUtils.cpp +++ clang-tidy/modernize/LoopConvertUtils.cpp @@ -809,40 +809,73 @@ std::string VariableNamer::createIndexName() { // FIXME: Add in naming conventions to handle: - // - Uppercase/lowercase indices. // - How to handle conflicts. // - An interactive process for naming. std::string IteratorName; - std::string ContainerName; + StringRef ContainerName; if (TheContainer) - ContainerName = TheContainer->getName().str(); + ContainerName = TheContainer->getName(); - size_t Len = ContainerName.length(); - if (Len > 1 && ContainerName[Len - 1] == 's') + size_t Len = ContainerName.size(); + if (Len > 1 && ContainerName.endswith(Style == NS_UpperCase ? "S" : "s")) { IteratorName = ContainerName.substr(0, Len - 1); - else - IteratorName = "elem"; + if (!declarationExists(IteratorName)) + return IteratorName; + } - if (!declarationExists(IteratorName)) - return IteratorName; + std::string Elem; + switch (Style) { + case NS_CamelBack: + case NS_LowerCase: + Elem = "elem"; + break; + case NS_CamelCase: + Elem = "Elem"; + break; + case NS_UpperCase: + Elem = "ELEM"; + } + if (!declarationExists(Elem)) + return Elem; - IteratorName = ContainerName + "_" + OldIndex->getName().str(); - if (!declarationExists(IteratorName)) - return IteratorName; + auto AddSuffixToContainer = [&](std::string Suffix) -> std::string { + std::string Name = ContainerName; + if (Style == NS_LowerCase || Style == NS_UpperCase) + Name += "_"; + if (Style == NS_CamelBack) + Suffix[0] = toupper(Suffix[0]); + Name += Suffix; + return Name; + }; - IteratorName = ContainerName + "_elem"; + IteratorName = AddSuffixToContainer(OldIndex->getName().str()); if (!declarationExists(IteratorName)) return IteratorName; - IteratorName += "_elem"; + IteratorName = AddSuffixToContainer(Elem); if (!declarationExists(IteratorName)) return IteratorName; - IteratorName = "_elem_"; - // Someone defeated my naming scheme... - while (declarationExists(IteratorName)) - IteratorName += "i"; + std::string GiveMeName; + switch (Style) { + case NS_CamelBack: + GiveMeName = "giveMeName"; + break; + case NS_CamelCase: + GiveMeName = "GiveMeName"; + break; + case NS_LowerCase: + GiveMeName = "give_me_name_"; + break; + case NS_UpperCase: + GiveMeName = "GIVE_ME_NAME_"; + } + int Attempt = 0; + do { + IteratorName = GiveMeName + std::to_string(Attempt++); + } while (declarationExists(IteratorName)); + return IteratorName; } Index: clang-tidy/modernize/ModernizeTidyModule.cpp =================================================================== --- clang-tidy/modernize/ModernizeTidyModule.cpp +++ clang-tidy/modernize/ModernizeTidyModule.cpp @@ -41,6 +41,7 @@ ClangTidyOptions Options; auto &Opts = Options.CheckOptions; Opts["modernize-loop-convert.MinConfidence"] = "reasonable"; + Opts["modernize-loop-convert.NamingStyle"] = "CamelCase"; Opts["modernize-pass-by-value.IncludeStyle"] = "llvm"; // Also: "google". Opts["modernize-replace-auto-ptr.IncludeStyle"] = "llvm"; // Also: "google". Index: test/clang-tidy/Inputs/modernize-loop-convert/structures.h =================================================================== --- test/clang-tidy/Inputs/modernize-loop-convert/structures.h +++ test/clang-tidy/Inputs/modernize-loop-convert/structures.h @@ -5,7 +5,7 @@ extern int printf(const char *restrict, ...); } -struct Val {int x; void g(); }; +struct Val {int X; void g(); }; struct MutableVal { void constFun(int) const; @@ -13,7 +13,7 @@ void constFun(MutableVal &) const; void constParamFun(const MutableVal &) const; void nonConstParamFun(const MutableVal &); - int x; + int X; }; struct S { @@ -32,7 +32,7 @@ iterator& operator ++(); bool operator!=(const iterator &other); void insert(int); - int x; + int X; }; iterator begin(); iterator end(); @@ -48,13 +48,13 @@ }; iterator begin(); iterator end(); - int x; + int X; }; struct X { - S s; - T t; - U u; + S Ss; + T Tt; + U Uu; S getS(); }; @@ -181,7 +181,7 @@ namespace Macros { struct MacroStruct { - int arr[10]; + int Arr[10]; }; static MacroStruct *MacroSt; #define CONT MacroSt-> Index: test/clang-tidy/modernize-loop-convert-basic.cpp =================================================================== --- test/clang-tidy/modernize-loop-convert-basic.cpp +++ test/clang-tidy/modernize-loop-convert-basic.cpp @@ -6,168 +6,168 @@ const int N = 6; const int NMinusOne = N - 1; -int arr[N] = {1, 2, 3, 4, 5, 6}; -const int constArr[N] = {1, 2, 3, 4, 5, 6}; -int (*pArr)[N] = &arr; +int Arr[N] = {1, 2, 3, 4, 5, 6}; +const int ConstArr[N] = {1, 2, 3, 4, 5, 6}; +int (*PArr)[N] = &Arr; void f() { - int sum = 0; + int Sum = 0; - for (int i = 0; i < N; ++i) { - sum += arr[i]; - int k; + for (int I = 0; I < N; ++I) { + Sum += Arr[I]; + int K; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead [modernize-loop-convert] - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: sum += elem; - // CHECK-FIXES-NEXT: int k; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: Sum += Elem; + // CHECK-FIXES-NEXT: int K; - for (int i = 0; i < N; ++i) { - printf("Fibonacci number is %d\n", arr[i]); - sum += arr[i] + 2; + for (int I = 0; I < N; ++I) { + printf("Fibonacci number is %d\n", Arr[I]); + Sum += Arr[I] + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; - for (int i = 0; i < N; ++i) { - int x = arr[i]; - int y = arr[i] + 2; + for (int I = 0; I < N; ++I) { + int X = Arr[I]; + int Y = Arr[I] + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: int x = elem; - // CHECK-FIXES-NEXT: int y = elem + 2; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: int X = Elem; + // CHECK-FIXES-NEXT: int Y = Elem + 2; - for (int i = 0; i < N; ++i) { - int x = N; - x = arr[i]; + for (int I = 0; I < N; ++I) { + int X = N; + X = Arr[I]; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: int x = N; - // CHECK-FIXES-NEXT: x = elem; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: int X = N; + // CHECK-FIXES-NEXT: X = Elem; - for (int i = 0; i < N; ++i) { - arr[i] += 1; + for (int I = 0; I < N; ++I) { + Arr[I] += 1; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: elem += 1; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: Elem += 1; - for (int i = 0; i < N; ++i) { - int x = arr[i] + 2; - arr[i]++; + for (int I = 0; I < N; ++I) { + int X = Arr[I] + 2; + Arr[I]++; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: int x = elem + 2; - // CHECK-FIXES-NEXT: elem++; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: int X = Elem + 2; + // CHECK-FIXES-NEXT: Elem++; - for (int i = 0; i < N; ++i) { - arr[i] = 4 + arr[i]; + for (int I = 0; I < N; ++I) { + Arr[I] = 4 + Arr[I]; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: elem = 4 + elem; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: Elem = 4 + Elem; - for (int i = 0; i < NMinusOne + 1; ++i) { - sum += arr[i]; + for (int I = 0; I < NMinusOne + 1; ++I) { + Sum += Arr[I]; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: sum += elem; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: Sum += Elem; - for (int i = 0; i < N; ++i) { - printf("Fibonacci number %d has address %p\n", arr[i], &arr[i]); - sum += arr[i] + 2; + for (int I = 0; I < N; ++I) { + printf("Fibonacci number %d has address %p\n", Arr[I], &Arr[I]); + Sum += Arr[I] + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: printf("Fibonacci number %d has address %p\n", elem, &elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: printf("Fibonacci number %d has address %p\n", Elem, &Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; - Val teas[N]; - for (int i = 0; i < N; ++i) { - teas[i].g(); + Val Teas[N]; + for (int I = 0; I < N; ++I) { + Teas[I].g(); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & tea : teas) - // CHECK-FIXES-NEXT: tea.g(); + // CHECK-FIXES: for (auto & Tea : Teas) + // CHECK-FIXES-NEXT: Tea.g(); } void constArray() { - for (int i = 0; i < N; ++i) { - printf("2 * %d = %d\n", constArr[i], constArr[i] + constArr[i]); + for (int I = 0; I < N; ++I) { + printf("2 * %d = %d\n", ConstArr[I], ConstArr[I] + ConstArr[I]); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : constArr) - // CHECK-FIXES-NEXT: printf("2 * %d = %d\n", elem, elem + elem); + // CHECK-FIXES: for (const auto & Elem : ConstArr) + // CHECK-FIXES-NEXT: printf("2 * %d = %d\n", Elem, Elem + Elem); } struct HasArr { int Arr[N]; Val ValArr[N]; void implicitThis() { - for (int i = 0; i < N; ++i) { - printf("%d", Arr[i]); + for (int I = 0; I < N; ++I) { + printf("%d", Arr[I]); } // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: printf("%d", elem); + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: printf("%d", Elem); - for (int i = 0; i < N; ++i) { - printf("%d", ValArr[i].x); + for (int I = 0; I < N; ++I) { + printf("%d", ValArr[I].X); } // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : ValArr) - // CHECK-FIXES-NEXT: printf("%d", elem.x); + // CHECK-FIXES: for (auto & Elem : ValArr) + // CHECK-FIXES-NEXT: printf("%d", Elem.X); } void explicitThis() { - for (int i = 0; i < N; ++i) { - printf("%d", this->Arr[i]); + for (int I = 0; I < N; ++I) { + printf("%d", this->Arr[I]); } // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : this->Arr) - // CHECK-FIXES-NEXT: printf("%d", elem); + // CHECK-FIXES: for (auto & Elem : this->Arr) + // CHECK-FIXES-NEXT: printf("%d", Elem); - for (int i = 0; i < N; ++i) { - printf("%d", this->ValArr[i].x); + for (int I = 0; I < N; ++I) { + printf("%d", this->ValArr[I].X); } // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : this->ValArr) - // CHECK-FIXES-NEXT: printf("%d", elem.x); + // CHECK-FIXES: for (auto & Elem : this->ValArr) + // CHECK-FIXES-NEXT: printf("%d", Elem.X); } }; // Loops whose bounds are value-dependent should not be converted. template void dependentExprBound() { - for (int i = 0; i < N; ++i) - arr[i] = 0; + for (int I = 0; I < N; ++I) + Arr[I] = 0; } template void dependentExprBound<20>(); void memberFunctionPointer() { - Val v; + Val V; void (Val::*mfpArr[N])(void) = {&Val::g}; - for (int i = 0; i < N; ++i) - (v.*mfpArr[i])(); + for (int I = 0; I < N; ++I) + (V.*mfpArr[I])(); // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : mfpArr) - // CHECK-FIXES-NEXT: (v.*elem)(); + // CHECK-FIXES: for (auto & Elem : mfpArr) + // CHECK-FIXES-NEXT: (V.*Elem)(); struct Foo { int (Val::*f)(); - } foo[N]; + } Foo[N]; - for (int i = 0; i < N; ++i) - int r = (v.*(foo[i].f))(); + for (int I = 0; I < N; ++I) + int R = (V.*(Foo[I].f))(); // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : foo) - // CHECK-FIXES-NEXT: int r = (v.*(elem.f))(); + // CHECK-FIXES: for (auto & Elem : Foo) + // CHECK-FIXES-NEXT: int R = (V.*(Elem.f))(); } @@ -177,146 +177,146 @@ void f() { /// begin()/end() - based for loops here: - T t; - for (T::iterator it = t.begin(), e = t.end(); it != e; ++it) { - printf("I found %d\n", *it); + T Tt; + for (T::iterator It = Tt.begin(), E = Tt.end(); It != E; ++It) { + printf("I found %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : t) - // CHECK-FIXES-NEXT: printf("I found %d\n", elem); + // CHECK-FIXES: for (auto & Elem : Tt) + // CHECK-FIXES-NEXT: printf("I found %d\n", Elem); - T *pt; - for (T::iterator it = pt->begin(), e = pt->end(); it != e; ++it) { - printf("I found %d\n", *it); + T *Pt; + for (T::iterator It = Pt->begin(), E = Pt->end(); It != E; ++It) { + printf("I found %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *pt) - // CHECK-FIXES-NEXT: printf("I found %d\n", elem); + // CHECK-FIXES: for (auto & Elem : *Pt) + // CHECK-FIXES-NEXT: printf("I found %d\n", Elem); - S s; - for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) { - printf("s has value %d\n", (*it).x); + S Ss; + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (elem).x); + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); - S *ps; - for (S::iterator it = ps->begin(), e = ps->end(); it != e; ++it) { - printf("s has value %d\n", (*it).x); + S *Ps; + for (S::iterator It = Ps->begin(), E = Ps->end(); It != E; ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & p : *ps) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (p).x); + // CHECK-FIXES: for (auto & P : *Ps) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (P).X); - for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) { - printf("s has value %d\n", it->x); + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + printf("s has value %d\n", It->X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: printf("s has value %d\n", elem.x); + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: printf("s has value %d\n", Elem.X); - for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) { - it->x = 3; + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + It->X = 3; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: elem.x = 3; + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: Elem.X = 3; - for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) { - (*it).x = 3; + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + (*It).X = 3; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: (elem).x = 3; + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: (Elem).X = 3; - for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) { - it->nonConstFun(4, 5); + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + It->nonConstFun(4, 5); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: elem.nonConstFun(4, 5); + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: Elem.nonConstFun(4, 5); - U u; - for (U::iterator it = u.begin(), e = u.end(); it != e; ++it) { - printf("s has value %d\n", it->x); + U Uu; + for (U::iterator It = Uu.begin(), E = Uu.end(); It != E; ++It) { + printf("s has value %d\n", It->X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : u) - // CHECK-FIXES-NEXT: printf("s has value %d\n", elem.x); + // CHECK-FIXES: for (auto & Elem : Uu) + // CHECK-FIXES-NEXT: printf("s has value %d\n", Elem.X); - for (U::iterator it = u.begin(), e = u.end(); it != e; ++it) { - printf("s has value %d\n", (*it).x); + for (U::iterator It = Uu.begin(), E = Uu.end(); It != E; ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : u) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (elem).x); + // CHECK-FIXES: for (auto & Elem : Uu) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); U::iterator A; - for (U::iterator i = u.begin(), e = u.end(); i != e; ++i) - int k = A->x + i->x; + for (U::iterator I = Uu.begin(), E = Uu.end(); I != E; ++I) + int K = A->X + I->X; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : u) - // CHECK-FIXES-NEXT: int k = A->x + elem.x; + // CHECK-FIXES: for (auto & Elem : Uu) + // CHECK-FIXES-NEXT: int K = A->X + Elem.X; - dependent v; - for (dependent::iterator it = v.begin(), e = v.end(); - it != e; ++it) { - printf("Fibonacci number is %d\n", *it); + dependent V; + for (dependent::iterator It = V.begin(), E = V.end(); + It != E; ++It) { + printf("Fibonacci number is %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : v) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); + // CHECK-FIXES: for (auto & Elem : V) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); - for (dependent::iterator it(v.begin()), e = v.end(); - it != e; ++it) { - printf("Fibonacci number is %d\n", *it); + for (dependent::iterator It(V.begin()), E = V.end(); + It != E; ++It) { + printf("Fibonacci number is %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : v) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); + // CHECK-FIXES: for (auto & Elem : V) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); - doublyDependent intmap; - for (doublyDependent::iterator it = intmap.begin(), e = intmap.end(); - it != e; ++it) { - printf("intmap[%d] = %d", it->first, it->second); + doublyDependent Intmap; + for (doublyDependent::iterator It = Intmap.begin(), E = Intmap.end(); + It != E; ++It) { + printf("Intmap[%d] = %d", It->first, It->second); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : intmap) - // CHECK-FIXES: printf("intmap[%d] = %d", elem.first, elem.second); + // CHECK-FIXES: for (auto & Elem : Intmap) + // CHECK-FIXES: printf("Intmap[%d] = %d", Elem.first, Elem.second); // PtrSet's iterator dereferences by value so auto & can't be used. { - PtrSet val_int_ptrs; - for (PtrSet::iterator I = val_int_ptrs.begin(), - E = val_int_ptrs.end(); + PtrSet Val_int_ptrs; + for (PtrSet::iterator I = Val_int_ptrs.begin(), + E = Val_int_ptrs.end(); I != E; ++I) { (void) *I; } // CHECK-MESSAGES: :[[@LINE-5]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto val_int_ptr : val_int_ptrs) + // CHECK-FIXES: for (auto Val_int_ptr : Val_int_ptrs) } // This container uses an iterator where the derefence type is a typedef of // a reference type. Make sure non-const auto & is still used. A failure here // means canonical types aren't being tested. { - TypedefDerefContainer int_ptrs; - for (TypedefDerefContainer::iterator I = int_ptrs.begin(), - E = int_ptrs.end(); + TypedefDerefContainer Int_ptrs; + for (TypedefDerefContainer::iterator I = Int_ptrs.begin(), + E = Int_ptrs.end(); I != E; ++I) { (void) *I; } // CHECK-MESSAGES: :[[@LINE-5]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & int_ptr : int_ptrs) + // CHECK-FIXES: for (auto & Int_ptr : Int_ptrs) } { // Iterators returning an rvalue reference should disqualify the loop from // transformation. - RValueDerefContainer container; - for (RValueDerefContainer::iterator I = container.begin(), - E = container.end(); + RValueDerefContainer Container; + for (RValueDerefContainer::iterator I = Container.begin(), + E = Container.end(); I != E; ++I) { (void) *I; } @@ -326,36 +326,36 @@ // Tests to verify the proper use of auto where the init variable type and the // initializer type differ or are mostly the same except for const qualifiers. void different_type() { - // s.begin() returns a type 'iterator' which is just a non-const pointer and + // Ss.begin() returns a type 'iterator' which is just a non-const pointer and // differs from const_iterator only on the const qualification. - S s; - for (S::const_iterator it = s.begin(), e = s.end(); it != e; ++it) { - printf("s has value %d\n", (*it).x); + S Ss; + for (S::const_iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : s) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (elem).x); + // CHECK-FIXES: for (const auto & Elem : Ss) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); - S *ps; - for (S::const_iterator it = ps->begin(), e = ps->end(); it != e; ++it) { - printf("s has value %d\n", (*it).x); + S *Ps; + for (S::const_iterator It = Ps->begin(), E = Ps->end(); It != E; ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & p : *ps) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (p).x); + // CHECK-FIXES: for (const auto & P : *Ps) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (P).X); - // v.begin() returns a user-defined type 'iterator' which, since it's + // V.begin() returns a user-defined type 'iterator' which, since it's // different from const_iterator, disqualifies these loops from // transformation. - dependent v; - for (dependent::const_iterator it = v.begin(), e = v.end(); - it != e; ++it) { - printf("Fibonacci number is %d\n", *it); + dependent V; + for (dependent::const_iterator It = V.begin(), E = V.end(); + It != E; ++It) { + printf("Fibonacci number is %d\n", *It); } - for (dependent::const_iterator it(v.begin()), e = v.end(); - it != e; ++it) { - printf("Fibonacci number is %d\n", *it); + for (dependent::const_iterator It(V.begin()), E = V.end(); + It != E; ++It) { + printf("Fibonacci number is %d\n", *It); } } @@ -379,12 +379,12 @@ for (iterator I = begin(), E = end(); I != E; ++I) (void) *I; // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *this) + // CHECK-FIXES: for (auto & Elem : *this) for (iterator I = C::begin(), E = C::end(); I != E; ++I) (void) *I; // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *this) + // CHECK-FIXES: for (auto & Elem : *this) for (iterator I = begin(), E = end(); I != E; ++I) { (void) *I; @@ -394,7 +394,7 @@ for (iterator I = begin(); I != end(); ++I) (void) *I; // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *this) + // CHECK-FIXES: for (auto & Elem : *this) for (iterator I = begin(); I != end(); ++I) { (void) *I; @@ -406,12 +406,12 @@ for (const_iterator I = begin(), E = end(); I != E; ++I) (void) *I; // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : *this) + // CHECK-FIXES: for (const auto & Elem : *this) for (const_iterator I = C::begin(), E = C::end(); I != E; ++I) (void) *I; // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : *this) + // CHECK-FIXES: for (const auto & Elem : *this) for (const_iterator I = begin(), E = end(); I != E; ++I) { (void) *I; @@ -433,7 +433,7 @@ for (iterator I = begin(), E = end(); I != E; ++I) (void) *I; // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *this) + // CHECK-FIXES: for (auto & Elem : *this) } }; @@ -442,114 +442,114 @@ namespace PseudoArray { const int N = 6; -dependent v; -dependent *pv; -const dependent constv; -const dependent *pconstv; +dependent V; +dependent *Pv; +const dependent Constv; +const dependent *Pconstv; -transparent> cv; +transparent> Cv; void f() { - int sum = 0; - for (int i = 0, e = v.size(); i < e; ++i) { - printf("Fibonacci number is %d\n", v[i]); - sum += v[i] + 2; + int Sum = 0; + for (int I = 0, E = V.size(); I < E; ++I) { + printf("Fibonacci number is %d\n", V[I]); + Sum += V[I] + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : v) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (auto & Elem : V) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; - for (int i = 0, e = v.size(); i < e; ++i) { - printf("Fibonacci number is %d\n", v.at(i)); - sum += v.at(i) + 2; + for (int I = 0, E = V.size(); I < E; ++I) { + printf("Fibonacci number is %d\n", V.at(I)); + Sum += V.at(I) + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : v) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (auto & Elem : V) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; - for (int i = 0, e = pv->size(); i < e; ++i) { - printf("Fibonacci number is %d\n", pv->at(i)); - sum += pv->at(i) + 2; + for (int I = 0, E = Pv->size(); I < E; ++I) { + printf("Fibonacci number is %d\n", Pv->at(I)); + Sum += Pv->at(I) + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *pv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (auto & Elem : *Pv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; // This test will fail if size() isn't called repeatedly, since it // returns unsigned int, and 0 is deduced to be signed int. // FIXME: Insert the necessary explicit conversion, or write out the types // explicitly. - for (int i = 0; i < pv->size(); ++i) { - printf("Fibonacci number is %d\n", (*pv).at(i)); - sum += (*pv)[i] + 2; + for (int I = 0; I < Pv->size(); ++I) { + printf("Fibonacci number is %d\n", (*Pv).at(I)); + Sum += (*Pv)[I] + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *pv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (auto & Elem : *Pv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; - for (int i = 0; i < cv->size(); ++i) { - printf("Fibonacci number is %d\n", cv->at(i)); - sum += cv->at(i) + 2; + for (int I = 0; I < Cv->size(); ++I) { + printf("Fibonacci number is %d\n", Cv->at(I)); + Sum += Cv->at(I) + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *cv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (auto & Elem : *Cv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; } void constness() { - int sum = 0; - for (int i = 0, e = constv.size(); i < e; ++i) { - printf("Fibonacci number is %d\n", constv[i]); - sum += constv[i] + 2; + int Sum = 0; + for (int I = 0, E = Constv.size(); I < E; ++I) { + printf("Fibonacci number is %d\n", Constv[I]); + Sum += Constv[I] + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : constv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (const auto & Elem : Constv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; - for (int i = 0, e = constv.size(); i < e; ++i) { - printf("Fibonacci number is %d\n", constv.at(i)); - sum += constv.at(i) + 2; + for (int I = 0, E = Constv.size(); I < E; ++I) { + printf("Fibonacci number is %d\n", Constv.at(I)); + Sum += Constv.at(I) + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : constv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (const auto & Elem : Constv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; - for (int i = 0, e = pconstv->size(); i < e; ++i) { - printf("Fibonacci number is %d\n", pconstv->at(i)); - sum += pconstv->at(i) + 2; + for (int I = 0, E = Pconstv->size(); I < E; ++I) { + printf("Fibonacci number is %d\n", Pconstv->at(I)); + Sum += Pconstv->at(I) + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : *pconstv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (const auto & Elem : *Pconstv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; // This test will fail if size() isn't called repeatedly, since it // returns unsigned int, and 0 is deduced to be signed int. // FIXME: Insert the necessary explicit conversion, or write out the types // explicitly. - for (int i = 0; i < pconstv->size(); ++i) { - printf("Fibonacci number is %d\n", (*pconstv).at(i)); - sum += (*pconstv)[i] + 2; + for (int I = 0; I < Pconstv->size(); ++I) { + printf("Fibonacci number is %d\n", (*Pconstv).at(I)); + Sum += (*Pconstv)[I] + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : *pconstv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); - // CHECK-FIXES-NEXT: sum += elem + 2; + // CHECK-FIXES: for (const auto & Elem : *Pconstv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2; } // Check for loops that don't mention containers. void noContainer() { - for (auto i = 0; i < v.size(); ++i) { + for (auto I = 0; I < V.size(); ++I) { } - for (auto i = 0; i < v.size(); ++i) + for (auto I = 0; I < V.size(); ++I) ; } @@ -581,26 +581,26 @@ // begin() and end() of the right const-ness. void NoBeginEndTest() { NoBeginEnd NBE; - for (unsigned i = 0, e = NBE.size(); i < e; ++i) - printf("%d\n", NBE[i]); + for (unsigned I = 0, E = NBE.size(); I < E; ++I) + printf("%d\n", NBE[I]); - const NoConstBeginEnd const_NCBE; - for (unsigned i = 0, e = const_NCBE.size(); i < e; ++i) - printf("%d\n", const_NCBE[i]); + const NoConstBeginEnd Const_NCBE; + for (unsigned I = 0, E = Const_NCBE.size(); I < E; ++I) + printf("%d\n", Const_NCBE[I]); ConstBeginEnd CBE; - for (unsigned i = 0, e = CBE.size(); i < e; ++i) - printf("%d\n", CBE[i]); + for (unsigned I = 0, E = CBE.size(); I < E; ++I) + printf("%d\n", CBE[I]); // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : CBE) - // CHECK-FIXES-NEXT: printf("%d\n", elem); + // CHECK-FIXES: for (auto & Elem : CBE) + // CHECK-FIXES-NEXT: printf("%d\n", Elem); - const ConstBeginEnd const_CBE; - for (unsigned i = 0, e = const_CBE.size(); i < e; ++i) - printf("%d\n", const_CBE[i]); + const ConstBeginEnd Const_CBE; + for (unsigned I = 0, E = Const_CBE.size(); I < E; ++I) + printf("%d\n", Const_CBE[I]); // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : const_CBE) - // CHECK-FIXES-NEXT: printf("%d\n", elem); + // CHECK-FIXES: for (const auto & Elem : Const_CBE) + // CHECK-FIXES-NEXT: printf("%d\n", Elem); } struct DerefByValue { @@ -614,21 +614,21 @@ void derefByValueTest() { DerefByValue DBV; - for (unsigned i = 0, e = DBV.size(); i < e; ++i) { - printf("%d\n", DBV[i]); + for (unsigned I = 0, E = DBV.size(); I < E; ++I) { + printf("%d\n", DBV[I]); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto elem : DBV) - // CHECK-FIXES-NEXT: printf("%d\n", elem); + // CHECK-FIXES: for (auto Elem : DBV) + // CHECK-FIXES-NEXT: printf("%d\n", Elem); - for (unsigned i = 0, e = DBV.size(); i < e; ++i) { - auto f = [DBV, i]() {}; - printf("%d\n", DBV[i]); + for (unsigned I = 0, E = DBV.size(); I < E; ++I) { + auto f = [DBV, I]() {}; + printf("%d\n", DBV[I]); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto elem : DBV) - // CHECK-FIXES-NEXT: auto f = [DBV, &elem]() {}; - // CHECK-FIXES-NEXT: printf("%d\n", elem); + // CHECK-FIXES: for (auto Elem : DBV) + // CHECK-FIXES-NEXT: auto f = [DBV, &Elem]() {}; + // CHECK-FIXES-NEXT: printf("%d\n", Elem); } } // namespace PseudoArray Index: test/clang-tidy/modernize-loop-convert-camelback.cpp =================================================================== --- /dev/null +++ test/clang-tidy/modernize-loop-convert-camelback.cpp @@ -0,0 +1,49 @@ +// RUN: %python %S/check_clang_tidy.py %s modernize-loop-convert %t \ +// RUN: -config="{CheckOptions: [{key: modernize-loop-convert.NamingStyle, value: 'camelBack'}]}" \ +// RUN: -- -std=c++11 -I %S/Inputs/modernize-loop-convert + +#include "structures.h" + +const int n = 10; +int arr[n]; +int nums[n]; + +void naming() { + for (int i = 0; i < n; ++i) { + printf("%d\n", arr[i]); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead [modernize-loop-convert] + // CHECK-FIXES: for (auto & elem : arr) + // CHECK-FIXES-NEXT: printf("%d\n", elem); + + 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); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & elem : nums) + // CHECK-FIXES-NEXT: printf("%d\n", elem + num); + + int elem = 0; + for (int i = 0; i < n; ++i) { + printf("%d\n", nums[i] + num + elem); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & numsI : nums) + // CHECK-FIXES-NEXT: printf("%d\n", numsI + num + elem); + + int numsI = 0; + for (int i = 0; i < n; ++i) { + printf("%d\n", nums[i] + num + elem + numsI); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & numsElem : nums) + // CHECK-FIXES-NEXT: printf("%d\n", numsElem + num + elem + numsI); +} Index: test/clang-tidy/modernize-loop-convert-extra.cpp =================================================================== --- test/clang-tidy/modernize-loop-convert-extra.cpp +++ test/clang-tidy/modernize-loop-convert-extra.cpp @@ -7,20 +7,20 @@ void f() { const int N = 6; const int M = 8; - int arr[N][M]; + int Arr[N][M]; - for (int i = 0; i < N; ++i) { - int a = 0; - int b = arr[i][a]; + for (int I = 0; I < N; ++I) { + int A = 0; + int B = Arr[I][A]; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: int a = 0; - // CHECK-FIXES-NEXT: int b = elem[a]; - - for (int j = 0; j < M; ++j) { - int a = 0; - int b = arr[a][j]; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: int A = 0; + // CHECK-FIXES-NEXT: int B = Elem[A]; + + for (int J = 0; J < M; ++J) { + int A = 0; + int B = Arr[A][J]; } } @@ -31,8 +31,8 @@ const int N = 10; Val Arr[N]; -dependent v; -dependent *pv; +dependent V; +dependent *Pv; Val &func(Val &); void sideEffect(int); @@ -41,131 +41,131 @@ // variable to hold each element, we can name the new variable for the // converted range-based loop as the temporary variable's name. - // In the following case, "t" is used as a temporary variable to hold each - // element, and thus we consider the name "t" aliased to the loop. + // In the following case, "T" is used as a temporary variable to hold each + // element, and thus we consider the name "T" aliased to the loop. // The extra blank braces are left as a placeholder for after the variable // declaration is deleted. - for (int i = 0; i < N; ++i) { - Val &t = Arr[i]; + for (int I = 0; I < N; ++I) { + Val &T = Arr[I]; {} - int y = t.x; + int Y = T.X; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & t : Arr) + // CHECK-FIXES: for (auto & T : Arr) // CHECK-FIXES-NOT: Val &{{[a-z_]+}} = // CHECK-FIXES: {} - // CHECK-FIXES-NEXT: int y = t.x; + // CHECK-FIXES-NEXT: int Y = T.X; // The container was not only used to initialize a temporary loop variable for // the container's elements, so we do not alias the new loop variable. - for (int i = 0; i < N; ++i) { - Val &t = Arr[i]; - int y = t.x; - int z = Arr[i].x + t.x; - } - // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: Val &t = elem; - // CHECK-FIXES-NEXT: int y = t.x; - // CHECK-FIXES-NEXT: int z = elem.x + t.x; - - for (int i = 0; i < N; ++i) { - Val t = Arr[i]; - int y = t.x; - int z = Arr[i].x + t.x; - } - // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: Val t = elem; - // CHECK-FIXES-NEXT: int y = t.x; - // CHECK-FIXES-NEXT: int z = elem.x + t.x; + for (int I = 0; I < N; ++I) { + Val &T = Arr[I]; + int Y = T.X; + int Z = Arr[I].X + T.X; + } + // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: Val &T = Elem; + // CHECK-FIXES-NEXT: int Y = T.X; + // CHECK-FIXES-NEXT: int Z = Elem.X + T.X; + + for (int I = 0; I < N; ++I) { + Val T = Arr[I]; + int Y = T.X; + int Z = Arr[I].X + T.X; + } + // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: Val T = Elem; + // CHECK-FIXES-NEXT: int Y = T.X; + // CHECK-FIXES-NEXT: int Z = Elem.X + T.X; // The same for pseudo-arrays like std::vector (or here dependent) // which provide a subscript operator[]. - for (int i = 0; i < v.size(); ++i) { - Val &t = v[i]; + for (int I = 0; I < V.size(); ++I) { + Val &T = V[I]; {} - int y = t.x; + int Y = T.X; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & t : v) + // CHECK-FIXES: for (auto & T : V) // CHECK-FIXES: {} - // CHECK-FIXES-NEXT: int y = t.x; + // CHECK-FIXES-NEXT: int Y = T.X; // The same with a call to at() - for (int i = 0; i < pv->size(); ++i) { - Val &t = pv->at(i); + for (int I = 0; I < Pv->size(); ++I) { + Val &T = Pv->at(I); {} - int y = t.x; + int Y = T.X; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & t : *pv) + // CHECK-FIXES: for (auto & T : *Pv) // CHECK-FIXES: {} - // CHECK-FIXES-NEXT: int y = t.x; + // CHECK-FIXES-NEXT: int Y = T.X; - for (int i = 0; i < N; ++i) { - Val &t = func(Arr[i]); - int y = t.x; + for (int I = 0; I < N; ++I) { + Val &T = func(Arr[I]); + int Y = T.X; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: Val &t = func(elem); - // CHECK-FIXES-NEXT: int y = t.x; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: Val &T = func(Elem); + // CHECK-FIXES-NEXT: int Y = T.X; int IntArr[N]; - for (unsigned i = 0; i < N; ++i) { - if (int alias = IntArr[i]) { - sideEffect(alias); + for (unsigned I = 0; I < N; ++I) { + if (int Alias = IntArr[I]) { + sideEffect(Alias); } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto alias : IntArr) - // CHECK-FIXES-NEXT: if (alias) + // CHECK-FIXES: for (auto Alias : IntArr) + // CHECK-FIXES-NEXT: if (Alias) - for (unsigned i = 0; i < N; ++i) { - while (int alias = IntArr[i]) { - sideEffect(alias); + for (unsigned I = 0; I < N; ++I) { + while (int Alias = IntArr[I]) { + sideEffect(Alias); } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto alias : IntArr) - // CHECK-FIXES-NEXT: while (alias) + // CHECK-FIXES: for (auto Alias : IntArr) + // CHECK-FIXES-NEXT: while (Alias) - for (unsigned i = 0; i < N; ++i) { - switch (int alias = IntArr[i]) { + for (unsigned I = 0; I < N; ++I) { + switch (int Alias = IntArr[I]) { default: - sideEffect(alias); + sideEffect(Alias); } } // CHECK-MESSAGES: :[[@LINE-6]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto alias : IntArr) - // CHECK-FIXES-NEXT: switch (alias) + // CHECK-FIXES: for (auto Alias : IntArr) + // CHECK-FIXES-NEXT: switch (Alias) - for (unsigned i = 0; i < N; ++i) { - for (int alias = IntArr[i]; alias < N; ++alias) { - sideEffect(alias); + for (unsigned I = 0; I < N; ++I) { + for (int Alias = IntArr[I]; Alias < N; ++Alias) { + sideEffect(Alias); } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto alias : IntArr) - // CHECK-FIXES-NEXT: for (; alias < N; ++alias) + // CHECK-FIXES: for (auto Alias : IntArr) + // CHECK-FIXES-NEXT: for (; Alias < N; ++Alias) - for (unsigned i = 0; i < N; ++i) { - for (unsigned j = 0; int alias = IntArr[i]; ++j) { - sideEffect(alias); + for (unsigned I = 0; I < N; ++I) { + for (unsigned J = 0; int Alias = IntArr[I]; ++J) { + sideEffect(Alias); } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto alias : IntArr) - // CHECK-FIXES-NEXT: for (unsigned j = 0; alias; ++j) + // CHECK-FIXES: for (auto Alias : IntArr) + // CHECK-FIXES-NEXT: for (unsigned J = 0; Alias; ++J) struct IntRef { IntRef(const int& i); }; - for (int i = 0; i < N; ++i) { - IntRef Int(IntArr[i]); + for (int I = 0; I < N; ++I) { + IntRef Int(IntArr[I]); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : IntArr) - // CHECK-FIXES-NEXT: IntRef Int(elem); + // CHECK-FIXES: for (auto & Elem : IntArr) + // CHECK-FIXES-NEXT: IntRef Int(Elem); } @@ -175,56 +175,56 @@ // variable was declared as a value, the loop variable will be declared as a // value and vice versa for references. - S s; - const S s_const = s; + S Ss; + const S S_const = Ss; - for (S::const_iterator it = s_const.begin(); it != s_const.end(); ++it) { - MutableVal alias = *it; + for (S::const_iterator It = S_const.begin(); It != S_const.end(); ++It) { + MutableVal Alias = *It; {} - alias.x = 0; + Alias.X = 0; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto alias : s_const) + // CHECK-FIXES: for (auto Alias : S_const) // CHECK-FIXES-NOT: MutableVal {{[a-z_]+}} = // CHECK-FIXES: {} - // CHECK-FIXES-NEXT: alias.x = 0; + // CHECK-FIXES-NEXT: Alias.X = 0; - for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) { - MutableVal alias = *it; + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + MutableVal Alias = *It; {} - alias.x = 0; + Alias.X = 0; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto alias : s) + // CHECK-FIXES: for (auto Alias : Ss) // CHECK-FIXES-NOT: MutableVal {{[a-z_]+}} = // CHECK-FIXES: {} - // CHECK-FIXES-NEXT: alias.x = 0; + // CHECK-FIXES-NEXT: Alias.X = 0; - for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) { - MutableVal &alias = *it; + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { + MutableVal &Alias = *It; {} - alias.x = 0; + Alias.X = 0; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & alias : s) + // CHECK-FIXES: for (auto & Alias : Ss) // CHECK-FIXES-NOT: MutableVal &{{[a-z_]+}} = // CHECK-FIXES: {} - // CHECK-FIXES-NEXT: alias.x = 0; + // CHECK-FIXES-NEXT: Alias.X = 0; - dependent dep, other; - for (dependent::iterator it = dep.begin(), e = dep.end(); it != e; ++it) { - printf("%d\n", *it); - const int& idx = other[0]; - unsigned othersize = other.size(); - } - // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : dep) - // CHECK-FIXES-NEXT: printf("%d\n", elem); - // CHECK-FIXES-NEXT: const int& idx = other[0]; - // CHECK-FIXES-NEXT: unsigned othersize = other.size(); + dependent Dep, Other; + for (dependent::iterator It = Dep.begin(), E = Dep.end(); It != E; ++It) { + printf("%d\n", *It); + const int& Idx = Other[0]; + unsigned Othersize = Other.size(); + } + // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & Elem : Dep) + // CHECK-FIXES-NEXT: printf("%d\n", Elem); + // CHECK-FIXES-NEXT: const int& Idx = Other[0]; + // CHECK-FIXES-NEXT: unsigned Othersize = Other.size(); - for (int i = 0, e = dep.size(); i != e; ++i) { - int idx = other.at(i); + for (int I = 0, E = Dep.size(); I != E; ++I) { + int Idx = Other.at(I); } } @@ -236,126 +236,138 @@ #define DEF 5 const int N = 10; -int nums[N]; -int sum = 0; +int Nums[N]; +int Sum = 0; namespace ns { -struct st { - int x; +struct St { + int X; }; } void sameNames() { - int num = 0; - for (int i = 0; i < N; ++i) { - printf("Fibonacci number is %d\n", nums[i]); - sum += nums[i] + 2 + num; - (void)nums[i]; + int Num = 0; + for (int I = 0; I < N; ++I) { + printf("Fibonacci number is %d\n", Nums[I]); + Sum += Nums[I] + 2 + Num; + (void)Nums[I]; + } + // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & Elem : Nums) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); + // CHECK-FIXES-NEXT: Sum += Elem + 2 + Num; + // CHECK-FIXES-NEXT: (void)Elem; + + int Elem = 0; + for (int I = 0; I < N; ++I) { + printf("Fibonacci number is %d\n", Nums[I]); + Sum += Nums[I] + 2 + Num + Elem; + (void)Nums[I]; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & nums_i : nums) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", nums_i); - // CHECK-FIXES-NEXT: sum += nums_i + 2 + num; - // CHECK-FIXES-NOT: (void) num; + // CHECK-FIXES: for (auto & NumsI : Nums) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", NumsI); + // CHECK-FIXES-NEXT: Sum += NumsI + 2 + Num + Elem; + // CHECK-FIXES-NEXT: (void)NumsI; } void macroConflict() { S MAXs; - for (S::iterator it = MAXs.begin(), e = MAXs.end(); it != e; ++it) { - printf("s has value %d\n", (*it).x); + for (S::iterator It = MAXs.begin(), E = MAXs.end(); It != E; ++It) { + printf("s has value %d\n", (*It).X); printf("Max of 3 and 5: %d\n", MAX(3, 5)); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & MAXs_it : MAXs) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (MAXs_it).x); + // CHECK-FIXES: for (auto & Elem : MAXs) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); // CHECK-FIXES-NEXT: printf("Max of 3 and 5: %d\n", MAX(3, 5)); - for (S::const_iterator it = MAXs.begin(), e = MAXs.end(); it != e; ++it) { - printf("s has value %d\n", (*it).x); + for (S::const_iterator It = MAXs.begin(), E = MAXs.end(); It != E; ++It) { + printf("s has value %d\n", (*It).X); printf("Max of 3 and 5: %d\n", MAX(3, 5)); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & MAXs_it : MAXs) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (MAXs_it).x); + // CHECK-FIXES: for (const auto & Elem : MAXs) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); // CHECK-FIXES-NEXT: printf("Max of 3 and 5: %d\n", MAX(3, 5)); T DEFs; - for (T::iterator it = DEFs.begin(), e = DEFs.end(); it != e; ++it) { - if (*it == DEF) { - printf("I found %d\n", *it); + for (T::iterator It = DEFs.begin(), E = DEFs.end(); It != E; ++It) { + if (*It == DEF) { + printf("I found %d\n", *It); } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & DEFs_it : DEFs) - // CHECK-FIXES-NEXT: if (DEFs_it == DEF) - // CHECK-FIXES-NEXT: printf("I found %d\n", DEFs_it); + // CHECK-FIXES: for (auto & Elem : DEFs) + // CHECK-FIXES-NEXT: if (Elem == DEF) + // CHECK-FIXES-NEXT: printf("I found %d\n", Elem); } void keywordConflict() { T ints; - for (T::iterator it = ints.begin(), e = ints.end(); it != e; ++it) { - *it = 5; + for (T::iterator It = ints.begin(), E = ints.end(); It != E; ++It) { + *It = 5; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & ints_it : ints) - // CHECK-FIXES-NEXT: ints_it = 5; + // CHECK-FIXES: for (auto & Elem : ints) + // CHECK-FIXES-NEXT: Elem = 5; U __FUNCTION__s; - for (U::iterator it = __FUNCTION__s.begin(), e = __FUNCTION__s.end(); - it != e; ++it) { - int __FUNCTION__s_it = (*it).x + 2; + for (U::iterator It = __FUNCTION__s.begin(), E = __FUNCTION__s.end(); + It != E; ++It) { + int __FUNCTION__s_It = (*It).X + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & __FUNCTION__s_elem : __FUNCTION__s) - // CHECK-FIXES-NEXT: int __FUNCTION__s_it = (__FUNCTION__s_elem).x + 2; + // CHECK-FIXES: for (auto & Elem : __FUNCTION__s) + // CHECK-FIXES-NEXT: int __FUNCTION__s_It = (Elem).X + 2; } void typeConflict() { T Vals; // Using the name "Val", although it is the name of an existing struct, is // safe in this loop since it will only exist within this scope. - for (T::iterator it = Vals.begin(), e = Vals.end(); it != e; ++it) - (void) *it; + for (T::iterator It = Vals.begin(), E = Vals.end(); It != E; ++It) + (void) *It; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead // CHECK-FIXES: for (auto & Val : Vals) // We cannot use the name "Val" in this loop since there is a reference to // it in the body of the loop. - for (T::iterator it = Vals.begin(), e = Vals.end(); it != e; ++it) { - *it = sizeof(Val); + for (T::iterator It = Vals.begin(), E = Vals.end(); It != E; ++It) { + *It = sizeof(Val); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & Vals_it : Vals) - // CHECK-FIXES-NEXT: Vals_it = sizeof(Val); + // CHECK-FIXES: for (auto & Elem : Vals) + // CHECK-FIXES-NEXT: Elem = sizeof(Val); typedef struct Val TD; U TDs; // Naming the variable "TD" within this loop is safe because the typedef // was never used within the loop. - for (U::iterator it = TDs.begin(), e = TDs.end(); it != e; ++it) - (void) *it; + for (U::iterator It = TDs.begin(), E = TDs.end(); It != E; ++It) + (void) *It; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead // CHECK-FIXES: for (auto & TD : TDs) // "TD" cannot be used in this loop since the typedef is being used. - for (U::iterator it = TDs.begin(), e = TDs.end(); it != e; ++it) { + for (U::iterator It = TDs.begin(), E = TDs.end(); It != E; ++It) { TD V; - V.x = 5; - (void) *it; + V.X = 5; + (void) *It; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & TDs_it : TDs) + // CHECK-FIXES: for (auto & Elem : TDs) // CHECK-FIXES-NEXT: TD V; - // CHECK-FIXES-NEXT: V.x = 5; + // CHECK-FIXES-NEXT: V.X = 5; - using ns::st; - T sts; - for (T::iterator it = sts.begin(), e = sts.end(); it != e; ++it) { - *it = sizeof(st); + using ns::St; + T Sts; + for (T::iterator It = Sts.begin(), E = Sts.end(); It != E; ++It) { + *It = sizeof(St); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & sts_it : sts) - // CHECK-FIXES-NEXT: sts_it = sizeof(st); + // CHECK-FIXES: for (auto & Elem : Sts) + // CHECK-FIXES-NEXT: Elem = sizeof(St); } } // namespace NamingConflict @@ -384,7 +396,7 @@ // so fails to transform these cases which it should. void f() { MyArray Arr; - for (unsigned i = 0, e = Arr.size(); i < e; ++i) { + for (unsigned I = 0, E = Arr.size(); I < E; ++I) { } MyContainer C; @@ -396,57 +408,57 @@ namespace Nesting { -void g(S::iterator it); -void const_g(S::const_iterator it); +void g(S::iterator It); +void const_g(S::const_iterator It); class Foo { public: - void g(S::iterator it); - void const_g(S::const_iterator it); + void g(S::iterator It); + void const_g(S::const_iterator It); }; void f() { const int N = 10; const int M = 15; Val Arr[N]; - for (int i = 0; i < N; ++i) { - for (int j = 0; j < N; ++j) { - int k = Arr[i].x + Arr[j].x; + for (int I = 0; I < N; ++I) { + for (int J = 0; J < N; ++J) { + int K = Arr[I].X + Arr[J].X; // The repeat is there to allow FileCheck to make sure the two variable // names aren't the same. - int l = Arr[i].x + Arr[j].x; + int L = Arr[I].X + Arr[J].X; } } // CHECK-MESSAGES: :[[@LINE-8]]:3: warning: use range-based for loop instead // CHECK-MESSAGES: :[[@LINE-8]]:5: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: for (auto & Arr_j : Arr) - // CHECK-FIXES-NEXT: int k = elem.x + Arr_j.x; - // CHECK-FIXES-NOT: int l = elem.x + elem.x; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: for (auto & ArrJ : Arr) + // CHECK-FIXES-NEXT: int K = Elem.X + ArrJ.X; + // CHECK-FIXES-NOT: int L = Elem.X + Elem.X; // The inner loop is also convertible, but doesn't need to be converted // immediately. FIXME: update this test when that changes. Val Nest[N][M]; - for (int i = 0; i < N; ++i) { - for (int j = 0; j < M; ++j) { - printf("Got item %d", Nest[i][j].x); + for (int I = 0; I < N; ++I) { + for (int J = 0; J < M; ++J) { + printf("Got item %d", Nest[I][J].X); } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Nest) - // CHECK-FIXES-NEXT: for (int j = 0; j < M; ++j) - // CHECK-FIXES-NEXT: printf("Got item %d", elem[j].x); + // CHECK-FIXES: for (auto & Elem : Nest) + // CHECK-FIXES-NEXT: for (int J = 0; J < M; ++J) + // CHECK-FIXES-NEXT: printf("Got item %d", Elem[J].X); // Note that the order of M and N are switched for this test. - for (int j = 0; j < M; ++j) { - for (int i = 0; i < N; ++i) { - printf("Got item %d", Nest[i][j].x); + for (int J = 0; J < M; ++J) { + for (int I = 0; I < N; ++I) { + printf("Got item %d", Nest[I][J].X); } } // CHECK-MESSAGES: :[[@LINE-4]]:5: warning: use range-based for loop instead - // CHECK-FIXES-NOT: for (auto & {{[a-zA-Z_]+}} : Nest[i]) - // CHECK-FIXES: for (int j = 0; j < M; ++j) - // CHECK-FIXES-NEXT: for (auto & elem : Nest) - // CHECK-FIXES-NEXT: printf("Got item %d", elem[j].x); + // CHECK-FIXES-NOT: for (auto & {{[a-zA-Z_]+}} : Nest[I]) + // CHECK-FIXES: for (int J = 0; J < M; ++J) + // CHECK-FIXES-NEXT: for (auto & Elem : Nest) + // CHECK-FIXES-NEXT: printf("Got item %d", Elem[J].X); // The inner loop is also convertible. Nested NestT; @@ -456,8 +468,8 @@ } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : NestT) - // CHECK-FIXES-NEXT: for (T::iterator TI = (elem).begin(), TE = (elem).end(); TI != TE; ++TI) + // CHECK-FIXES: for (auto & Elem : NestT) + // CHECK-FIXES-NEXT: for (T::iterator TI = (Elem).begin(), TE = (Elem).end(); TI != TE; ++TI) // CHECK-FIXES-NEXT: printf("%d", *TI); // The inner loop is also convertible. @@ -468,50 +480,50 @@ } } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : NestS) - // CHECK-FIXES-NEXT: for (S::const_iterator SI = (elem).begin(), SE = (elem).end(); SI != SE; ++SI) + // CHECK-FIXES: for (const auto & Elem : NestS) + // CHECK-FIXES-NEXT: for (S::const_iterator SI = (Elem).begin(), SE = (Elem).end(); SI != SE; ++SI) // CHECK-FIXES-NEXT: printf("%d", *SI); for (Nested::const_iterator I = NestS.begin(), E = NestS.end(); I != E; ++I) { - const S &s = *I; - for (S::const_iterator SI = s.begin(), SE = s.end(); SI != SE; ++SI) { + const S &Ss = *I; + for (S::const_iterator SI = Ss.begin(), SE = Ss.end(); SI != SE; ++SI) { printf("%d", *SI); const_g(SI); } } // CHECK-MESSAGES: :[[@LINE-7]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & s : NestS) + // CHECK-FIXES: for (const auto & Ss : NestS) for (Nested::iterator I = NestS.begin(), E = NestS.end(); I != E; ++I) { - S &s = *I; - for (S::iterator SI = s.begin(), SE = s.end(); SI != SE; ++SI) { + S &Ss = *I; + for (S::iterator SI = Ss.begin(), SE = Ss.end(); SI != SE; ++SI) { printf("%d", *SI); g(SI); } } // CHECK-MESSAGES: :[[@LINE-7]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & s : NestS) + // CHECK-FIXES: for (auto & Ss : NestS) Foo foo; for (Nested::const_iterator I = NestS.begin(), E = NestS.end(); I != E; ++I) { - const S &s = *I; - for (S::const_iterator SI = s.begin(), SE = s.end(); SI != SE; ++SI) { + const S &Ss = *I; + for (S::const_iterator SI = Ss.begin(), SE = Ss.end(); SI != SE; ++SI) { printf("%d", *SI); foo.const_g(SI); } } // CHECK-MESSAGES: :[[@LINE-7]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & s : NestS) + // CHECK-FIXES: for (const auto & Ss : NestS) for (Nested::iterator I = NestS.begin(), E = NestS.end(); I != E; ++I) { - S &s = *I; - for (S::iterator SI = s.begin(), SE = s.end(); SI != SE; ++SI) { + S &Ss = *I; + for (S::iterator SI = Ss.begin(), SE = Ss.end(); SI != SE; ++SI) { printf("%d", *SI); foo.g(SI); } } // CHECK-MESSAGES: :[[@LINE-7]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & s : NestS) + // CHECK-FIXES: for (auto & Ss : NestS) } @@ -520,162 +532,162 @@ namespace SingleIterator { void complexContainer() { - X exes[5]; - int index = 0; + X Exes[5]; + int Index = 0; - for (S::iterator i = exes[index].getS().begin(), e = exes[index].getS().end(); i != e; ++i) { - MutableVal k = *i; - MutableVal j = *i; + for (S::iterator I = Exes[Index].getS().begin(), E = Exes[Index].getS().end(); I != E; ++I) { + MutableVal K = *I; + MutableVal J = *I; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : exes[index].getS()) - // CHECK-FIXES-NEXT: MutableVal k = elem; - // CHECK-FIXES-NEXT: MutableVal j = elem; + // CHECK-FIXES: for (auto & Elem : Exes[Index].getS()) + // CHECK-FIXES-NEXT: MutableVal K = Elem; + // CHECK-FIXES-NEXT: MutableVal J = Elem; } void f() { /// begin()/end() - based for loops here: - T t; - for (T::iterator it = t.begin(); it != t.end(); ++it) { - printf("I found %d\n", *it); + T Tt; + for (T::iterator It = Tt.begin(); It != Tt.end(); ++It) { + printf("I found %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : t) - // CHECK-FIXES-NEXT: printf("I found %d\n", elem); + // CHECK-FIXES: for (auto & Elem : Tt) + // CHECK-FIXES-NEXT: printf("I found %d\n", Elem); - T *pt; - for (T::iterator it = pt->begin(); it != pt->end(); ++it) { - printf("I found %d\n", *it); + T *Pt; + for (T::iterator It = Pt->begin(); It != Pt->end(); ++It) { + printf("I found %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : *pt) - // CHECK-FIXES-NEXT: printf("I found %d\n", elem); + // CHECK-FIXES: for (auto & Elem : *Pt) + // CHECK-FIXES-NEXT: printf("I found %d\n", Elem); - S s; - for (S::iterator it = s.begin(); it != s.end(); ++it) { - printf("s has value %d\n", (*it).x); + S Ss; + for (S::iterator It = Ss.begin(); It != Ss.end(); ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (elem).x); + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); - S *ps; - for (S::iterator it = ps->begin(); it != ps->end(); ++it) { - printf("s has value %d\n", (*it).x); + S *Ps; + for (S::iterator It = Ps->begin(); It != Ps->end(); ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & p : *ps) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (p).x); + // CHECK-FIXES: for (auto & P : *Ps) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (P).X); - for (S::iterator it = s.begin(); it != s.end(); ++it) { - printf("s has value %d\n", it->x); + for (S::iterator It = Ss.begin(); It != Ss.end(); ++It) { + printf("s has value %d\n", It->X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: printf("s has value %d\n", elem.x); + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: printf("s has value %d\n", Elem.X); - for (S::iterator it = s.begin(); it != s.end(); ++it) { - it->x = 3; + for (S::iterator It = Ss.begin(); It != Ss.end(); ++It) { + It->X = 3; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: elem.x = 3; + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: Elem.X = 3; - for (S::iterator it = s.begin(); it != s.end(); ++it) { - (*it).x = 3; + for (S::iterator It = Ss.begin(); It != Ss.end(); ++It) { + (*It).X = 3; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: (elem).x = 3; + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: (Elem).X = 3; - for (S::iterator it = s.begin(); it != s.end(); ++it) { - it->nonConstFun(4, 5); + for (S::iterator It = Ss.begin(); It != Ss.end(); ++It) { + It->nonConstFun(4, 5); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) - // CHECK-FIXES-NEXT: elem.nonConstFun(4, 5); + // CHECK-FIXES: for (auto & Elem : Ss) + // CHECK-FIXES-NEXT: Elem.nonConstFun(4, 5); - U u; - for (U::iterator it = u.begin(); it != u.end(); ++it) { - printf("s has value %d\n", it->x); + U Uu; + for (U::iterator It = Uu.begin(); It != Uu.end(); ++It) { + printf("s has value %d\n", It->X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : u) - // CHECK-FIXES-NEXT: printf("s has value %d\n", elem.x); + // CHECK-FIXES: for (auto & Elem : Uu) + // CHECK-FIXES-NEXT: printf("s has value %d\n", Elem.X); - for (U::iterator it = u.begin(); it != u.end(); ++it) { - printf("s has value %d\n", (*it).x); + for (U::iterator It = Uu.begin(); It != Uu.end(); ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : u) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (elem).x); + // CHECK-FIXES: for (auto & Elem : Uu) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); U::iterator A; - for (U::iterator i = u.begin(); i != u.end(); ++i) - int k = A->x + i->x; + for (U::iterator I = Uu.begin(); I != Uu.end(); ++I) + int K = A->X + I->X; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : u) - // CHECK-FIXES-NEXT: int k = A->x + elem.x; + // CHECK-FIXES: for (auto & Elem : Uu) + // CHECK-FIXES-NEXT: int K = A->X + Elem.X; - dependent v; - for (dependent::iterator it = v.begin(); - it != v.end(); ++it) { - printf("Fibonacci number is %d\n", *it); + dependent V; + for (dependent::iterator It = V.begin(); + It != V.end(); ++It) { + printf("Fibonacci number is %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : v) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); + // CHECK-FIXES: for (auto & Elem : V) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); - for (dependent::iterator it(v.begin()); - it != v.end(); ++it) { - printf("Fibonacci number is %d\n", *it); + for (dependent::iterator It(V.begin()); + It != V.end(); ++It) { + printf("Fibonacci number is %d\n", *It); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : v) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", elem); + // CHECK-FIXES: for (auto & Elem : V) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); doublyDependent intmap; - for (doublyDependent::iterator it = intmap.begin(); - it != intmap.end(); ++it) { - printf("intmap[%d] = %d", it->first, it->second); + for (doublyDependent::iterator It = intmap.begin(); + It != intmap.end(); ++It) { + printf("intmap[%d] = %d", It->first, It->second); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : intmap) - // CHECK-FIXES-NEXT: printf("intmap[%d] = %d", elem.first, elem.second); + // CHECK-FIXES: for (auto & Elem : intmap) + // CHECK-FIXES-NEXT: printf("intmap[%d] = %d", Elem.first, Elem.second); } void different_type() { // Tests to verify the proper use of auto where the init variable type and the // initializer type differ or are mostly the same except for const qualifiers. - // s.begin() returns a type 'iterator' which is just a non-const pointer and + // Ss.begin() returns a type 'iterator' which is just a non-const pointer and // differs from const_iterator only on the const qualification. - S s; - for (S::const_iterator it = s.begin(); it != s.end(); ++it) { - printf("s has value %d\n", (*it).x); + S Ss; + for (S::const_iterator It = Ss.begin(); It != Ss.end(); ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & elem : s) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (elem).x); + // CHECK-FIXES: for (const auto & Elem : Ss) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (Elem).X); - S *ps; - for (S::const_iterator it = ps->begin(); it != ps->end(); ++it) { - printf("s has value %d\n", (*it).x); + S *Ps; + for (S::const_iterator It = Ps->begin(); It != Ps->end(); ++It) { + printf("s has value %d\n", (*It).X); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (const auto & p : *ps) - // CHECK-FIXES-NEXT: printf("s has value %d\n", (p).x); + // CHECK-FIXES: for (const auto & P : *Ps) + // CHECK-FIXES-NEXT: printf("s has value %d\n", (P).X); - // v.begin() returns a user-defined type 'iterator' which, since it's + // V.begin() returns a user-defined type 'iterator' which, since it's // different from const_iterator, disqualifies these loops from // transformation. - dependent v; - for (dependent::const_iterator it = v.begin(); it != v.end(); ++it) { - printf("Fibonacci number is %d\n", *it); + dependent V; + for (dependent::const_iterator It = V.begin(); It != V.end(); ++It) { + printf("Fibonacci number is %d\n", *It); } - for (dependent::const_iterator it(v.begin()); it != v.end(); ++it) { - printf("Fibonacci number is %d\n", *it); + for (dependent::const_iterator It(V.begin()); It != V.end(); ++It) { + printf("Fibonacci number is %d\n", *It); } } @@ -688,29 +700,29 @@ #define THREE_PARAM(x, y, z) if (x == y) {z;} const int N = 10; -int arr[N]; +int Arr[N]; void messing_with_macros() { - for (int i = 0; i < N; ++i) { - printf("Value: %d\n", arr[i]); + for (int I = 0; I < N; ++I) { + printf("Value: %d\n", Arr[I]); } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: printf("Value: %d\n", elem); + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: printf("Value: %d\n", Elem); - for (int i = 0; i < N; ++i) { - printf("Value: %d\n", CONT arr[i]); + for (int I = 0; I < N; ++I) { + printf("Value: %d\n", CONT Arr[I]); } // Multiple macro arguments. - for (int i = 0; i < N; ++i) { - TWO_PARAM(arr[i], arr[i]); - THREE_PARAM(arr[i], arr[i], arr[i]); + for (int I = 0; I < N; ++I) { + TWO_PARAM(Arr[I], Arr[I]); + THREE_PARAM(Arr[I], Arr[I], Arr[I]); } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : arr) - // CHECK-FIXES-NEXT: TWO_PARAM(elem, elem); - // CHECK-FIXES-NEXT: THREE_PARAM(elem, elem, elem); + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: TWO_PARAM(Elem, Elem); + // CHECK-FIXES-NEXT: THREE_PARAM(Elem, Elem, Elem); } } // namespace Macros @@ -724,16 +736,16 @@ (void) *SI; } - S s; - for (S::iterator SI = s.begin(), SE = s.end(); SI != SE; ++SI) + S Ss; + for (S::iterator SI = Ss.begin(), SE = Ss.end(); SI != SE; ++SI) (void) *SI; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : s) + // CHECK-FIXES: for (auto & Elem : Ss) } void template_instantiation() { - S a; - set_union(a); + S Ss; + set_union(Ss); } } // namespace Templates @@ -749,43 +761,43 @@ for (int I = 0; I < N; ++I) auto F1 = [Arr, I]() { int R1 = Arr[I] + 1; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto F1 = [Arr, &elem]() { int R1 = elem + 1; }; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto F1 = [Arr, &Elem]() { int R1 = Elem + 1; }; for (int I = 0; I < N; ++I) auto F2 = [Arr, &I]() { int R2 = Arr[I] + 3; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto F2 = [Arr, &elem]() { int R2 = elem + 3; }; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto F2 = [Arr, &Elem]() { int R2 = Elem + 3; }; // FIXME: alias don't work if the index is captured. // Alias declared inside lambda (by value). for (int I = 0; I < N; ++I) auto F3 = [&Arr, I]() { int R3 = Arr[I]; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto F3 = [&Arr, &elem]() { int R3 = elem; }; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto F3 = [&Arr, &Elem]() { int R3 = Elem; }; for (int I = 0; I < N; ++I) auto F4 = [&Arr, &I]() { int R4 = Arr[I]; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto F4 = [&Arr, &elem]() { int R4 = elem; }; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto F4 = [&Arr, &Elem]() { int R4 = Elem; }; // Alias declared inside lambda (by reference). for (int I = 0; I < N; ++I) auto F5 = [&Arr, I]() { int &R5 = Arr[I]; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto F5 = [&Arr, &elem]() { int &R5 = elem; }; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto F5 = [&Arr, &Elem]() { int &R5 = Elem; }; for (int I = 0; I < N; ++I) auto F6 = [&Arr, &I]() { int &R6 = Arr[I]; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto F6 = [&Arr, &elem]() { int &R6 = elem; }; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto F6 = [&Arr, &Elem]() { int &R6 = Elem; }; for (int I = 0; I < N; ++I) { auto F = [Arr, I](int k) { @@ -794,10 +806,10 @@ F(Arr[I]); } // CHECK-MESSAGES: :[[@LINE-6]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto F = [Arr, &elem](int k) - // CHECK-FIXES-NEXT: printf("%d\n", elem + k); - // CHECK-FIXES: F(elem); + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto F = [Arr, &Elem](int k) + // CHECK-FIXES-NEXT: printf("%d\n", Elem + k); + // CHECK-FIXES: F(Elem); } void implicitCapture() { @@ -826,10 +838,10 @@ }; } // CHECK-MESSAGES: :[[@LINE-6]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) + // CHECK-FIXES: for (auto & Elem : Arr) // CHECK-FIXES-NEXT: auto G3 = [&]() - // CHECK-FIXES-NEXT: int R3 = elem; - // CHECK-FIXES-NEXT: int J3 = elem + R3; + // CHECK-FIXES-NEXT: int R3 = Elem; + // CHECK-FIXES-NEXT: int J3 = Elem + R3; for (int I = 0; I < N; ++I) { auto G4 = [=]() { @@ -837,9 +849,9 @@ }; } // CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) + // CHECK-FIXES: for (auto & Elem : Arr) // CHECK-FIXES-NEXT: auto G4 = [=]() - // CHECK-FIXES-NEXT: int R4 = elem + 5; + // CHECK-FIXES-NEXT: int R4 = Elem + 5; // Alias by value. for (int I = 0; I < N; ++I) { @@ -867,30 +879,30 @@ } void iterators() { - dependent dep; + dependent Dep; - for (dependent::iterator I = dep.begin(), E = dep.end(); I != E; ++I) + for (dependent::iterator I = Dep.begin(), E = Dep.end(); I != E; ++I) auto H1 = [&I]() { int R = *I; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : dep) - // CHECK-FIXES-NEXT: auto H1 = [&elem]() { int R = elem; }; + // CHECK-FIXES: for (auto & Elem : Dep) + // CHECK-FIXES-NEXT: auto H1 = [&Elem]() { int R = Elem; }; - for (dependent::iterator I = dep.begin(), E = dep.end(); I != E; ++I) + for (dependent::iterator I = Dep.begin(), E = Dep.end(); I != E; ++I) auto H2 = [&]() { int R = *I + 2; }; // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : dep) - // CHECK-FIXES-NEXT: auto H2 = [&]() { int R = elem + 2; }; + // CHECK-FIXES: for (auto & Elem : Dep) + // CHECK-FIXES-NEXT: auto H2 = [&]() { int R = Elem + 2; }; // FIXME: It doesn't work with const iterators. - for (dependent::const_iterator I = dep.begin(), E = dep.end(); + for (dependent::const_iterator I = Dep.begin(), E = Dep.end(); I != E; ++I) auto H3 = [I]() { int R = *I; }; - for (dependent::const_iterator I = dep.begin(), E = dep.end(); + for (dependent::const_iterator I = Dep.begin(), E = Dep.end(); I != E; ++I) auto H4 = [&]() { int R = *I + 1; }; - for (dependent::const_iterator I = dep.begin(), E = dep.end(); + for (dependent::const_iterator I = Dep.begin(), E = Dep.end(); I != E; ++I) auto H5 = [=]() { int R = *I; }; } @@ -908,24 +920,24 @@ auto C1 = [&Arr, I]() { if (Arr[I] == 1); }; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Arr) - // CHECK-FIXES-NEXT: auto C1 = [&Arr, &elem]() { if (elem == 1); }; + // CHECK-FIXES: for (auto & Elem : Arr) + // CHECK-FIXES-NEXT: auto C1 = [&Arr, &Elem]() { if (Elem == 1); }; for (unsigned I = 0; I < Dep.size(); ++I) { auto C2 = [&Dep, I]() { if (Dep[I] == 2); }; } // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto & elem : Dep) - // CHECK-FIXES-NEXT: auto C2 = [&Dep, &elem]() { if (elem == 2); }; + // CHECK-FIXES: for (auto & Elem : Dep) + // CHECK-FIXES-NEXT: auto C2 = [&Dep, &Elem]() { if (Elem == 2); }; } } // namespace Lambdas namespace InitLists { -struct D { int i; }; -struct E { D d; }; -int g(int b); +struct D { int Ii; }; +struct E { D Dd; }; +int g(int B); void f() { const unsigned N = 3; @@ -933,19 +945,19 @@ // Subtrees of InitListExpr are visited twice. Test that we do not do repeated // replacements. - for (unsigned i = 0; i < N; ++i) { - int a{ Array[i] }; - int b{ g(Array[i]) }; - int c{ g( { Array[i] } ) }; - D d{ { g( { Array[i] } ) } }; - E e{ { { g( { Array[i] } ) } } }; + for (unsigned I = 0; I < N; ++I) { + int A{ Array[I] }; + int B{ g(Array[I]) }; + int C{ g( { Array[I] } ) }; + D Dd{ { g( { Array[I] } ) } }; + E Ee{ { { g( { Array[I] } ) } } }; } // CHECK-MESSAGES: :[[@LINE-7]]:3: warning: use range-based for loop instead - // CHECK-FIXES: int a{ elem }; - // CHECK-FIXES-NEXT: int b{ g(elem) }; - // CHECK-FIXES-NEXT: int c{ g( { elem } ) }; - // CHECK-FIXES-NEXT: D d{ { g( { elem } ) } }; - // CHECK-FIXES-NEXT: E e{ { { g( { elem } ) } } }; + // CHECK-FIXES: int A{ Elem }; + // CHECK-FIXES-NEXT: int B{ g(Elem) }; + // CHECK-FIXES-NEXT: int C{ g( { Elem } ) }; + // CHECK-FIXES-NEXT: D Dd{ { g( { Elem } ) } }; + // CHECK-FIXES-NEXT: E Ee{ { { g( { Elem } ) } } }; } } // namespace InitLists Index: test/clang-tidy/modernize-loop-convert-lowercase.cpp =================================================================== --- /dev/null +++ test/clang-tidy/modernize-loop-convert-lowercase.cpp @@ -0,0 +1,49 @@ +// RUN: %python %S/check_clang_tidy.py %s modernize-loop-convert %t \ +// RUN: -config="{CheckOptions: [{key: modernize-loop-convert.NamingStyle, value: 'lower_case'}]}" \ +// RUN: -- -std=c++11 -I %S/Inputs/modernize-loop-convert + +#include "structures.h" + +const int n = 10; +int arr[n]; +int nums[n]; + +void naming() { + for (int i = 0; i < n; ++i) { + printf("%d\n", arr[i]); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead [modernize-loop-convert] + // CHECK-FIXES: for (auto & elem : arr) + // CHECK-FIXES-NEXT: printf("%d\n", elem); + + 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); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & elem : nums) + // CHECK-FIXES-NEXT: printf("%d\n", elem + num); + + int elem = 0; + for (int i = 0; i < n; ++i) { + printf("%d\n", nums[i] + num + elem); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & nums_i : nums) + // CHECK-FIXES-NEXT: printf("%d\n", nums_i + num + elem); + + int nums_i = 0; + for (int i = 0; i < n; ++i) { + printf("%d\n", nums[i] + num + elem + nums_i); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & nums_elem : nums) + // CHECK-FIXES-NEXT: printf("%d\n", nums_elem + num + elem + nums_i); +} Index: test/clang-tidy/modernize-loop-convert-negative.cpp =================================================================== --- test/clang-tidy/modernize-loop-convert-negative.cpp +++ test/clang-tidy/modernize-loop-convert-negative.cpp @@ -8,23 +8,23 @@ namespace Negative { const int N = 6; -int arr[N] = {1, 2, 3, 4, 5, 6}; -int (*pArr)[N] = &arr; +int Arr[N] = {1, 2, 3, 4, 5, 6}; +int (*pArr)[N] = &Arr; int Sum = 0; // Checks for the Index start and end: void IndexStartAndEnd() { for (int I = 0; I < N + 1; ++I) - Sum += arr[I]; + Sum += Arr[I]; for (int I = 0; I < N - 1; ++I) - Sum += arr[I]; + Sum += Arr[I]; for (int I = 1; I < N; ++I) - Sum += arr[I]; + Sum += Arr[I]; for (int I = 1; I < N; ++I) - Sum += arr[I]; + Sum += Arr[I]; for (int I = 0;; ++I) Sum += (*pArr)[I]; @@ -33,13 +33,13 @@ // Checks for invalid increment steps: void increment() { for (int I = 0; I < N; --I) - Sum += arr[I]; + Sum += Arr[I]; for (int I = 0; I < N; I) - Sum += arr[I]; + Sum += Arr[I]; for (int I = 0; I < N;) - Sum += arr[I]; + Sum += Arr[I]; for (int I = 0; I < N; I += 2) Sum++; @@ -48,7 +48,7 @@ // Checks to make sure that the Index isn't used outside of the array: void IndexUse() { for (int I = 0; I < N; ++I) - arr[I] += 1 + I; + Arr[I] += 1 + I; } // Check for loops that don't mention arrays @@ -65,30 +65,30 @@ // Checks for incorrect loop variables. void mixedVariables() { - int badIndex; - for (int I = 0; badIndex < N; ++I) - Sum += arr[I]; + int BadIndex; + for (int I = 0; BadIndex < N; ++I) + Sum += Arr[I]; - for (int I = 0; I < N; ++badIndex) - Sum += arr[I]; + for (int I = 0; I < N; ++BadIndex) + Sum += Arr[I]; - for (int I = 0; badIndex < N; ++badIndex) - Sum += arr[I]; + for (int I = 0; BadIndex < N; ++BadIndex) + Sum += Arr[I]; - for (int I = 0; badIndex < N; ++badIndex) - Sum += arr[badIndex]; + for (int I = 0; BadIndex < N; ++BadIndex) + Sum += Arr[BadIndex]; } // Checks for multiple arrays Indexed. void multipleArrays() { - int badArr[N]; + int BadArr[N]; for (int I = 0; I < N; ++I) - Sum += arr[I] + badArr[I]; + Sum += Arr[I] + BadArr[I]; for (int I = 0; I < N; ++I) { - int K = badArr[I]; - Sum += arr[I] + K; + int K = BadArr[I]; + Sum += Arr[I] + K; } } @@ -105,7 +105,7 @@ } for (int I = 0; I < N; ++I) { - printf("%d", HA.ValArr[I].x); + printf("%d", HA.ValArr[I].X); } } @@ -115,7 +115,7 @@ } for (int I = 0; I < N; ++I) { - printf("%d", this->HA.ValArr[I].x); + printf("%d", this->HA.ValArr[I].X); } } }; @@ -123,9 +123,9 @@ namespace NegativeIterator { -S s; -T t; -U u; +S Ss; +T Tt; +U Tu; struct BadBeginEnd : T { iterator notBegin(); @@ -142,29 +142,29 @@ } void badLoopShapes() { - for (T::iterator I = t.begin(), E = t.end(), F = E; I != E; ++I) + for (T::iterator I = Tt.begin(), E = Tt.end(), F = E; I != E; ++I) int K = *I; - for (T::iterator I = t.begin(), E = t.end(); I != E;) + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E;) int K = *I; - for (T::iterator I = t.begin(), E = t.end();; ++I) + for (T::iterator I = Tt.begin(), E = Tt.end();; ++I) int K = *I; - T::iterator outsideI; - T::iterator outsideE; + T::iterator OutsideI; + T::iterator OutsideE; - for (; outsideI != outsideE; ++outsideI) - int K = *outsideI; + for (; OutsideI != OutsideE; ++OutsideI) + int K = *OutsideI; } void iteratorArrayMix() { - int lower; + int Lower; const int N = 6; - for (T::iterator I = t.begin(), E = t.end(); lower < N; ++I) + for (T::iterator I = Tt.begin(), E = Tt.end(); Lower < N; ++I) int K = *I; - for (T::iterator I = t.begin(), E = t.end(); lower < N; ++lower) + for (T::iterator I = Tt.begin(), E = Tt.end(); Lower < N; ++Lower) int K = *I; } @@ -174,76 +174,76 @@ }; void badConstructor() { - for (T::iterator I = ExtraConstructor(t.begin(), 0), E = t.end(); + for (T::iterator I = ExtraConstructor(Tt.begin(), 0), E = Tt.end(); I != E; ++I) int K = *I; - for (T::iterator I = ExtraConstructor(t.begin()), E = t.end(); I != E; ++I) + for (T::iterator I = ExtraConstructor(Tt.begin()), E = Tt.end(); I != E; ++I) int K = *I; } void foo(S::iterator It) {} class Foo {public: void bar(S::iterator It); }; -Foo fo; +Foo Fo; void iteratorUsed() { - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) foo(I); - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) - fo.bar(I); + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) + Fo.bar(I); S::iterator Ret; - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) Ret = I; } void iteratorMemberUsed() { - for (T::iterator I = t.begin(), E = t.end(); I != E; ++I) - I.x = *I; + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E; ++I) + I.X = *I; - for (T::iterator I = t.begin(), E = t.end(); I != E; ++I) - int K = I.x + *I; + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E; ++I) + int K = I.X + *I; - for (T::iterator I = t.begin(), E = t.end(); I != E; ++I) - int K = E.x + *I; + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E; ++I) + int K = E.X + *I; } void iteratorMethodCalled() { - for (T::iterator I = t.begin(), E = t.end(); I != E; ++I) + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E; ++I) I.insert(3); - for (T::iterator I = t.begin(), E = t.end(); I != E; ++I) + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E; ++I) if (I != I) int K = 3; } void iteratorOperatorCalled() { - for (T::iterator I = t.begin(), E = t.end(); I != E; ++I) + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E; ++I) int K = *(++I); - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) MutableVal K = *(++I); } void differentContainers() { - T other; - for (T::iterator I = t.begin(), E = other.end(); I != E; ++I) + T Other; + for (T::iterator I = Tt.begin(), E = Other.end(); I != E; ++I) int K = *I; - for (T::iterator I = other.begin(), E = t.end(); I != E; ++I) + for (T::iterator I = Other.begin(), E = Tt.end(); I != E; ++I) int K = *I; - S otherS; - for (S::iterator I = s.begin(), E = otherS.end(); I != E; ++I) + S OtherS; + for (S::iterator I = Ss.begin(), E = OtherS.end(); I != E; ++I) MutableVal K = *I; - for (S::iterator I = otherS.begin(), E = s.end(); I != E; ++I) + for (S::iterator I = OtherS.begin(), E = Ss.end(); I != E; ++I) MutableVal K = *I; } void wrongIterators() { - T::iterator other; - for (T::iterator I = t.begin(), E = t.end(); I != other; ++I) + T::iterator Other; + for (T::iterator I = Tt.begin(), E = Tt.end(); I != Other; ++I) int K = *I; } @@ -260,12 +260,12 @@ Val K = *I; } -void f(const T::iterator &it, int); -void f(const T &it, int); -void g(T &it, int); +void f(const T::iterator &It, int); +void f(const T &It, int); +void g(T &It, int); void iteratorPassedToFunction() { - for (T::iterator I = t.begin(), E = t.end(); I != E; ++I) + for (T::iterator I = Tt.begin(), E = Tt.end(); I != E; ++I) f(I, *I); } @@ -273,12 +273,12 @@ // decide that this is a safe transformation. Until then, we don't want it // applied. void iteratorDefinedOutside() { - T::iterator TheEnd = t.end(); - for (T::iterator I = t.begin(); I != TheEnd; ++I) + T::iterator TheEnd = Tt.end(); + for (T::iterator I = Tt.begin(); I != TheEnd; ++I) int K = *I; - T::iterator TheBegin = t.begin(); - for (T::iterator E = t.end(); TheBegin != E; ++TheBegin) + T::iterator TheBegin = Tt.begin(); + for (T::iterator E = Tt.end(); TheBegin != E; ++TheBegin) int K = *TheBegin; } @@ -287,105 +287,105 @@ namespace NegativePseudoArray { const int N = 6; -dependent v; -dependent *pv; +dependent V; +dependent *Pv; int Sum = 0; // Checks for the Index start and end: void IndexStartAndEnd() { - for (int I = 0; I < v.size() + 1; ++I) - Sum += v[I]; + for (int I = 0; I < V.size() + 1; ++I) + Sum += V[I]; - for (int I = 0; I < v.size() - 1; ++I) - Sum += v[I]; + for (int I = 0; I < V.size() - 1; ++I) + Sum += V[I]; - for (int I = 1; I < v.size(); ++I) - Sum += v[I]; + for (int I = 1; I < V.size(); ++I) + Sum += V[I]; - for (int I = 1; I < v.size(); ++I) - Sum += v[I]; + for (int I = 1; I < V.size(); ++I) + Sum += V[I]; for (int I = 0;; ++I) - Sum += (*pv)[I]; + Sum += (*Pv)[I]; } // Checks for invalid increment steps: void increment() { - for (int I = 0; I < v.size(); --I) - Sum += v[I]; + for (int I = 0; I < V.size(); --I) + Sum += V[I]; - for (int I = 0; I < v.size(); I) - Sum += v[I]; + for (int I = 0; I < V.size(); I) + Sum += V[I]; - for (int I = 0; I < v.size();) - Sum += v[I]; + for (int I = 0; I < V.size();) + Sum += V[I]; - for (int I = 0; I < v.size(); I += 2) + for (int I = 0; I < V.size(); I += 2) Sum++; } // Checks to make sure that the Index isn't used outside of the container: void IndexUse() { - for (int I = 0; I < v.size(); ++I) - v[I] += 1 + I; + for (int I = 0; I < V.size(); ++I) + V[I] += 1 + I; } // Checks for incorrect loop variables. void mixedVariables() { - int badIndex; - for (int I = 0; badIndex < v.size(); ++I) - Sum += v[I]; + int BadIndex; + for (int I = 0; BadIndex < V.size(); ++I) + Sum += V[I]; - for (int I = 0; I < v.size(); ++badIndex) - Sum += v[I]; + for (int I = 0; I < V.size(); ++BadIndex) + Sum += V[I]; - for (int I = 0; badIndex < v.size(); ++badIndex) - Sum += v[I]; + for (int I = 0; BadIndex < V.size(); ++BadIndex) + Sum += V[I]; - for (int I = 0; badIndex < v.size(); ++badIndex) - Sum += v[badIndex]; + for (int I = 0; BadIndex < V.size(); ++BadIndex) + Sum += V[BadIndex]; } // Checks for an array Indexed in addition to the container. void multipleArrays() { - int badArr[N]; + int BadArr[N]; - for (int I = 0; I < v.size(); ++I) - Sum += v[I] + badArr[I]; + for (int I = 0; I < V.size(); ++I) + Sum += V[I] + BadArr[I]; - for (int I = 0; I < v.size(); ++I) - Sum += badArr[I]; + for (int I = 0; I < V.size(); ++I) + Sum += BadArr[I]; - for (int I = 0; I < v.size(); ++I) { - int K = badArr[I]; + for (int I = 0; I < V.size(); ++I) { + int K = BadArr[I]; Sum += K + 2; } - for (int I = 0; I < v.size(); ++I) { - int K = badArr[I]; - Sum += v[I] + K; + for (int I = 0; I < V.size(); ++I) { + int K = BadArr[I]; + Sum += V[I] + K; } } // Checks for multiple containers being Indexed container. void multipleContainers() { - dependent badArr; + dependent BadArr; - for (int I = 0; I < v.size(); ++I) - Sum += v[I] + badArr[I]; + for (int I = 0; I < V.size(); ++I) + Sum += V[I] + BadArr[I]; - for (int I = 0; I < v.size(); ++I) - Sum += badArr[I]; + for (int I = 0; I < V.size(); ++I) + Sum += BadArr[I]; - for (int I = 0; I < v.size(); ++I) { - int K = badArr[I]; + for (int I = 0; I < V.size(); ++I) { + int K = BadArr[I]; Sum += K + 2; } - for (int I = 0; I < v.size(); ++I) { - int K = badArr[I]; - Sum += v[I] + K; + for (int I = 0; I < V.size(); ++I) { + int K = BadArr[I]; + Sum += V[I] + K; } } @@ -393,31 +393,31 @@ void derefContainer() { // Note the dependent::operator*() returns another dependent. // This test makes sure that we don't allow an arbitrary number of *'s. - for (int I = 0; I < pv->size(); ++I) - Sum += (**pv).at(I); + for (int I = 0; I < Pv->size(); ++I) + Sum += (**Pv).at(I); - for (int I = 0; I < pv->size(); ++I) - Sum += (**pv)[I]; + for (int I = 0; I < Pv->size(); ++I) + Sum += (**Pv)[I]; } void wrongEnd() { int Bad; - for (int I = 0, E = v.size(); I < Bad; ++I) - Sum += v[I]; + for (int I = 0, E = V.size(); I < Bad; ++I) + Sum += V[I]; } // Checks to see that non-const member functions are not called on the container // object. // These could be conceivably allowed with a lower required confidence level. void memberFunctionCalled() { - for (int I = 0; I < v.size(); ++I) { - Sum += v[I]; - v.foo(); + for (int I = 0; I < V.size(); ++I) { + Sum += V[I]; + V.foo(); } - for (int I = 0; I < v.size(); ++I) { - Sum += v[I]; - dependent::iterator It = v.begin(); + for (int I = 0; I < V.size(); ++I) { + Sum += V[I]; + dependent::iterator It = V.begin(); } } @@ -425,38 +425,38 @@ namespace NegativeMultiEndCall { -S s; -T t; -U u; +S Ss; +T Tt; +U Uu; void f(X); void f(S); void f(T); void complexContainer() { - X x; - for (S::iterator I = x.s.begin(), E = x.s.end(); I != E; ++I) { - f(x); + X Xx; + for (S::iterator I = Xx.Ss.begin(), E = Xx.Ss.end(); I != E; ++I) { + f(Xx); MutableVal K = *I; } - for (T::iterator I = x.t.begin(), E = x.t.end(); I != E; ++I) { - f(x); + for (T::iterator I = Xx.Tt.begin(), E = Xx.Tt.end(); I != E; ++I) { + f(Xx); int K = *I; } - for (S::iterator I = x.s.begin(), E = x.s.end(); I != E; ++I) { - f(x.s); + for (S::iterator I = Xx.Ss.begin(), E = Xx.Ss.end(); I != E; ++I) { + f(Xx.Ss); MutableVal K = *I; } - for (T::iterator I = x.t.begin(), E = x.t.end(); I != E; ++I) { - f(x.t); + for (T::iterator I = Xx.Tt.begin(), E = Xx.Tt.end(); I != E; ++I) { + f(Xx.Tt); int K = *I; } - for (S::iterator I = x.getS().begin(), E = x.getS().end(); I != E; ++I) { - f(x.getS()); + for (S::iterator I = Xx.getS().begin(), E = Xx.getS().end(); I != E; ++I) { + f(Xx.getS()); MutableVal K = *I; } @@ -476,9 +476,9 @@ namespace NoUsages { const int N = 6; -int arr[N] = {1, 2, 3, 4, 5, 6}; -S s; -dependent v; +int Arr[N] = {1, 2, 3, 4, 5, 6}; +S Ss; +dependent V; int Count = 0; void foo(); @@ -492,20 +492,20 @@ for (int I = 0; I < N; ++I) foo(); - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) {} - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) {} + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) printf("Hello world\n"); - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) ++Count; - for (S::iterator I = s.begin(), E = s.end(); I != E; ++I) + for (S::iterator I = Ss.begin(), E = Ss.end(); I != E; ++I) foo(); - for (int I = 0; I < v.size(); ++I) {} - for (int I = 0; I < v.size(); ++I) + for (int I = 0; I < V.size(); ++I) {} + for (int I = 0; I < V.size(); ++I) printf("Hello world\n"); - for (int I = 0; I < v.size(); ++I) + for (int I = 0; I < V.size(); ++I) ++Count; - for (int I = 0; I < v.size(); ++I) + for (int I = 0; I < V.size(); ++I) foo(); } Index: test/clang-tidy/modernize-loop-convert-uppercase.cpp =================================================================== --- /dev/null +++ test/clang-tidy/modernize-loop-convert-uppercase.cpp @@ -0,0 +1,49 @@ +// RUN: %python %S/check_clang_tidy.py %s modernize-loop-convert %t \ +// RUN: -config="{CheckOptions: [{key: modernize-loop-convert.NamingStyle, value: 'UPPER_CASE'}]}" \ +// RUN: -- -std=c++11 -I %S/Inputs/modernize-loop-convert + +#include "structures.h" + +const int N = 10; +int ARR[N]; +int NUMS[N]; + +void naming() { + for (int I = 0; I < N; ++I) { + printf("%d\n", ARR[I]); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead [modernize-loop-convert] + // CHECK-FIXES: for (auto & ELEM : ARR) + // CHECK-FIXES-NEXT: printf("%d\n", ELEM); + + 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); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & ELEM : NUMS) + // CHECK-FIXES-NEXT: printf("%d\n", ELEM + NUM); + + int ELEM = 0; + for (int I = 0; I < N; ++I) { + printf("%d\n", NUMS[I] + NUM + ELEM); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & NUMS_I : NUMS) + // CHECK-FIXES-NEXT: printf("%d\n", NUMS_I + NUM + ELEM); + + int NUMS_I = 0; + for (int I = 0; I < N; ++I) { + printf("%d\n", NUMS[I] + NUM + ELEM + NUMS_I); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & NUMS_ELEM : NUMS) + // CHECK-FIXES-NEXT: printf("%d\n", NUMS_ELEM + NUM + ELEM + NUMS_I); +}