immunant / c2rust

Migrate C code to Rust
https://c2rust.com/
Other
3.79k stars 219 forks source link

Missing parentheses on reference applied to a cast #1084

Open glguy opened 2 months ago

glguy commented 2 months ago
int main(void) {
    int * x = &(int){0};
}

generates

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
unsafe fn main_0() -> libc::c_int {
    let mut x: *mut libc::c_int = &mut 0 as libc::c_int as *mut libc::c_int;
    return 0;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}

but extra parentheses are needed

    let mut x: *mut libc::c_int = &mut (0 as libc::c_int) as *mut libc::c_int;

This can happen in more realistic code when an output function parameter is being ignored f(&(int){0});

If someone reads this issue and wonders if this is even legal C, check out compound literal

The value category of a compound literal is lvalue (its address can be taken). ...