-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
c2rust handles else-if chains poorly #769
Comments
Is this something |
Clippy works on the small example here, but warns on and takes multiple iterations to fix longer examples. |
I do think |
For some information, this simple c code int simple(int i){
if(i == 0){
return 0;
}else if(i == 10){
return 10;
}else if(i == 20){
return 20;
}else if(i == 30){
return 30;
}else{
return -1;
}
} actually producing correct #![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#[no_mangle]
pub unsafe extern "C" fn simple(mut i: libc::c_int) -> libc::c_int {
if i == 0 as libc::c_int {
return 0 as libc::c_int
} else if i == 10 as libc::c_int {
return 10 as libc::c_int
} else if i == 20 as libc::c_int {
return 20 as libc::c_int
} else if i == 30 as libc::c_int {
return 30 as libc::c_int
} else {
return -(1 as libc::c_int)
};
} |
however, if last int simple(int i){
if(i == 0){
return 0;
}else if(i == 10){
return 10;
}else if(i == 20){
return 20;
}else if(i == 30){
return 30;
}
return -1;
} it will produce: #[no_mangle]
pub unsafe extern "C" fn simple(mut i: libc::c_int) -> libc::c_int {
if i == 0 as libc::c_int {
return 0 as libc::c_int
} else {
if i == 10 as libc::c_int {
return 10 as libc::c_int
} else {
if i == 20 as libc::c_int {
return 20 as libc::c_int
} else {
if i == 30 as libc::c_int {
return 30 as libc::c_int;
}
}
}
}
return -(1 as libc::c_int);
} |
c2rust handles else-if chains poorly, see example.
As an example this function:
Currently translates on the website (https://c2rust.com) as:
(The nesting depth gets worse as the number of else-ifs increases.)
But it could translate as:
This translation would be better formatted, and better preserve the format of the original code.
The text was updated successfully, but these errors were encountered: