Skip to content

Commit d599f5b

Browse files
committed
rustc/driver: improve common patterns
1 parent 2c482d8 commit d599f5b

File tree

4 files changed

+33
-58
lines changed

4 files changed

+33
-58
lines changed

src/librustc_driver/driver.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,15 +1484,12 @@ fn write_out_deps(sess: &Session, outputs: &OutputFilenames, out_filenames: &[Pa
14841484
Ok(())
14851485
})();
14861486

1487-
match result {
1488-
Ok(()) => {}
1489-
Err(e) => {
1490-
sess.fatal(&format!(
1491-
"error writing dependencies to `{}`: {}",
1492-
deps_filename.display(),
1493-
e
1494-
));
1495-
}
1487+
if let Err(e) = result {
1488+
sess.fatal(&format!(
1489+
"error writing dependencies to `{}`: {}",
1490+
deps_filename.display(),
1491+
e
1492+
));
14961493
}
14971494
}
14981495

@@ -1650,10 +1647,7 @@ pub fn build_output_filenames(
16501647
// "-" as input file will cause the parser to read from stdin so we
16511648
// have to make up a name
16521649
// We want to toss everything after the final '.'
1653-
let dirpath = match *odir {
1654-
Some(ref d) => d.clone(),
1655-
None => PathBuf::new(),
1656-
};
1650+
let dirpath = (*odir).as_ref().cloned().unwrap_or_else(|| PathBuf::new());
16571651

16581652
// If a crate name is present, we use it as the link name
16591653
let stem = sess.opts

src/librustc_driver/lib.rs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,10 @@ fn load_backend_from_dylib(path: &Path) -> fn() -> Box<dyn CodegenBackend> {
224224
// available for future dynamic libraries opened. This is currently used by
225225
// loading LLVM and then making its symbols available for other dynamic
226226
// libraries.
227-
let lib = match DynamicLibrary::open_global_now(path) {
228-
Ok(lib) => lib,
229-
Err(err) => {
230-
let err = format!("couldn't load codegen backend {:?}: {:?}",
231-
path,
232-
err);
233-
early_error(ErrorOutputType::default(), &err);
234-
}
235-
};
227+
let lib = DynamicLibrary::open_global_now(path).unwrap_or_else(|err| {
228+
let err = format!("couldn't load codegen backend {:?}: {:?}", path, err);
229+
early_error(ErrorOutputType::default(), &err);
230+
});
236231
unsafe {
237232
match lib.symbol("__rustc_codegen_backend") {
238233
Ok(f) => {
@@ -337,28 +332,22 @@ fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend> {
337332
f.exists()
338333
})
339334
.next();
340-
let sysroot = match sysroot {
341-
Some(path) => path,
342-
None => {
343-
let candidates = sysroot_candidates.iter()
344-
.map(|p| p.display().to_string())
345-
.collect::<Vec<_>>()
346-
.join("\n* ");
347-
let err = format!("failed to find a `codegen-backends` folder \
348-
in the sysroot candidates:\n* {}", candidates);
349-
early_error(ErrorOutputType::default(), &err);
350-
}
351-
};
335+
let sysroot = sysroot.unwrap_or_else(|| {
336+
let candidates = sysroot_candidates.iter()
337+
.map(|p| p.display().to_string())
338+
.collect::<Vec<_>>()
339+
.join("\n* ");
340+
let err = format!("failed to find a `codegen-backends` folder \
341+
in the sysroot candidates:\n* {}", candidates);
342+
early_error(ErrorOutputType::default(), &err);
343+
});
352344
info!("probing {} for a codegen backend", sysroot.display());
353345

354-
let d = match sysroot.read_dir() {
355-
Ok(d) => d,
356-
Err(e) => {
357-
let err = format!("failed to load default codegen backend, couldn't \
358-
read `{}`: {}", sysroot.display(), e);
359-
early_error(ErrorOutputType::default(), &err);
360-
}
361-
};
346+
let d = sysroot.read_dir().unwrap_or_else(|e| {
347+
let err = format!("failed to load default codegen backend, couldn't \
348+
read `{}`: {}", sysroot.display(), e);
349+
early_error(ErrorOutputType::default(), &err);
350+
});
362351

363352
let mut file: Option<PathBuf> = None;
364353

@@ -1055,10 +1044,8 @@ impl RustcDefaultCalls {
10551044
Sysroot => println!("{}", sess.sysroot().display()),
10561045
TargetSpec => println!("{}", sess.target.target.to_json().pretty()),
10571046
FileNames | CrateName => {
1058-
let input = match input {
1059-
Some(input) => input,
1060-
None => early_error(ErrorOutputType::default(), "no input file provided"),
1061-
};
1047+
let input = input.unwrap_or_else(||
1048+
early_error(ErrorOutputType::default(), "no input file provided"));
10621049
let attrs = attrs.as_ref().unwrap();
10631050
let t_outputs = driver::build_output_filenames(input, odir, ofile, attrs, sess);
10641051
let id = rustc_codegen_utils::link::find_crate_name(Some(sess), attrs, input);
@@ -1406,10 +1393,8 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
14061393
for option in config::rustc_optgroups() {
14071394
(option.apply)(&mut options);
14081395
}
1409-
let matches = match options.parse(args) {
1410-
Ok(m) => m,
1411-
Err(f) => early_error(ErrorOutputType::default(), &f.to_string()),
1412-
};
1396+
let matches = options.parse(args).unwrap_or_else(|f|
1397+
early_error(ErrorOutputType::default(), &f.to_string()));
14131398

14141399
// For all options we just parsed, we check a few aspects:
14151400
//
@@ -1631,7 +1616,7 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
16311616
}
16321617
}
16331618

1634-
if result.len() > 0 {
1619+
if !result.is_empty() {
16351620
Some((result, excluded_cargo_defaults))
16361621
} else {
16371622
None

src/librustc_driver/profile/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
9090
match msg {
9191
ProfileQueriesMsg::Halt => return,
9292
ProfileQueriesMsg::Dump(params) => {
93-
assert!(stack.len() == 0);
93+
assert!(stack.is_empty());
9494
assert!(frame.parse_st == ParseState::Clear);
9595
{
9696
// write log of all messages
@@ -141,7 +141,7 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
141141
ProfileQueriesMsg::QueryBegin(span,querymsg)) => {
142142
let start = Instant::now();
143143
frame.parse_st = ParseState::HaveQuery
144-
(Query{span:span, msg:querymsg}, start)
144+
(Query { span, msg: querymsg }, start)
145145
},
146146
(ParseState::Clear,
147147
ProfileQueriesMsg::CacheHit) => {

src/librustc_driver/profile/trace.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ fn html_of_fraction(frac: f64) -> (String, &'static str) {
108108
}
109109

110110
fn total_duration(traces: &[Rec]) -> Duration {
111-
let mut sum : Duration = Duration::new(0,0);
112-
for t in traces.iter() {
113-
sum += t.dur_total;
114-
}
115-
return sum
111+
Duration::new(0, 0) + traces.iter().map(|t| t.dur_total).sum()
116112
}
117113

118114
fn duration_div(nom: Duration, den: Duration) -> f64 {

0 commit comments

Comments
 (0)