Skip to content

Commit 3acd6a5

Browse files
committed
Only apply IsRedundantCopyInConversion for iterators
1 parent 1ac73cc commit 3acd6a5

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
@@ -636,14 +636,31 @@ std::string GetClassName(clang::QualType type) {
636636
return {};
637637
}
638638

639+
static bool IsIteratorType(clang::QualType qt) {
640+
if (auto *record = qt->getAsCXXRecordDecl()) {
641+
for (auto *d : record->decls()) {
642+
if (auto *tnd = llvm::dyn_cast<clang::TypedefNameDecl>(d)) {
643+
if (tnd->getName() == "iterator_category")
644+
return true;
645+
}
646+
}
647+
}
648+
return false;
649+
}
650+
639651
bool IsRedundantCopyInConversion(clang::ASTContext &ctx,
640652
const clang::CXXConstructExpr *expr) {
641-
auto parents = ctx.getParentMapContext().getParents(*expr);
642-
if (parents.empty()) {
643-
return false;
653+
if (auto parents = ctx.getParentMapContext().getParents(*expr);
654+
!parents.empty()) {
655+
if (auto *parent = parents[0].get<clang::CXXConstructExpr>()) {
656+
if (parent->getConstructor()->isConvertingConstructor(
657+
/* AllowExplicit= */ false)) {
658+
return IsIteratorType(expr->getType()) &&
659+
IsIteratorType(parent->getType());
660+
}
661+
}
644662
}
645-
auto *parent = parents[0].get<clang::CXXConstructExpr>();
646-
return parent && parent->getConstructor()->isConvertingConstructor(false);
663+
return false;
647664
}
648665

649666
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)