Skip to content

Commit

Permalink
[clang-tidy] performance-for-range-copy: Don't trigger on implicit ty…
Browse files Browse the repository at this point in the history
…pe 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
  • Loading branch information
Felix Berger committed Mar 3, 2021
1 parent b3ac90d commit a189b3b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Expand Up @@ -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")))
Expand Down
Expand Up @@ -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) {
}
}

Expand Down

0 comments on commit a189b3b

Please sign in to comment.