Skip to content

Commit f2778f6

Browse files
committed
translator: minor cleanup
* remove redundant clones; * simplify pattern (remove redundant `ref` and `&` patterns); * convert `opt.map(f).unwrap_or(v)` into `opt.map_or(v, f)`; * remove explicit `into_iter` and `iter` calls where redundant; * eliminate gouble-step initialization of mutable variable via combinators;
1 parent a5d828b commit f2778f6

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

c2rust-transpile/src/translator/assembly.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,7 @@ impl<'c> Translation<'c> {
686686
let mut tokens: Vec<TokenTree> = vec![];
687687

688688
let mut tied_operands = HashMap::new();
689-
for (
690-
input_idx,
691-
&AsmOperand {
692-
ref constraints, ..
693-
},
694-
) in inputs.iter().enumerate()
695-
{
689+
for (input_idx, AsmOperand { constraints, .. }) in inputs.iter().enumerate() {
696690
let constraints_digits = constraints.trim_matches(|c: char| !c.is_ascii_digit());
697691
if let Ok(output_idx) = constraints_digits.parse::<usize>() {
698692
let output_key = (output_idx, true);
@@ -721,10 +715,9 @@ impl<'c> Translation<'c> {
721715
if let Ok(idx) = ref_str.parse::<usize>() {
722716
outputs
723717
.iter()
724-
.chain(inputs.iter())
718+
.chain(inputs)
725719
.nth(idx)
726-
.map(operand_is_mem_only)
727-
.unwrap_or(false)
720+
.map_or(false, operand_is_mem_only)
728721
} else {
729722
false
730723
}
@@ -757,13 +750,12 @@ impl<'c> Translation<'c> {
757750
Ok((mut dir_spec, mem_only, parsed)) => {
758751
// Add to args list; if a matching in_expr is found, this is
759752
// an inout and we remove the output from the outputs list
760-
let mut in_expr = inputs_by_register.remove(&parsed);
761-
if in_expr.is_none() {
753+
let in_expr = inputs_by_register
754+
.remove(&parsed)
762755
// Also check for by-index references to this output
763-
in_expr = inputs_by_register.remove(&i.to_string());
764-
}
765-
// Extract expression
766-
let in_expr = in_expr.map(|(i, operand)| (i, operand.expression));
756+
.or_else(|| inputs_by_register.remove(&i.to_string()))
757+
// Extract expression
758+
.map(|(i, operand)| (i, operand.expression));
767759

768760
// For inouts, change the dirspec to include 'in'
769761
if in_expr.is_some() {
@@ -1026,7 +1018,7 @@ impl<'c> Translation<'c> {
10261018
stmts.push(mac);
10271019

10281020
// Push the post-macro statements
1029-
stmts.extend(post_stmts.into_iter());
1021+
stmts.extend(post_stmts);
10301022

10311023
Ok(stmts)
10321024
}

c2rust-transpile/src/translator/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,10 @@ fn clean_path(mod_names: &RefCell<IndexMap<String, PathBuf>>, path: Option<&path
456456
let mut file_path: String = path.map_or("internal".to_string(), path_to_str);
457457
let path = path.unwrap_or_else(|| path::Path::new(""));
458458
let mut mod_names = mod_names.borrow_mut();
459-
if !mod_names.contains_key(&file_path.clone()) {
459+
if !mod_names.contains_key(&file_path) {
460460
mod_names.insert(file_path.clone(), path.to_path_buf());
461461
} else {
462-
let mod_path = mod_names.get(&file_path.clone()).unwrap();
462+
let mod_path = mod_names.get(&file_path).unwrap();
463463
// A collision in the module names has occured.
464464
// Ex: types.h can be included from
465465
// /usr/include/bits and /usr/include/sys

0 commit comments

Comments
 (0)