Skip to content

Commit 6083dcc

Browse files
authored
Return 0 when main falls off its end (#482)
1 parent f467a98 commit 6083dcc

4 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@ void Converter::ConvertFunctionBody(clang::FunctionDecl *decl) {
429429
return;
430430
}
431431
auto compound = clang::dyn_cast<clang::CompoundStmt>(decl->getBody());
432+
if (decl->isMain() && compound &&
433+
(compound->body_empty() ||
434+
!clang::isa<clang::ReturnStmt>(compound->body_back()))) {
435+
StrCat('0');
436+
return;
437+
}
432438
if (!compound || compound->body_empty()) {
433439
return;
434440
}

‎tests/unit/empty_main.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
int main(int, char **) { return 0; }
1+
int main(int, char **) {}

‎tests/unit/out/refcount/empty_main.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ pub fn main() {
2424
::std::process::exit(main_0(::std::env::args().len() as i32, argv.as_pointer()));
2525
}
2626
fn main_0(_a0: i32, _a1: Ptr<Ptr<i8>>) -> i32 {
27-
return 0;
27+
0
2828
}
2929
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/empty_main.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ pub fn main() {
2323
}
2424
}
2525
unsafe fn main_0(mut _a0: i32, mut _a1: *mut *mut libc::c_char) -> i32 {
26-
return 0;
26+
0
2727
}
2828
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)