[clang-tidy] Match derived types in in modernize-loop-convert
authorChris Cotter <ccotter14@bloomberg.net>
Tue, 10 Jan 2023 15:23:32 +0000 (15:23 +0000)
committerCarlos Galvez <carlosgalvezp@gmail.com>
Tue, 10 Jan 2023 16:08:25 +0000 (16:08 +0000)
This patch allows clang-tidy to replace traditional for-loops where the
container type inherits its `begin`/`end` methods from a base class.

Test plan: Added unit test cases that confirm the tool replaces the new
pattern.

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D140307

clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/loop-convert/structures.h
clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp

index 84df644..0056acb 100644 (file)
@@ -252,17 +252,18 @@ StatementMatcher makePseudoArrayLoopMatcher() {
   // 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(
index 0f060f7..70d26db 100644 (file)
@@ -216,6 +216,10 @@ Changes in existing checks
   :doc: `readability-redundant-string-cstr <clang-tidy/checks/readability/redundant-string-cstr>`
   check.
 
+- Improved :doc:`modernize-loop-convert <clang-tidy/checks/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
 ^^^^^^^^^^^^^^
 
index abd6490..bc025ce 100644 (file)
@@ -126,6 +126,10 @@ class dependent {
   void constFoo() const;
 };
 
+template<typename ElemType>
+class dependent_derived : public dependent<ElemType> {
+};
+
 template<typename First, typename Second>
 class doublyDependent{
  public:
index b42ce4a..96b3956 100644 (file)
@@ -575,6 +575,7 @@ const dependent<NonTriviallyCopyable> Constv;
 const dependent<NonTriviallyCopyable> *Pconstv;
 
 transparent<dependent<int>> Cv;
+dependent_derived<int> VD;
 
 void f() {
   int Sum = 0;
@@ -653,6 +654,15 @@ void f() {
   // 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.