From 4c70027f207088e390b993e37e60248eb2bc254d Mon Sep 17 00:00:00 2001 From: Rua Date: Wed, 10 Dec 2025 19:11:40 +0100 Subject: [PATCH 1/3] transpile: Add snapshot test for #1506 --- c2rust-transpile/tests/snapshots/exprs.c | 9 +++++++++ .../tests/snapshots/snapshots__transpile@exprs.c.snap | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/c2rust-transpile/tests/snapshots/exprs.c b/c2rust-transpile/tests/snapshots/exprs.c index 17eb867edb..4a30738bed 100644 --- a/c2rust-transpile/tests/snapshots/exprs.c +++ b/c2rust-transpile/tests/snapshots/exprs.c @@ -38,3 +38,12 @@ void compound_literal(){ /// https://github.com/immunant/c2rust/issues/1234 int i = (enum {A, B, C}){1}; } + +void statement_expr() { + ({ + puts("should execute"); + return; + }); + + puts("should be unreachable!"); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap index 56a782adb8..5d462fae89 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap @@ -11,7 +11,7 @@ input_file: c2rust-transpile/tests/snapshots/exprs.c unused_assignments, unused_mut )] -#![feature(raw_ref_op)] +#![feature(label_break_value, raw_ref_op)] extern "C" { fn puts(str: *const ::core::ffi::c_char) -> ::core::ffi::c_int; } @@ -56,3 +56,8 @@ pub unsafe extern "C" fn unary_with_side_effect() { pub unsafe extern "C" fn compound_literal() { let mut i: ::core::ffi::c_int = B as ::core::ffi::c_int; } +#[no_mangle] +pub unsafe extern "C" fn statement_expr() { + puts(b"should execute\0" as *const u8 as *const ::core::ffi::c_char); + puts(b"should be unreachable!\0" as *const u8 as *const ::core::ffi::c_char); +} From 7647a3fbacf5fe1bf6961f3f596fa034a2ead6c8 Mon Sep 17 00:00:00 2001 From: Rua Date: Wed, 10 Dec 2025 19:38:38 +0100 Subject: [PATCH 2/3] transpile: Strip tail `return` only when transpiling a function --- c2rust-transpile/src/cfg/mod.rs | 9 +---- c2rust-transpile/src/cfg/structures.rs | 13 +------ c2rust-transpile/src/translator/mod.rs | 37 +++++++++++++------ .../snapshots__transpile@exprs.c.snap | 1 + 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/c2rust-transpile/src/cfg/mod.rs b/c2rust-transpile/src/cfg/mod.rs index 5e7d7a599f..628aa2314b 100644 --- a/c2rust-transpile/src/cfg/mod.rs +++ b/c2rust-transpile/src/cfg/mod.rs @@ -2114,13 +2114,8 @@ impl CfgBuilder { }; // Run relooper - let mut stmts = translator.convert_cfg( - &format!("", stmt_id), - graph, - store, - live_in, - false, - )?; + let mut stmts = + translator.convert_cfg(&format!("", stmt_id), graph, store, live_in)?; let inner_span = stmts.first().map(|stmt| stmt.span()); diff --git a/c2rust-transpile/src/cfg/structures.rs b/c2rust-transpile/src/cfg/structures.rs index 0b209f2a05..d62dc6ca6a 100644 --- a/c2rust-transpile/src/cfg/structures.rs +++ b/c2rust-transpile/src/cfg/structures.rs @@ -2,7 +2,7 @@ use super::*; use log::warn; -use syn::{spanned::Spanned as _, ExprBreak, ExprIf, ExprReturn, ExprUnary, Stmt}; +use syn::{spanned::Spanned as _, ExprBreak, ExprIf, ExprUnary, Stmt}; use crate::rust_ast::{comment_store, set_span::SetSpan, BytePos, SpanExt}; @@ -12,7 +12,6 @@ pub fn structured_cfg( comment_store: &mut comment_store::CommentStore, current_block: Box, debug_labels: bool, - cut_out_trailing_ret: bool, ) -> TranslationResult> { let ast: StructuredAST, Pat, Label, Stmt> = structured_cfg_help(vec![], &IndexSet::new(), root, &mut IndexSet::new())?; @@ -21,15 +20,7 @@ pub fn structured_cfg( debug_labels, current_block, }; - let (mut stmts, _span) = s.to_stmt(ast, comment_store); - - // If the very last statement in the vector is a `return`, we can either cut it out or replace - // it with the returned value. - if cut_out_trailing_ret { - if let Some(Stmt::Expr(Expr::Return(ExprReturn { expr: None, .. }), _)) = stmts.last() { - stmts.pop(); - } - } + let (stmts, _span) = s.to_stmt(ast, comment_store); Ok(stmts) } diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 78b3e3c110..9bce9200f1 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -14,11 +14,11 @@ use proc_macro2::{Punct, Spacing::*, Span, TokenStream, TokenTree}; use syn::spanned::Spanned as _; use syn::{ AttrStyle, BareVariadic, Block, Expr, ExprBinary, ExprBlock, ExprBreak, ExprCast, ExprField, - ExprIndex, ExprParen, ExprUnary, FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro, - ForeignItemStatic, ForeignItemType, Ident, Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn, - ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, ItemTrait, - ItemTraitAlias, ItemType, ItemUnion, ItemUse, Lit, MacroDelimiter, PathSegment, ReturnType, - Stmt, Type, TypeTuple, UseTree, Visibility, + ExprIndex, ExprParen, ExprReturn, ExprUnary, FnArg, ForeignItem, ForeignItemFn, + ForeignItemMacro, ForeignItemStatic, ForeignItemType, Ident, Item, ItemConst, ItemEnum, + ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, + ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Lit, MacroDelimiter, PathSegment, + ReturnType, Stmt, Type, TypeTuple, UseTree, Visibility, }; use syn::{BinOp, UnOp}; // To override `c_ast::{BinOp,UnOp}` from glob import. @@ -2403,7 +2403,8 @@ impl<'c> Translation<'c> { _ => panic!("function body expects to be a compound statement"), }; let mut converted_body = - self.convert_function_body(ctx, name, body_ids, return_type, ret)?; + self.convert_block_with_scope(ctx, name, body_ids, return_type, ret)?; + strip_tail_return(&mut converted_body); // If `alloca` was used in the function body, include a variable to hold the // allocations. @@ -2520,7 +2521,6 @@ impl<'c> Translation<'c> { graph: cfg::Cfg, store: cfg::DeclStmtStore, live_in: IndexSet, - cut_out_trailing_ret: bool, ) -> TranslationResult> { if self.tcfg.dump_function_cfgs { graph @@ -2582,12 +2582,11 @@ impl<'c> Translation<'c> { &mut self.comment_store.borrow_mut(), current_block, self.tcfg.debug_relooper_labels, - cut_out_trailing_ret, )?); Ok(stmts) } - fn convert_function_body( + fn convert_block_with_scope( &self, ctx: ExprContext, name: &str, @@ -2598,7 +2597,7 @@ impl<'c> Translation<'c> { // Function body scope self.with_scope(|| { let (graph, store) = cfg::Cfg::from_stmts(self, ctx, body_ids, ret, ret_ty)?; - self.convert_cfg(name, graph, store, IndexSet::new(), true) + self.convert_cfg(name, graph, store, IndexSet::new()) }) } @@ -4262,10 +4261,16 @@ impl<'c> Translation<'c> { let mut stmts = match self.ast_context[result_id].kind { CStmtKind::Expr(expr_id) => { let ret = cfg::ImplicitReturnType::StmtExpr(ctx, expr_id, lbl.clone()); - self.convert_function_body(ctx, &name, &substmt_ids[0..(n - 1)], None, ret)? + self.convert_block_with_scope( + ctx, + &name, + &substmt_ids[0..(n - 1)], + None, + ret, + )? } - _ => self.convert_function_body( + _ => self.convert_block_with_scope( ctx, &name, substmt_ids, @@ -5282,3 +5287,11 @@ impl<'c> Translation<'c> { } } } + +// If the very last statement in the vector is a `return`, either cut it out or replace it with +// the returned value. +fn strip_tail_return(stmts: &mut Vec) { + if let Some(Stmt::Expr(Expr::Return(ExprReturn { expr: None, .. }), _)) = stmts.last() { + stmts.pop(); + } +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap index 5d462fae89..5a2418b6ea 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap @@ -59,5 +59,6 @@ pub unsafe extern "C" fn compound_literal() { #[no_mangle] pub unsafe extern "C" fn statement_expr() { puts(b"should execute\0" as *const u8 as *const ::core::ffi::c_char); + return; puts(b"should be unreachable!\0" as *const u8 as *const ::core::ffi::c_char); } From afaa01419198d5fa4def87cadb5029817ba274bf Mon Sep 17 00:00:00 2001 From: Rua Date: Fri, 12 Dec 2025 19:18:36 +0100 Subject: [PATCH 3/3] transpile: Convert final return statement into tail expression --- c2rust-transpile/src/translator/mod.rs | 94 ++++++++++++++++--- ...snapshots__transpile-aarch64@vm_x86.c.snap | 2 +- .../snapshots__transpile-linux@macros.c.snap | 4 +- .../snapshots__transpile-linux@rnd.c.snap | 2 +- .../snapshots__transpile-linux@rotate.c.snap | 10 +- .../snapshots__transpile-macos@macros.c.snap | 4 +- .../snapshots__transpile-macos@rnd.c.snap | 2 +- .../snapshots__transpile-macos@rotate.c.snap | 4 +- .../snapshots__transpile-x86_64@vm_x86.c.snap | 2 +- .../snapshots__transpile@alloca.c.snap | 2 +- .../snapshots__transpile@atomics.c.snap | 2 +- .../snapshots__transpile@exprs.c.snap | 2 +- .../snapshots__transpile@factorial.c.snap | 2 +- .../snapshots__transpile@gotos.c.snap | 2 +- .../snapshots__transpile@macrocase.c.snap | 2 +- .../snapshots__transpile@macros.c.snap | 32 +++---- .../snapshots__transpile@main_fn.c.snap | 2 +- .../snapshots__transpile@ref_ub.c.snap | 2 +- .../snapshots__transpile@rotate.c.snap | 12 +-- 19 files changed, 124 insertions(+), 60 deletions(-) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 9bce9200f1..f533af0184 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -13,12 +13,13 @@ use log::{error, info, trace, warn}; use proc_macro2::{Punct, Spacing::*, Span, TokenStream, TokenTree}; use syn::spanned::Spanned as _; use syn::{ - AttrStyle, BareVariadic, Block, Expr, ExprBinary, ExprBlock, ExprBreak, ExprCast, ExprField, - ExprIndex, ExprParen, ExprReturn, ExprUnary, FnArg, ForeignItem, ForeignItemFn, - ForeignItemMacro, ForeignItemStatic, ForeignItemType, Ident, Item, ItemConst, ItemEnum, - ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, - ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Lit, MacroDelimiter, PathSegment, - ReturnType, Stmt, Type, TypeTuple, UseTree, Visibility, + Arm, AttrStyle, BareVariadic, Block, Expr, ExprBinary, ExprBlock, ExprBreak, ExprCast, + ExprField, ExprIf, ExprIndex, ExprMatch, ExprParen, ExprReturn, ExprTuple, ExprUnary, + ExprUnsafe, FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro, ForeignItemStatic, + ForeignItemType, Ident, Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn, ItemForeignMod, + ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, + ItemUnion, ItemUse, Lit, MacroDelimiter, PathSegment, ReturnType, Stmt, Type, TypeTuple, + UseTree, Visibility, }; use syn::{BinOp, UnOp}; // To override `c_ast::{BinOp,UnOp}` from glob import. @@ -340,12 +341,7 @@ pub fn stmts_block(mut stmts: Vec) -> Block { }), None, )) if stmts.is_empty() => return block, - Some(mut s) => { - if let Stmt::Expr(e, None) = s { - s = Stmt::Expr(e, Some(Default::default())); - } - stmts.push(s); - } + Some(s) => stmts.push(s), } mk().block(stmts) } @@ -5291,7 +5287,77 @@ impl<'c> Translation<'c> { // If the very last statement in the vector is a `return`, either cut it out or replace it with // the returned value. fn strip_tail_return(stmts: &mut Vec) { - if let Some(Stmt::Expr(Expr::Return(ExprReturn { expr: None, .. }), _)) = stmts.last() { - stmts.pop(); + if let Some(Stmt::Expr(expr, semi)) = stmts.last_mut() { + *semi = None; + strip_tail_return_expr(expr); + + // If the expression was replaced with an empty tuple (), then just delete it altogether. + if matches!(expr, Expr::Tuple(ExprTuple { elems, .. }) if elems.is_empty()) { + stmts.pop(); + } + } +} + +// If a return is found, replace it with the returned expression. +// If an expression of another kind is found, and it contains a subexpression that becomes the +// final value of the whole, then recurse down into it. +fn strip_tail_return_expr(expr: &mut Expr) { + match expr { + old_expr @ Expr::Return(ExprReturn { .. }) => { + // placeholder value to allow swapping + let temp = mem::replace(old_expr, Expr::Verbatim(Default::default())); + // TODO: Rust 1.65: use let-else + let expr = match temp { + Expr::Return(ExprReturn { expr, .. }) => expr, + _ => unreachable!(), + }; + + if let Some(expr) = expr { + // Replace return + expression with the expression. + *old_expr = *expr; + } else { + // Replace standalone return with () + *old_expr = *mk().tuple_expr(vec![]); + } + } + + // Simple blocks, recurse down + // TODO: add when syn is updated + // | Expr::Const(ExprConst { block, .. }) + Expr::Block(ExprBlock { block, .. }) | Expr::Unsafe(ExprUnsafe { block, .. }) => { + strip_tail_return(&mut block.stmts); + } + + // Recurse down both branches of the `if` + Expr::If(ExprIf { + then_branch, + else_branch, + .. + }) => { + strip_tail_return(&mut then_branch.stmts); + + // If the function returns a value, then there must be an else_branch, + // but if it returns void then there doesn't have to be. For example + // + // if condition { + // // stuff + // return; + // } + // } // end of function + if let Some((_, else_branch)) = else_branch { + strip_tail_return_expr(else_branch); + } + } + + // Recurse down all arms of the `match` + Expr::Match(ExprMatch { arms, .. }) => { + for Arm { body, .. } in arms { + strip_tail_return_expr(body); + } + } + + // Other expression types do not have blocks with tail expressions, whose value becomes + // that of the whole expression. + _ => (), } } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap index b0b1a88287..01ce5eff6a 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap @@ -68,5 +68,5 @@ pub unsafe extern "C" fn VM_CallCompiled( return 0 as ::core::ffi::c_int; } (*vm).programStack = stackOnEntry; - return *opStack.offset(opStackOfs as isize); + *opStack.offset(opStackOfs as isize) } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap index 2c6f0f2a7e..4a658fb4ed 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap @@ -22,12 +22,12 @@ extern "C" { pub type size_t = usize; #[no_mangle] pub unsafe extern "C" fn errno_is_error() -> bool { - return *__errno_location() != 0 as ::core::ffi::c_int; + *__errno_location() != 0 as ::core::ffi::c_int } #[no_mangle] pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { let mut a: [::core::ffi::c_int; 10] = [0; 10]; - return SIZE as ::core::ffi::c_int; + SIZE as ::core::ffi::c_int } pub const SIZE: usize = ::core::mem::size_of::<[::core::ffi::c_int; 10]>(); pub const POS: [::core::ffi::c_char; 3] = diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap index 4b8296e79c..ced9a5ceef 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap @@ -32,5 +32,5 @@ pub unsafe extern "C" fn get_rand_seed() -> uint32_t { .wrapping_mul(cur_rand_seed) .wrapping_add(INCREMENT); let mut ret: uint32_t = abs(cur_rand_seed as ::core::ffi::c_int) as uint32_t; - return ret; + ret } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap index 5db0995d02..35e14ee6a1 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap @@ -15,15 +15,13 @@ input_file: c2rust-transpile/tests/snapshots/os-specific/rotate.c pub unsafe extern "C" fn rotate_left_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { - return (x as ::core::ffi::c_ulong) - .rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulong as u32) - as ::core::ffi::c_ulonglong; + (x as ::core::ffi::c_ulong).rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulong as u32) + as ::core::ffi::c_ulonglong } #[no_mangle] pub unsafe extern "C" fn rotate_right_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { - return (x as ::core::ffi::c_ulong) - .rotate_right(4 as ::core::ffi::c_int as ::core::ffi::c_ulong as u32) - as ::core::ffi::c_ulonglong; + (x as ::core::ffi::c_ulong).rotate_right(4 as ::core::ffi::c_int as ::core::ffi::c_ulong as u32) + as ::core::ffi::c_ulonglong } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap index 375ddd0edf..fceb18a707 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap @@ -23,12 +23,12 @@ pub type __darwin_size_t = usize; pub type size_t = __darwin_size_t; #[no_mangle] pub unsafe extern "C" fn errno_is_error() -> bool { - return *__error() != 0 as ::core::ffi::c_int; + *__error() != 0 as ::core::ffi::c_int } #[no_mangle] pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { let mut a: [::core::ffi::c_int; 10] = [0; 10]; - return SIZE as ::core::ffi::c_int; + SIZE as ::core::ffi::c_int } pub const SIZE: usize = ::core::mem::size_of::<[::core::ffi::c_int; 10]>(); pub const POS: [::core::ffi::c_char; 3] = diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap index 1043201bf3..dab32bfc9b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap @@ -30,5 +30,5 @@ pub unsafe extern "C" fn get_rand_seed() -> uint32_t { .wrapping_mul(cur_rand_seed) .wrapping_add(INCREMENT); let mut ret: uint32_t = abs(cur_rand_seed as ::core::ffi::c_int) as uint32_t; - return ret; + ret } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap index f6e1255ff6..370eb0b88a 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap @@ -15,11 +15,11 @@ input_file: c2rust-transpile/tests/snapshots/os-specific/rotate.c pub unsafe extern "C" fn rotate_left_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { - return x.rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulonglong as u32); + x.rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulonglong as u32) } #[no_mangle] pub unsafe extern "C" fn rotate_right_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { - return x.rotate_right(4 as ::core::ffi::c_int as ::core::ffi::c_ulonglong as u32); + x.rotate_right(4 as ::core::ffi::c_int as ::core::ffi::c_ulonglong as u32) } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap index b38469f35d..77fb6c3520 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap @@ -78,5 +78,5 @@ pub unsafe extern "C" fn VM_CallCompiled( return 0 as ::core::ffi::c_int; } (*vm).programStack = stackOnEntry; - return *opStack.offset(opStackOfs as isize); + *opStack.offset(opStackOfs as isize) } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap index cc09026f03..4ee2348054 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap @@ -37,5 +37,5 @@ pub unsafe extern "C" fn alloca_sum( alloca2 = alloca_allocations.last_mut().unwrap().as_mut_ptr() as *mut ::core::ffi::c_int; *alloca2 = val2; } - return *alloca1 + *alloca2; + *alloca1 + *alloca2 } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap index 7a9af1db0e..fd3825b4d5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap @@ -35,6 +35,6 @@ pub unsafe extern "C" fn c11_atomics(mut x: ::core::ffi::c_int) -> ::core::ffi:: ::core::intrinsics::atomic_cxchgweak_seqcst_seqcst(&raw mut x, *&raw mut expected, desired); *&raw mut expected = fresh1.0; fresh1.1; - return x; + x } pub const __ATOMIC_SEQ_CST: ::core::ffi::c_int = 5 as ::core::ffi::c_int; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap index 5a2418b6ea..5c6b368f2e 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap @@ -21,7 +21,7 @@ pub const B: C2RustUnnamed = 1; pub const A: C2RustUnnamed = 0; unsafe extern "C" fn side_effect() -> ::core::ffi::c_int { puts(b"the return of side effect\0" as *const u8 as *const ::core::ffi::c_char); - return 10 as ::core::ffi::c_int; + 10 as ::core::ffi::c_int } #[no_mangle] pub unsafe extern "C" fn unary_without_side_effect() { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap index c179b35ca8..c7f6c7058b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap @@ -19,5 +19,5 @@ pub unsafe extern "C" fn factorial(mut n: ::core::ffi::c_ushort) -> ::core::ffi: result = (result as ::core::ffi::c_int * i as ::core::ffi::c_int) as ::core::ffi::c_ushort; i = i.wrapping_add(1); } - return result; + result } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap index f0fbcd7b90..2d3a71c567 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap @@ -18,5 +18,5 @@ pub unsafe extern "C" fn sum(mut count: ::core::ffi::c_int) -> ::core::ffi::c_in x += count; count -= 1; } - return x; + x } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap index 5a32f730df..0b66f5d71f 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap @@ -29,5 +29,5 @@ pub unsafe extern "C" fn ZSTD_dParam_getBounds(mut dParam: ZSTD_dParameter) -> : } _ => {} } - return bounds; + bounds } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap index 49b31f2c83..6b4f1f6c3c 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap @@ -342,7 +342,7 @@ pub static mut global_const_ternary: ::core::ffi::c_int = 0; pub static mut global_const_member: ::core::ffi::c_int = 0; #[no_mangle] pub unsafe extern "C" fn test_fn_macro(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { - return x * x; + x * x } pub const TEST_CONST1: ::core::ffi::c_int = 1 as ::core::ffi::c_int; pub const TEST_NESTED: ::core::ffi::c_int = 2 as ::core::ffi::c_int; @@ -356,7 +356,7 @@ pub unsafe extern "C" fn reference_define() -> ::core::ffi::c_int { if (3 as ::core::ffi::c_int) < TEST_PARENS { x += TEST_PARENS; } - return x; + x } #[no_mangle] pub static mut fns: fn_ptrs = fn_ptrs { @@ -370,11 +370,11 @@ pub const ZSTD_WINDOWLOG_MAX_32: ::core::ffi::c_int = 30 as ::core::ffi::c_int; pub const ZSTD_WINDOWLOG_MAX_64: ::core::ffi::c_int = 31 as ::core::ffi::c_int; #[no_mangle] pub unsafe extern "C" fn test_zstd() -> U64 { - return (if ::core::mem::size_of::() as usize == 4 as usize { + (if ::core::mem::size_of::() as usize == 4 as usize { ZSTD_WINDOWLOG_MAX_32 } else { ZSTD_WINDOWLOG_MAX_64 - }) as U64; + }) as U64 } #[no_mangle] pub unsafe extern "C" fn stmt_expr_inc() -> ::core::ffi::c_int { @@ -385,10 +385,10 @@ pub unsafe extern "C" fn stmt_expr_inc() -> ::core::ffi::c_int { *b; *b }); - return ({ + ({ *b += 1; *b - }); + }) } #[no_mangle] pub unsafe extern "C" fn test_switch(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { @@ -397,7 +397,7 @@ pub unsafe extern "C" fn test_switch(mut x: ::core::ffi::c_int) -> ::core::ffi:: TEST_NESTED => return 20 as ::core::ffi::c_int, _ => {} } - return 0 as ::core::ffi::c_int; + 0 as ::core::ffi::c_int } pub const silk_int16_MIN: ::core::ffi::c_short = 0x8000 as ::core::ffi::c_int as ::core::ffi::c_short; @@ -406,37 +406,37 @@ pub unsafe extern "C" fn test_silk_int16_MIN() -> ::core::ffi::c_int { let mut _null: ::core::ffi::c_char = ::core::mem::transmute::<[u8; 1], [::core::ffi::c_char; 1]>(*b"\0") [(silk_int16_MIN as ::core::ffi::c_int + 0x8000 as ::core::ffi::c_int) as usize]; - return silk_int16_MIN as ::core::ffi::c_int; + silk_int16_MIN as ::core::ffi::c_int } #[no_mangle] pub unsafe extern "C" fn use_extern_value() -> ::core::ffi::c_int { - return extern_fn(); + extern_fn() } #[no_mangle] pub unsafe extern "C" fn local_fn() -> ::core::ffi::c_int { - return 1234 as ::core::ffi::c_int; + 1234 as ::core::ffi::c_int } #[no_mangle] pub unsafe extern "C" fn use_local_value() -> ::core::ffi::c_int { - return local_fn(); + local_fn() } #[no_mangle] pub unsafe extern "C" fn use_portable_type(mut len: uintptr_t) -> bool { - return len <= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as ::core::ffi::c_int as uintptr_t); + len <= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as ::core::ffi::c_int as uintptr_t) } #[no_mangle] pub unsafe extern "C" fn ntlm_v2_blob_len(mut ntlm: *mut ntlmdata) -> ::core::ffi::c_uint { - return ((44 as ::core::ffi::c_int - 16 as ::core::ffi::c_int) as ::core::ffi::c_uint) + ((44 as ::core::ffi::c_int - 16 as ::core::ffi::c_int) as ::core::ffi::c_uint) .wrapping_add((*ntlm).target_info_len) - .wrapping_add(4 as ::core::ffi::c_uint); + .wrapping_add(4 as ::core::ffi::c_uint) } #[no_mangle] pub unsafe extern "C" fn late_init_var() -> ::core::ffi::c_int { - return ({ + ({ let mut i: ::core::ffi::c_int = 0; i = 1 as ::core::ffi::c_int; i - }); + }) } unsafe extern "C" fn run_static_initializers() { global_static_const_ptr_arithmetic = PTR_ARITHMETIC; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.snap index 59d3df0542..d18d001a74 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.snap @@ -16,7 +16,7 @@ unsafe fn main_0( mut argv: *mut *mut ::core::ffi::c_char, mut envp: *mut *mut ::core::ffi::c_char, ) -> ::core::ffi::c_int { - return 0 as ::core::ffi::c_int; + 0 as ::core::ffi::c_int } pub fn main() { let mut args_strings: Vec> = ::std::env::args() diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap index 620c5bfb9b..a98b01c726 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap @@ -29,7 +29,7 @@ unsafe fn main_0() -> ::core::ffi::c_int { dec(fp); f.len = 6 as ::core::ffi::c_int; dec(fp); - return 0; + 0 } pub fn main() { unsafe { ::std::process::exit(main_0() as i32) } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap index f6653f6360..5f139325f2 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap @@ -13,25 +13,25 @@ input_file: c2rust-transpile/tests/snapshots/rotate.c )] #[no_mangle] pub unsafe extern "C" fn rotate_left_8(mut x: ::core::ffi::c_uchar) -> ::core::ffi::c_uchar { - return x.rotate_left(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32); + x.rotate_left(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32) } #[no_mangle] pub unsafe extern "C" fn rotate_left_16(mut x: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { - return x.rotate_left(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32); + x.rotate_left(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32) } #[no_mangle] pub unsafe extern "C" fn rotate_left_32(mut x: ::core::ffi::c_uint) -> ::core::ffi::c_uint { - return x.rotate_left(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32); + x.rotate_left(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32) } #[no_mangle] pub unsafe extern "C" fn rotate_right_8(mut x: ::core::ffi::c_uchar) -> ::core::ffi::c_uchar { - return x.rotate_right(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32); + x.rotate_right(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32) } #[no_mangle] pub unsafe extern "C" fn rotate_right_16(mut x: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { - return x.rotate_right(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32); + x.rotate_right(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32) } #[no_mangle] pub unsafe extern "C" fn rotate_right_32(mut x: ::core::ffi::c_uint) -> ::core::ffi::c_uint { - return x.rotate_right(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32); + x.rotate_right(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32) }