c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

Missing parentheses on reference applied to a cast

Open glguy opened this issue 1 year ago • 0 comments

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). ...

glguy avatar Apr 22 '24 23:04 glguy