Skip to content

Commit e38f874

Browse files
committed
work
1 parent 3f30d90 commit e38f874

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,14 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
26242624
auto type = expr->getTypeAsWritten();
26252625
auto *sub_expr = expr->getSubExpr();
26262626
if (type->isVoidType()) {
2627+
if (auto *assign =
2628+
clang::dyn_cast<clang::BinaryOperator>(sub_expr->IgnoreParens());
2629+
assign && assign->isAssignmentOp()) {
2630+
PushBrace brace(*this);
2631+
PushExprKind push(*this, ExprKind::Void);
2632+
Convert(sub_expr);
2633+
return false;
2634+
}
26272635
StrCat(token::kRef);
26282636
PushParen paren(*this);
26292637
PushExprKind push(*this, ExprKind::Void);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,14 @@ bool ConverterRefCount::VisitFunctionPointerCast(
15681568

15691569
bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
15701570
if (expr->getTypeAsWritten()->isVoidType()) {
1571+
if (auto *assign = clang::dyn_cast<clang::BinaryOperator>(
1572+
expr->getSubExpr()->IgnoreParens());
1573+
assign && assign->isAssignmentOp()) {
1574+
PushBrace brace(*this);
1575+
PushExprKind push(*this, ExprKind::Void);
1576+
Convert(expr->getSubExpr());
1577+
return false;
1578+
}
15711579
StrCat(token::kRef);
15721580
PushParen paren(*this);
15731581
PushExprKind push(*this, ExprKind::Void);

‎tests/unit/void_cast.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ int main() {
6565
assert(err == 7);
6666
assert(chosen == 123);
6767

68+
(void)(err += 1);
69+
assert(err == 8);
70+
71+
Holder assigned{1};
72+
(void)(assigned = Holder{2});
73+
assert(assigned.field == 2);
74+
75+
int target = 0;
76+
int *target_ptr = &target;
77+
(void)(*target_ptr = 5);
78+
assert(target == 5);
79+
6880
(void)bump_and_return;
6981
assert(side_effect_counter == 2);
7082

0 commit comments

Comments
 (0)