diff --git a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp index 4b7a34f6c40f52..8046301e3ce757 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 e22650e10198a0..07b5116dfb1480 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) { } }