[clang-tools-extra] a189b3b - [clang-tidy] performance-for-range-copy: Don't trigger on implicit type conversions.

Felix Berger via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 2 17:03:55 PST 2021


Author: Felix Berger
Date: 2021-03-02T20:02:48-05:00
New Revision: a189b3b9e8bb398d9fe8770956f8ad1d58c2a214

URL: https://github.com/llvm/llvm-project/commit/a189b3b9e8bb398d9fe8770956f8ad1d58c2a214
DIFF: https://github.com/llvm/llvm-project/commit/a189b3b9e8bb398d9fe8770956f8ad1d58c2a214.diff

LOG: [clang-tidy] performance-for-range-copy: Don't trigger on implicit type conversions.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
index 4b7a34f6c40f..8046301e3ce7 100644
--- a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
@@ -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")))

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/performance-for-range-copy.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance-for-range-copy.cpp
index e22650e10198..07b5116dfb14 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance-for-range-copy.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance-for-range-copy.cpp
@@ -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) {
   }
 }
 


        


More information about the cfe-commits mailing list