Skip to content

Commit 6145a5e

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

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
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: 16 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,17 @@ fn get_output_path(
667669
input_path
668670
}
669671
}
672+
673+
fn rustfmt(output_path: &Path, build_dir: &Path) {
674+
let edition = "2021";
675+
676+
let status = Command::new("rustfmt")
677+
.args(["--edition", edition])
678+
.arg(output_path)
679+
.current_dir(build_dir)
680+
.status();
681+
682+
if !status.map_or(false, |status| status.success()) {
683+
warn!("rustfmt failed, code may not be well-formatted");
684+
}
685+
}

c2rust-transpile/tests/snapshots.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ fn transpile(platform: Option<&str>, c_path: &Path) {
9090

9191
let edition = "2021";
9292

93-
let status = Command::new("rustfmt")
94-
.args(["--edition", edition])
95-
.arg(&rs_path)
96-
.status();
97-
assert!(status.unwrap().success());
98-
9993
let rs = fs::read_to_string(&rs_path).unwrap();
10094
let debug_expr = format!("cat {}", rs_path.display());
10195

0 commit comments

Comments
 (0)