Skip to content

Commit 76b8137

Browse files
committed
don't add unused statement in increment/decrement operator
1 parent 210e58d commit 76b8137

File tree

6 files changed

+12
-22
lines changed

6 files changed

+12
-22
lines changed

c2rust-transpile/src/translator/operators.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,13 +1034,19 @@ impl<'c> Translation<'c> {
10341034
}
10351035
}?;
10361036

1037-
// Unused unary operators (`-foo()`) may have side effects, so we need
1038-
// to add them to stmts.
1039-
if ctx.is_unused() {
1037+
// Some unused unary operators (`-foo()`) may have side effects, so we need
1038+
// to add them to stmts when name is not increment/decrement operator.
1039+
if ctx.is_unused()
1040+
&& !matches!(
1041+
name,
1042+
c_ast::UnOp::PreDecrement
1043+
| c_ast::UnOp::PreIncrement
1044+
| c_ast::UnOp::PostDecrement
1045+
| c_ast::UnOp::PostIncrement
1046+
)
1047+
{
10401048
let v = unary.clone().into_value();
1041-
unary
1042-
.stmts_mut()
1043-
.push(Stmt::Expr(*v, Some(Default::default())));
1049+
unary.add_stmt(mk().semi_stmt(v));
10441050
}
10451051
Ok(unary)
10461052
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ pub unsafe extern "C" fn unary_without_side_effect() {
2929
&mut i;
3030
i;
3131
i += 1;
32-
i;
3332
i -= 1;
34-
i;
3533
i -= 1;
36-
i;
3734
i += 1;
38-
i;
3935
}
4036
#[no_mangle]
4137
pub unsafe extern "C" fn unary_with_side_effect() {
@@ -50,11 +46,7 @@ pub unsafe extern "C" fn unary_with_side_effect() {
5046
>(side_effect)() as isize) as *const std::ffi::c_char;
5147
*arr[side_effect() as usize];
5248
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(1);
53-
arr[side_effect() as usize];
5449
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(-1);
55-
arr[side_effect() as usize];
5650
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(1);
57-
arr[side_effect() as usize];
5851
arr[side_effect() as usize] = (arr[side_effect() as usize]).offset(-1);
59-
arr[side_effect() as usize];
6052
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub unsafe extern "C" fn factorial(mut n: std::ffi::c_ushort) -> std::ffi::c_ush
1919
while (i as std::ffi::c_int) < n as std::ffi::c_int {
2020
result = (result as std::ffi::c_int * i as std::ffi::c_int) as std::ffi::c_ushort;
2121
i = i.wrapping_add(1);
22-
i;
2322
}
2423
return result;
2524
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3-
assertion_line: 67
43
expression: cat tests/snapshots/gotos.rs
54
input_file: c2rust-transpile/tests/snapshots/gotos.c
65
---
@@ -19,8 +18,6 @@ pub unsafe extern "C" fn sum(mut count: std::ffi::c_int) -> std::ffi::c_int {
1918
while !(count <= 0 as std::ffi::c_int) {
2019
x += count;
2120
count -= 1;
22-
count;
2321
}
2422
return x;
2523
}
26-

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ pub unsafe extern "C" fn insertion_sort(n: std::ffi::c_int, p: *mut std::ffi::c_
2121
while j > 0 as std::ffi::c_int && *p.offset((j - 1 as std::ffi::c_int) as isize) > tmp {
2222
*p.offset(j as isize) = *p.offset((j - 1 as std::ffi::c_int) as isize);
2323
j -= 1;
24-
j;
2524
}
2625
*p.offset(j as isize) = tmp;
2726
i += 1;
28-
i;
2927
}
3028
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,10 @@ pub unsafe extern "C" fn stmt_expr_inc() -> std::ffi::c_int {
407407
({
408408
*b += 1;
409409
*b;
410-
*b;
411410
*b
412411
});
413412
return ({
414413
*b += 1;
415-
*b;
416414
*b
417415
});
418416
}

0 commit comments

Comments
 (0)