Skip to content

Commit 7575f2f

Browse files
authored
Fix gcc-shim, apply clippy warning & optimizations to it (#777)
1 parent f507481 commit 7575f2f

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

src/bin/gcc-shim.rs

+45-23
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,69 @@
22

33
use std::env;
44
use std::fs::File;
5-
use std::io::prelude::*;
5+
use std::io::{self, prelude::*};
66
use std::path::PathBuf;
77

88
fn main() {
99
let mut args = env::args();
1010
let program = args.next().expect("Unexpected empty args");
1111

1212
let out_dir = PathBuf::from(
13-
env::var_os("GCCTEST_OUT_DIR").expect(&format!("{}: GCCTEST_OUT_DIR not found", program)),
13+
env::var_os("GCCTEST_OUT_DIR")
14+
.unwrap_or_else(|| panic!("{}: GCCTEST_OUT_DIR not found", program)),
1415
);
1516

1617
// Find the first nonexistent candidate file to which the program's args can be written.
17-
for i in 0.. {
18-
let candidate = &out_dir.join(format!("out{}", i));
18+
let candidate = (0..).find_map(|i| {
19+
let candidate = out_dir.join(format!("out{}", i));
1920

20-
// If the file exists, commands have already run. Try again.
2121
if candidate.exists() {
22-
continue;
22+
// If the file exists, commands have already run. Try again.
23+
None
24+
} else {
25+
Some(candidate)
2326
}
27+
}).unwrap_or_else(|| panic!("Cannot find the first nonexistent candidate file to which the program's args can be written under out_dir '{}'", out_dir.display()));
2428

25-
// Create a file and record the args passed to the command.
26-
let mut f = File::create(candidate).expect(&format!(
27-
"{}: can't create candidate: {}",
29+
// Create a file and record the args passed to the command.
30+
let f = File::create(&candidate).unwrap_or_else(|e| {
31+
panic!(
32+
"{}: can't create candidate: {}, error: {}",
2833
program,
29-
candidate.to_string_lossy()
30-
));
34+
candidate.display(),
35+
e
36+
)
37+
});
38+
let mut f = io::BufWriter::new(f);
39+
40+
(|| {
3141
for arg in args {
32-
writeln!(f, "{}", arg).expect(&format!(
33-
"{}: can't write to candidate: {}",
34-
program,
35-
candidate.to_string_lossy()
36-
));
42+
writeln!(f, "{}", arg)?;
3743
}
38-
break;
39-
}
44+
45+
f.flush()?;
46+
47+
let mut f = f.into_inner()?;
48+
f.flush()?;
49+
f.sync_all()
50+
})()
51+
.unwrap_or_else(|e| {
52+
panic!(
53+
"{}: can't write to candidate: {}, error: {}",
54+
program,
55+
candidate.display(),
56+
e
57+
)
58+
});
4059

4160
// Create a file used by some tests.
4261
let path = &out_dir.join("libfoo.a");
43-
File::create(path).expect(&format!(
44-
"{}: can't create libfoo.a: {}",
45-
program,
46-
path.to_string_lossy()
47-
));
62+
File::create(path).unwrap_or_else(|e| {
63+
panic!(
64+
"{}: can't create libfoo.a: {}, error: {}",
65+
program,
66+
path.display(),
67+
e
68+
)
69+
});
4870
}

0 commit comments

Comments
 (0)