Skip to content

Commit d7ebcd4

Browse files
committed
translator: remove redundant explicit type casts and generic parameters
1 parent eb146b0 commit d7ebcd4

12 files changed

+122
-176
lines changed

c2rust-transpile/src/cfg/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,8 @@ impl Cfg<Label, StmtOrDecl> {
602602
.push(StmtOrDecl::Stmt(mk().semi_stmt(mk().return_expr(ret_expr))));
603603
}
604604
ImplicitReturnType::Void => {
605-
wip.body.push(StmtOrDecl::Stmt(
606-
mk().semi_stmt(mk().return_expr(None as Option<Box<Expr>>)),
607-
));
605+
wip.body
606+
.push(StmtOrDecl::Stmt(mk().semi_stmt(mk().return_expr(None))));
608607
}
609608
ImplicitReturnType::NoImplicitReturnType => {
610609
// NOTE: emitting `ret_expr` is not necessarily an error. For instance,
@@ -2028,10 +2027,10 @@ impl CfgBuilder {
20282027
false,
20292028
),
20302029

2031-
Some(ImplicitReturnType::Void) => (mk().return_expr(None as Option<Box<Expr>>), false),
2030+
Some(ImplicitReturnType::Void) => (mk().return_expr(None), false),
20322031

20332032
_ => (
2034-
mk().break_expr_value(Some(brk_lbl.pretty_print()), None as Option<Box<Expr>>),
2033+
mk().break_expr_value(Some(brk_lbl.pretty_print()), None),
20352034
true,
20362035
),
20372036
};

c2rust-transpile/src/cfg/structures.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl StructureState {
491491
let (stmts, span) = self.to_stmt(stmts, comment_store);
492492

493493
let body = mk().block_expr(mk().span(span).block(stmts));
494-
mk().arm(pat, None as Option<Box<Expr>>, body)
494+
mk().arm(pat, None, body)
495495
})
496496
.collect();
497497

@@ -515,19 +515,16 @@ impl StructureState {
515515
let mut if_stmt = match (then_stmts.is_empty(), els_stmts.is_empty()) {
516516
(true, true) => mk().semi_stmt(cond),
517517
(false, true) => {
518-
let if_expr = mk().ifte_expr(
519-
cond,
520-
mk().span(then_span).block(then_stmts),
521-
None as Option<Box<Expr>>,
522-
);
518+
let if_expr =
519+
mk().ifte_expr(cond, mk().span(then_span).block(then_stmts), None);
523520
mk().expr_stmt(if_expr)
524521
}
525522
(true, false) => {
526523
let negated_cond = not(&cond);
527524
let if_expr = mk().ifte_expr(
528525
negated_cond,
529526
mk().span(els_span).block(els_stmts),
530-
None as Option<Box<Expr>>,
527+
None,
531528
);
532529
mk().expr_stmt(if_expr)
533530
}
@@ -582,15 +579,15 @@ impl StructureState {
582579
};
583580
let pat = mk().lit_pat(lbl_expr);
584581
let body = mk().block_expr(mk().span(stmts_span).block(stmts));
585-
mk().arm(pat, None as Option<Box<Expr>>, body)
582+
mk().arm(pat, None, body)
586583
})
587584
.collect();
588585

589586
let (then, then_span) = self.to_stmt(*then, comment_store);
590587

591588
arms.push(mk().arm(
592589
mk().wild_pat(),
593-
None as Option<Box<Expr>>,
590+
None,
594591
mk().block_expr(mk().span(then_span).block(then)),
595592
));
596593

c2rust-transpile/src/convert_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl TypeConverter {
311311
}
312312

313313
match ctxt.index(ctype).kind {
314-
CTypeKind::Void => Ok(mk().tuple_ty(vec![] as Vec<Box<Type>>)),
314+
CTypeKind::Void => Ok(mk().tuple_ty(vec![])),
315315
CTypeKind::Bool => Ok(mk().path_ty(mk().path(vec!["bool"]))),
316316
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["libc", "c_short"]))),
317317
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["libc", "c_int"]))),

c2rust-transpile/src/translator/assembly.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -850,18 +850,14 @@ impl<'c> Translation<'c> {
850850
let output_name = self.renamer.borrow_mut().fresh();
851851
let output_local = mk().local(
852852
mk().ident_pat(&output_name),
853-
None as Option<Box<Type>>,
853+
None,
854854
Some(mk().mutbl().addr_of_expr(out_expr)),
855855
);
856856
stmts.push(mk().local_stmt(Box::new(output_local)));
857857

858858
// `let mut freshN;`
859859
let inner_name = self.renamer.borrow_mut().fresh();
860-
let inner_local = mk().local(
861-
mk().ident_pat(&inner_name),
862-
None as Option<Box<Type>>,
863-
None as Option<Box<Expr>>,
864-
);
860+
let inner_local = mk().local(mk().ident_pat(&inner_name), None, None);
865861
stmts.push(mk().local_stmt(Box::new(inner_local)));
866862

867863
out_expr = mk().ident_expr(&inner_name);
@@ -892,11 +888,7 @@ impl<'c> Translation<'c> {
892888
let (output_name, inner_name) = operand_renames.get(tied_operand).unwrap();
893889

894890
let input_name = self.renamer.borrow_mut().fresh();
895-
let input_local = mk().local(
896-
mk().ident_pat(&input_name),
897-
None as Option<Box<Type>>,
898-
Some(in_expr),
899-
);
891+
let input_local = mk().local(mk().ident_pat(&input_name), None, Some(in_expr));
900892
stmts.push(mk().local_stmt(Box::new(input_local)));
901893

902894
// Replace `in_expr` with

c2rust-transpile/src/translator/atomics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<'c> Translation<'c> {
276276
let res_name = self.renamer.borrow_mut().fresh();
277277
let res_let = mk().local_stmt(Box::new(mk().local(
278278
mk().ident_pat(&res_name),
279-
None as Option<Box<Type>>,
279+
None,
280280
Some(call),
281281
)));
282282
let assignment = mk().semi_stmt(mk().assign_expr(
@@ -410,14 +410,14 @@ impl<'c> Translation<'c> {
410410
let arg0_name = self.renamer.borrow_mut().fresh();
411411
let arg0_let = mk().local_stmt(Box::new(mk().local(
412412
mk().ident_pat(&arg0_name),
413-
None as Option<Box<Type>>,
413+
None,
414414
Some(dst),
415415
)));
416416

417417
let arg1_name = self.renamer.borrow_mut().fresh();
418418
let arg1_let = mk().local_stmt(Box::new(mk().local(
419419
mk().ident_pat(&arg1_name),
420-
None as Option<Box<Type>>,
420+
None,
421421
Some(src),
422422
)));
423423

c2rust-transpile/src/translator/builtins.rs

+17-36
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ impl<'c> Translation<'c> {
7070
let val = self.convert_expr(ctx.used(), args[0])?;
7171

7272
Ok(val.map(|v| {
73-
let val =
74-
mk().method_call_expr(v, "is_sign_negative", vec![] as Vec<Box<Expr>>);
73+
let val = mk().method_call_expr(v, "is_sign_negative", vec![]);
7574

7675
mk().cast_expr(val, mk().path_ty(vec!["libc", "c_int"]))
7776
}))
@@ -84,11 +83,7 @@ impl<'c> Translation<'c> {
8483
let zero = mk().lit_expr(mk().int_lit(0, ""));
8584
let one = mk().lit_expr(mk().int_lit(1, ""));
8685
let cmp = BinOp::Eq(Default::default());
87-
let zeros = mk().method_call_expr(
88-
x.clone(),
89-
"trailing_zeros",
90-
vec![] as Vec<Box<Expr>>,
91-
);
86+
let zeros = mk().method_call_expr(x.clone(), "trailing_zeros", vec![]);
9287
let zeros_cast = mk().cast_expr(zeros, mk().path_ty(vec!["i32"]));
9388
let zeros_plus1 = mk().binary_expr(add, zeros_cast, one);
9489
let block = mk().block(vec![mk().expr_stmt(zero.clone())]);
@@ -100,25 +95,24 @@ impl<'c> Translation<'c> {
10095
"__builtin_clz" | "__builtin_clzl" | "__builtin_clzll" => {
10196
let val = self.convert_expr(ctx.used(), args[0])?;
10297
Ok(val.map(|x| {
103-
let zeros = mk().method_call_expr(x, "leading_zeros", vec![] as Vec<Box<Expr>>);
98+
let zeros = mk().method_call_expr(x, "leading_zeros", vec![]);
10499
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
105100
}))
106101
}
107102
"__builtin_ctz" | "__builtin_ctzl" | "__builtin_ctzll" => {
108103
let val = self.convert_expr(ctx.used(), args[0])?;
109104
Ok(val.map(|x| {
110-
let zeros =
111-
mk().method_call_expr(x, "trailing_zeros", vec![] as Vec<Box<Expr>>);
105+
let zeros = mk().method_call_expr(x, "trailing_zeros", vec![]);
112106
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
113107
}))
114108
}
115109
"__builtin_bswap16" | "__builtin_bswap32" | "__builtin_bswap64" => {
116110
let val = self.convert_expr(ctx.used(), args[0])?;
117-
Ok(val.map(|x| mk().method_call_expr(x, "swap_bytes", vec![] as Vec<Box<Expr>>)))
111+
Ok(val.map(|x| mk().method_call_expr(x, "swap_bytes", vec![])))
118112
}
119113
"__builtin_fabs" | "__builtin_fabsf" | "__builtin_fabsl" => {
120114
let val = self.convert_expr(ctx.used(), args[0])?;
121-
Ok(val.map(|x| mk().method_call_expr(x, "abs", vec![] as Vec<Box<Expr>>)))
115+
Ok(val.map(|x| mk().method_call_expr(x, "abs", vec![])))
122116
}
123117
"__builtin_isfinite" | "__builtin_isnan" => {
124118
let val = self.convert_expr(ctx.used(), args[0])?;
@@ -129,19 +123,15 @@ impl<'c> Translation<'c> {
129123
_ => panic!(),
130124
};
131125
Ok(val.map(|x| {
132-
let call = mk().method_call_expr(x, seg, vec![] as Vec<Box<Expr>>);
126+
let call = mk().method_call_expr(x, seg, vec![]);
133127
mk().cast_expr(call, mk().path_ty(vec!["i32"]))
134128
}))
135129
}
136130
"__builtin_isinf_sign" => {
137131
// isinf_sign(x) -> fabs(x) == infinity ? (signbit(x) ? -1 : 1) : 0
138132
let val = self.convert_expr(ctx.used(), args[0])?;
139133
Ok(val.map(|x| {
140-
let inner_cond = mk().method_call_expr(
141-
x.clone(),
142-
"is_sign_positive",
143-
vec![] as Vec<Box<Expr>>,
144-
);
134+
let inner_cond = mk().method_call_expr(x.clone(), "is_sign_positive", vec![]);
145135
let one = mk().lit_expr(mk().int_lit(1, ""));
146136
let minus_one = mk().unary_expr(
147137
UnOp::Neg(Default::default()),
@@ -150,8 +140,7 @@ impl<'c> Translation<'c> {
150140
let one_block = mk().block(vec![mk().expr_stmt(one)]);
151141
let inner_ifte = mk().ifte_expr(inner_cond, one_block, Some(minus_one));
152142
let zero = mk().lit_expr(mk().int_lit(0, ""));
153-
let outer_cond =
154-
mk().method_call_expr(x, "is_infinite", vec![] as Vec<Box<Expr>>);
143+
let outer_cond = mk().method_call_expr(x, "is_infinite", vec![]);
155144
let inner_ifte_block = mk().block(vec![mk().expr_stmt(inner_ifte)]);
156145
mk().ifte_expr(outer_cond, inner_ifte_block, Some(zero))
157146
}))
@@ -167,7 +156,7 @@ impl<'c> Translation<'c> {
167156
"__builtin_popcount" | "__builtin_popcountl" | "__builtin_popcountll" => {
168157
let val = self.convert_expr(ctx.used(), args[0])?;
169158
Ok(val.map(|x| {
170-
let zeros = mk().method_call_expr(x, "count_ones", vec![] as Vec<Box<Expr>>);
159+
let zeros = mk().method_call_expr(x, "count_ones", vec![]);
171160
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
172161
}))
173162
}
@@ -272,11 +261,8 @@ impl<'c> Translation<'c> {
272261
let fn_ctx = self.function_context.borrow();
273262
let src = fn_ctx.get_va_list_arg_name();
274263

275-
let call_expr = mk().method_call_expr(
276-
mk().ident_expr(src),
277-
"clone",
278-
vec![] as Vec<Box<Expr>>,
279-
);
264+
let call_expr =
265+
mk().method_call_expr(mk().ident_expr(src), "clone", vec![]);
280266
let assign_expr = mk().assign_expr(dst.to_expr(), call_expr);
281267
let stmt = mk().semi_stmt(assign_expr);
282268

@@ -295,8 +281,7 @@ impl<'c> Translation<'c> {
295281
let dst = self.convert_expr(ctx.expect_valistimpl().used(), args[0])?;
296282
let src = self.convert_expr(ctx.expect_valistimpl().used(), args[1])?;
297283

298-
let call_expr =
299-
mk().method_call_expr(src.to_expr(), "clone", vec![] as Vec<Box<Expr>>);
284+
let call_expr = mk().method_call_expr(src.to_expr(), "clone", vec![]);
300285
let assign_expr = mk().assign_expr(dst.to_expr(), call_expr);
301286
let stmt = mk().semi_stmt(assign_expr);
302287

@@ -326,14 +311,10 @@ impl<'c> Translation<'c> {
326311
Ok(WithStmts::new(
327312
vec![mk().local_stmt(Box::new(mk().local(
328313
mk().mutbl().ident_pat(&alloca_name),
329-
None as Option<Box<Type>>,
314+
None,
330315
Some(vec_expr(zero_elem, cast_int(count, "usize", false))),
331316
)))],
332-
mk().method_call_expr(
333-
mk().ident_expr(&alloca_name),
334-
"as_mut_ptr",
335-
vec![] as Vec<Box<Expr>>,
336-
),
317+
mk().method_call_expr(mk().ident_expr(&alloca_name), "as_mut_ptr", vec![]),
337318
))
338319
})
339320
}
@@ -540,7 +521,7 @@ impl<'c> Translation<'c> {
540521
self.use_feature("core_intrinsics");
541522

542523
let atomic_func = mk().abs_path_expr(vec!["core", "intrinsics", "atomic_fence"]);
543-
let call_expr = mk().call_expr(atomic_func, vec![] as Vec<Box<Expr>>);
524+
let call_expr = mk().call_expr(atomic_func, vec![]);
544525
self.convert_side_effects_expr(
545526
ctx,
546527
WithStmts::new_val(call_expr),
@@ -657,7 +638,7 @@ impl<'c> Translation<'c> {
657638
mk().ident_pat(&sum_name),
658639
mk().ident_pat(over_name.clone()),
659640
]),
660-
None as Option<Box<Type>>,
641+
None,
661642
Some(overflowing),
662643
)));
663644

0 commit comments

Comments
 (0)