Skip to content

Commit 01e8de6

Browse files
committed
Handle floating to boolean cast
1 parent f9f633f commit 01e8de6

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
27102710
Convert(sub_expr);
27112711
break;
27122712
case clang::CastKind::CK_IntegralToBoolean:
2713+
case clang::CastKind::CK_FloatingToBoolean:
27132714
ConvertIntegralToBooleanCast(expr);
27142715
break;
27152716
case clang::CastKind::CK_PointerToBoolean:

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,8 @@ clang::Expr *NormalizeToBool(clang::Expr *expr, clang::ASTContext &ctx) {
21312131
clang::CastKind cast_kind;
21322132
if (expr->getType()->isPointerType()) {
21332133
cast_kind = clang::CK_PointerToBoolean;
2134+
} else if (expr->getType()->isFloatingType()) {
2135+
cast_kind = clang::CK_FloatingToBoolean;
21342136
} else /* expr->getType()->isIntegerType() */ {
21352137
cast_kind = clang::CK_IntegralToBoolean;
21362138
}

‎tests/unit/expr_as_bool_cpp.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,14 @@ int main() {
6666
}
6767
assert(d == 0.0);
6868

69+
float f = 2.0f;
70+
bool fb = f;
71+
bool nf = !f;
72+
assert(fb);
73+
assert(!nf);
74+
assert(!0.0);
75+
int fi = (bool)f;
76+
assert(fi == 1);
77+
6978
return 0;
7079
}

0 commit comments

Comments
 (0)