Skip to content

Commit 42f60a0

Browse files
committed
Remove redundant as i32 cast
1 parent 5cac927 commit 42f60a0

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,16 @@ bool Converter::VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr) {
17311731
return false;
17321732
}
17331733

1734+
void Converter::ConvertIntegerToEnumeralCast(clang::Expr *to,
1735+
clang::Expr *from) {
1736+
StrCat(GetUnsafeTypeAsString(to->getType()), "::from");
1737+
PushParen paren(*this);
1738+
Convert(from);
1739+
if (!from->getType()->isSpecificBuiltinType(clang::BuiltinType::Int)) {
1740+
StrCat(keyword::kAs, "i32");
1741+
}
1742+
}
1743+
17341744
bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17351745
auto *sub_expr = expr->getSubExpr();
17361746
auto type = expr->getType();
@@ -1847,10 +1857,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
18471857
break;
18481858
}
18491859
if (type->isEnumeralType() && !sub_expr->getType()->isEnumeralType()) {
1850-
StrCat(std::format("{}::from", GetUnsafeTypeAsString(type)));
1851-
PushParen paren(*this);
1852-
Convert(sub_expr);
1853-
StrCat(keyword::kAs, "i32");
1860+
ConvertIntegerToEnumeralCast(expr, sub_expr);
18541861
break;
18551862
}
18561863
{
@@ -1898,10 +1905,7 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
18981905
return false;
18991906
}
19001907
if (type->isEnumeralType() && !sub_expr->getType()->isEnumeralType()) {
1901-
StrCat(std::format("{}::from", GetUnsafeTypeAsString(type)));
1902-
PushParen paren(*this);
1903-
Convert(sub_expr);
1904-
StrCat(keyword::kAs, "i32");
1908+
ConvertIntegerToEnumeralCast(expr, sub_expr);
19051909
return false;
19061910
}
19071911
{

cpp2rust/converter/converter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
241241

242242
virtual bool VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr);
243243

244+
void ConvertIntegerToEnumeralCast(clang::Expr *to, clang::Expr *from);
245+
244246
virtual bool VisitImplicitCastExpr(clang::ImplicitCastExpr *expr);
245247

246248
virtual bool VisitExplicitCastExpr(clang::ExplicitCastExpr *expr);

0 commit comments

Comments
 (0)