Skip to content

Commit 02bc6f3

Browse files
committed
transpile: Avoid unnecessary parentheses around calls or fields
1 parent 621fc16 commit 02bc6f3

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

c2rust-ast-builder/src/builder.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,19 +2222,21 @@ fn parenthesize_if_necessary(mut outer: Expr) -> Expr {
22222222
}
22232223
};
22242224
match outer {
2225+
Expr::MethodCall(ref mut emc) => {
2226+
// Do not parenthesize `a.b.c()` or `a().b()` or `a[b].c()`
2227+
if !matches!(
2228+
*emc.receiver,
2229+
Expr::Field(..) | Expr::Call(..) | Expr::Index(..)
2230+
) {
2231+
parenthesize_if_gt(&mut emc.receiver);
2232+
}
2233+
}
22252234
Expr::Field(ref mut ef) => {
2226-
if let Expr::Index(_) = *ef.base {
2227-
/* we do not need to parenthesize the indexing in a[b].c */
2228-
} else {
2229-
/*if let Expr::Unary(_) = *ef.base {
2230-
parenthesize_mut(&mut ef.base);
2231-
} else { */
2235+
// Do not parenthesize `a().b` or `a[b].c`
2236+
if !matches!(*ef.base, Expr::Call(..) | Expr::Index(..)) {
22322237
parenthesize_if_gt(&mut ef.base);
22332238
}
22342239
}
2235-
Expr::MethodCall(ref mut emc) => {
2236-
parenthesize_if_gt(&mut emc.receiver);
2237-
}
22382240
Expr::Call(ref mut ec) => {
22392241
parenthesize_if_gt(&mut ec.func);
22402242
}

c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub unsafe extern "C" fn VM_CallCompiled(
5151
as *mut ::core::ffi::c_int) = 0 as ::core::ffi::c_int;
5252
*(&mut *image.offset(programStack as isize) as *mut byte as *mut ::core::ffi::c_int) =
5353
-(1 as ::core::ffi::c_int);
54-
entryPoint = ((*vm).codeBase).offset((*vm).entryOfs as isize);
54+
entryPoint = (*vm).codeBase.offset((*vm).entryOfs as isize);
5555
opStack =
5656
(stack.as_mut_ptr() as *mut ::core::ffi::c_int).offset(16 as ::core::ffi::c_int as isize);
5757
*opStack = 0 as ::core::ffi::c_int;

c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub unsafe extern "C" fn VM_CallCompiled(
5353
as *mut ::core::ffi::c_int) = 0 as ::core::ffi::c_int;
5454
*(&mut *image.offset(programStack as isize) as *mut byte as *mut ::core::ffi::c_int) =
5555
-(1 as ::core::ffi::c_int);
56-
entryPoint = ((*vm).codeBase).offset((*vm).entryOfs as isize);
56+
entryPoint = (*vm).codeBase.offset((*vm).entryOfs as isize);
5757
opStack =
5858
(stack.as_mut_ptr() as *mut ::core::ffi::c_int).offset(16 as ::core::ffi::c_int as isize);
5959
*opStack = 0 as ::core::ffi::c_int;

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ pub unsafe extern "C" fn unary_with_side_effect() {
4848
unsafe extern "C" fn() -> ::core::ffi::c_int,
4949
>(side_effect)() as isize) as *const ::core::ffi::c_char;
5050
*arr[side_effect() as usize];
51-
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(1);
52-
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(-1);
53-
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(1);
54-
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(-1);
51+
arr[side_effect() as usize] = arr[side_effect() as usize].offset(1);
52+
arr[side_effect() as usize] = arr[side_effect() as usize].offset(-1);
53+
arr[side_effect() as usize] = arr[side_effect() as usize].offset(1);
54+
arr[side_effect() as usize] = arr[side_effect() as usize].offset(-1);
5555
}
5656
#[no_mangle]
5757
pub unsafe extern "C" fn compound_literal() {

0 commit comments

Comments
 (0)