diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp @@ -252,17 +252,18 @@ // functions called begin() and end() taking the container as an argument // are also allowed. TypeMatcher RecordWithBeginEnd = qualType(anyOf( - qualType( - isConstQualified(), - hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl( - hasMethod(cxxMethodDecl(hasName("begin"), isConst())), - hasMethod(cxxMethodDecl(hasName("end"), - isConst())))) // hasDeclaration - ))), // qualType + qualType(isConstQualified(), + hasUnqualifiedDesugaredType(recordType(hasDeclaration( + cxxRecordDecl(isSameOrDerivedFrom(cxxRecordDecl( + hasMethod(cxxMethodDecl(hasName("begin"), isConst())), + hasMethod(cxxMethodDecl(hasName("end"), + isConst())))))) // hasDeclaration + ))), // qualType qualType(unless(isConstQualified()), hasUnqualifiedDesugaredType(recordType(hasDeclaration( - cxxRecordDecl(hasMethod(hasName("begin")), - hasMethod(hasName("end"))))))) // qualType + cxxRecordDecl(isSameOrDerivedFrom(cxxRecordDecl( + hasMethod(hasName("begin")), + hasMethod(hasName("end"))))))))) // qualType )); StatementMatcher SizeCallMatcher = cxxMemberCallExpr( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -216,6 +216,10 @@ :doc: `readability-redundant-string-cstr ` check. +- Improved :doc:`modernize-loop-convert ` + to check for container functions ``begin``/``end`` etc on base classes of the container + type, instead of only as direct members of the container type itself. + Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/loop-convert/structures.h b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/loop-convert/structures.h --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/loop-convert/structures.h +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/loop-convert/structures.h @@ -126,6 +126,10 @@ void constFoo() const; }; +template +class dependent_derived : public dependent { +}; + template class doublyDependent{ public: diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp @@ -575,6 +575,7 @@ const dependent *Pconstv; transparent> Cv; +dependent_derived VD; void f() { int Sum = 0; @@ -653,6 +654,15 @@ // CHECK-FIXES: for (int I : V) // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", I); // CHECK-FIXES-NEXT: Sum += I + 2; + + for (int I = 0, E = VD.size(); E != I; ++I) { + printf("Fibonacci number is %d\n", VD[I]); + Sum += VD[I] + 2; + } + // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (int I : VD) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", I); + // CHECK-FIXES-NEXT: Sum += I + 2; } // Ensure that 'const auto &' is used with containers of non-trivial types.