Skip to content

Commit d66f043

Browse files
committed
Only apply IsRedundantCopyInConversion for iterators
1 parent 942d599 commit d66f043

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

cpp2rust/converter/converter_lib.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,31 @@ std::string GetClassName(clang::QualType type) {
644644
return {};
645645
}
646646

647+
static bool IsIteratorType(clang::QualType qt) {
648+
if (auto *record = qt->getAsCXXRecordDecl()) {
649+
for (auto *d : record->decls()) {
650+
if (auto *tnd = llvm::dyn_cast<clang::TypedefNameDecl>(d)) {
651+
if (tnd->getName() == "iterator_category")
652+
return true;
653+
}
654+
}
655+
}
656+
return false;
657+
}
658+
647659
bool IsRedundantCopyInConversion(clang::ASTContext &ctx,
648660
const clang::CXXConstructExpr *expr) {
649-
auto parents = ctx.getParentMapContext().getParents(*expr);
650-
if (parents.empty()) {
651-
return false;
661+
if (auto parents = ctx.getParentMapContext().getParents(*expr);
662+
!parents.empty()) {
663+
if (auto *parent = parents[0].get<clang::CXXConstructExpr>()) {
664+
if (parent->getConstructor()->isConvertingConstructor(
665+
/* AllowExplicit= */ false)) {
666+
return IsIteratorType(expr->getType()) &&
667+
IsIteratorType(parent->getType());
668+
}
669+
}
652670
}
653-
auto *parent = parents[0].get<clang::CXXConstructExpr>();
654-
return parent && parent->getConstructor()->isConvertingConstructor(false);
671+
return false;
655672
}
656673

657674
bool IsVaListType(clang::QualType type) {

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,9 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
15641564
}
15651565

15661566
if (ctor->isCopyConstructor()) {
1567-
StrCat(ConvertRValue(expr->getArg(0)));
1567+
StrCat(IsRedundantCopyInConversion(ctx_, expr)
1568+
? ConvertRValue(expr->getArg(0))
1569+
: ConvertFreshRValue(expr->getArg(0)));
15681570
return false;
15691571
}
15701572

0 commit comments

Comments
 (0)