Skip to content

Commit f519f02

Browse files
committed
transpile: Run rustfmt on generated .rs files
1 parent 4e79794 commit f519f02

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

c2rust-transpile/src/build_files/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use serde_json::json;
1111

1212
use super::compile_cmds::LinkCmd;
1313
use super::TranspilerConfig;
14-
use crate::get_module_name;
1514
use crate::CrateSet;
1615
use crate::ExternCrateDetails;
1716
use crate::PragmaSet;
17+
use crate::{get_module_name, rustfmt};
1818

1919
#[derive(Debug, Copy, Clone)]
2020
pub enum BuildDirectoryContents {
@@ -225,7 +225,10 @@ fn emit_build_rs(
225225
});
226226
let output = reg.render("build.rs", &json).unwrap();
227227
let output_path = build_dir.join("build.rs");
228-
maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)
228+
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
229+
rustfmt(&output_path, build_dir);
230+
231+
Some(path)
229232
}
230233

231234
/// Emit lib.rs (main.rs) for a library (binary). Returns `Some(path)`
@@ -252,8 +255,10 @@ fn emit_lib_rs(
252255

253256
let output_path = build_dir.join(file_name);
254257
let output = reg.render("lib.rs", &json).unwrap();
258+
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
259+
rustfmt(&output_path, build_dir);
255260

256-
maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)
261+
Some(path)
257262
}
258263

259264
/// If we translate variadic functions, the output will only compile

c2rust-transpile/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ fn transpile_single(
620620
),
621621
};
622622

623+
rustfmt(&output_path, build_dir);
624+
623625
Ok((output_path, pragmas, crates))
624626
}
625627

@@ -667,3 +669,15 @@ fn get_output_path(
667669
input_path
668670
}
669671
}
672+
673+
fn rustfmt(output_path: &Path, build_dir: &Path) {
674+
let status = Command::new("rustfmt")
675+
.args(["--edition", "2021"])
676+
.arg(output_path)
677+
.current_dir(build_dir)
678+
.status();
679+
680+
if !status.map_or(false, |status| status.success()) {
681+
warn!("rustfmt failed, code may not be well-formatted");
682+
}
683+
}

0 commit comments

Comments
 (0)