[clang-tidy] performance-for-range-copy: Don't trigger on implicit type conversions.
authorFelix Berger <flx@google.com>
Fri, 26 Feb 2021 21:27:06 +0000 (16:27 -0500)
committerFelix Berger <flx@google.com>
Wed, 3 Mar 2021 01:02:48 +0000 (20:02 -0500)
This disables the check for false positive cases where implicit type conversion
through either an implicit single argument constructor or a member conversion
operator is triggered when constructing the loop variable.

Fix the test cases that meant to cover these cases.

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

Reviewed-by: hokein
clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/performance-for-range-copy.cpp

index 4b7a34f..8046301 100644 (file)
@@ -45,10 +45,14 @@ void ForRangeCopyCheck::registerMatchers(MatchFinder *Finder) {
       hasOverloadedOperatorName("*"),
       callee(
           cxxMethodDecl(returns(unless(hasCanonicalType(referenceType()))))));
+  auto NotConstructedByCopy = cxxConstructExpr(
+      hasDeclaration(cxxConstructorDecl(unless(isCopyConstructor()))));
+  auto ConstructedByConversion = cxxMemberCallExpr(callee(cxxConversionDecl()));
   auto LoopVar =
       varDecl(HasReferenceOrPointerTypeOrIsAllowed,
-              unless(hasInitializer(expr(hasDescendant(expr(anyOf(
-                  materializeTemporaryExpr(), IteratorReturnsValueType)))))));
+              unless(hasInitializer(expr(hasDescendant(expr(
+                  anyOf(materializeTemporaryExpr(), IteratorReturnsValueType,
+                        NotConstructedByCopy, ConstructedByConversion)))))));
   Finder->addMatcher(
       traverse(TK_AsIs,
                cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar")))
index e22650e..07b5116 100644 (file)
@@ -60,13 +60,13 @@ void negativeConstReference() {
 
 void negativeUserDefinedConversion() {
   Convertible C[0];
-  for (const S &S1 : C) {
+  for (const S S1 : C) {
   }
 }
 
 void negativeImplicitConstructorConversion() {
   ConstructorConvertible C[0];
-  for (const S &S1 : C) {
+  for (const S S1 : C) {
   }
 }