Skip to content

Commit 3dbb2cc

Browse files
committed
codegen_llvm_back: improve common patterns
1 parent 4286c3c commit 3dbb2cc

File tree

6 files changed

+30
-57
lines changed

6 files changed

+30
-57
lines changed

src/librustc_codegen_llvm/back/archive.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,8 @@ impl<'a> ArchiveBuilder<'a> {
185185
/// Combine the provided files, rlibs, and native libraries into a single
186186
/// `Archive`.
187187
pub fn build(&mut self) {
188-
let kind = match self.llvm_archive_kind() {
189-
Ok(kind) => kind,
190-
Err(kind) => {
191-
self.config.sess.fatal(&format!("Don't know how to build archive of type: {}",
192-
kind));
193-
}
194-
};
188+
let kind = self.llvm_archive_kind().unwrap_or_else(|kind|
189+
self.config.sess.fatal(&format!("Don't know how to build archive of type: {}", kind)));
195190

196191
if let Err(e) = self.build_with_llvm(kind) {
197192
self.config.sess.fatal(&format!("failed to build archive: {}", e));

src/librustc_codegen_llvm/back/link.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,10 @@ pub fn get_linker(sess: &Session, linker: &Path, flavor: LinkerFlavor) -> (PathB
107107
}
108108

109109
pub fn remove(sess: &Session, path: &Path) {
110-
match fs::remove_file(path) {
111-
Ok(..) => {}
112-
Err(e) => {
113-
sess.err(&format!("failed to remove {}: {}",
114-
path.display(),
115-
e));
116-
}
110+
if let Err(e) = fs::remove_file(path) {
111+
sess.err(&format!("failed to remove {}: {}",
112+
path.display(),
113+
e));
117114
}
118115
}
119116

@@ -184,7 +181,7 @@ fn preserve_objects_for_their_debuginfo(sess: &Session) -> bool {
184181
// the objects as they're losslessly contained inside the archives.
185182
let output_linked = sess.crate_types.borrow()
186183
.iter()
187-
.any(|x| *x != config::CrateType::Rlib && *x != config::CrateType::Staticlib);
184+
.any(|&x| x != config::CrateType::Rlib && x != config::CrateType::Staticlib);
188185
if !output_linked {
189186
return false
190187
}
@@ -289,24 +286,19 @@ fn link_binary_output(sess: &Session,
289286
// final destination, with a `fs::rename` call. In order for the rename to
290287
// always succeed, the temporary file needs to be on the same filesystem,
291288
// which is why we create it inside the output directory specifically.
292-
let metadata_tmpdir = match TempFileBuilder::new()
289+
let metadata_tmpdir = TempFileBuilder::new()
293290
.prefix("rmeta")
294291
.tempdir_in(out_filename.parent().unwrap())
295-
{
296-
Ok(tmpdir) => tmpdir,
297-
Err(err) => sess.fatal(&format!("couldn't create a temp dir: {}", err)),
298-
};
292+
.unwrap_or_else(|err| sess.fatal(&format!("couldn't create a temp dir: {}", err)));
299293
let metadata = emit_metadata(sess, codegen_results, &metadata_tmpdir);
300294
if let Err(e) = fs::rename(metadata, &out_filename) {
301295
sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
302296
}
303297
out_filenames.push(out_filename);
304298
}
305299

306-
let tmpdir = match TempFileBuilder::new().prefix("rustc").tempdir() {
307-
Ok(tmpdir) => tmpdir,
308-
Err(err) => sess.fatal(&format!("couldn't create a temp dir: {}", err)),
309-
};
300+
let tmpdir = TempFileBuilder::new().prefix("rustc").tempdir().unwrap_or_else(|err|
301+
sess.fatal(&format!("couldn't create a temp dir: {}", err)));
310302

311303
if outputs.outputs.should_codegen() {
312304
let out_filename = out_filename(sess, crate_type, outputs, crate_name);
@@ -869,9 +861,8 @@ fn link_natively(sess: &Session,
869861
sess.opts.debuginfo != DebugInfo::None &&
870862
!preserve_objects_for_their_debuginfo(sess)
871863
{
872-
match Command::new("dsymutil").arg(out_filename).output() {
873-
Ok(..) => {}
874-
Err(e) => sess.fatal(&format!("failed to run dsymutil: {}", e)),
864+
if let Err(e) = Command::new("dsymutil").arg(out_filename).output() {
865+
sess.fatal(&format!("failed to run dsymutil: {}", e))
875866
}
876867
}
877868

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -919,12 +919,6 @@ impl ThinLTOImports {
919919
}
920920

921921
fn module_name_to_str(c_str: &CStr) -> &str {
922-
match c_str.to_str() {
923-
Ok(s) => s,
924-
Err(e) => {
925-
bug!("Encountered non-utf8 LLVM module name `{}`: {}",
926-
c_str.to_string_lossy(),
927-
e)
928-
}
929-
}
922+
c_str.to_str().unwrap_or_else(|e|
923+
bug!("Encountered non-utf8 LLVM module name `{}`: {}", c_str.to_string_lossy(), e))
930924
}

src/librustc_codegen_llvm/back/wasm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const WASM_EXTERNAL_KIND_GLOBAL: u8 = 3;
4242
/// https://github.com/llvm-mirror/llvm/commit/0f32e1365, although support still
4343
/// needs to be added, tracked at https://bugs.llvm.org/show_bug.cgi?id=37168
4444
pub fn rewrite_imports(path: &Path, import_map: &FxHashMap<String, String>) {
45-
if import_map.len() == 0 {
45+
if import_map.is_empty() {
4646
return
4747
}
4848

@@ -127,7 +127,7 @@ impl<'a> Iterator for WasmSections<'a> {
127127
type Item = (u8, &'a [u8]);
128128

129129
fn next(&mut self) -> Option<(u8, &'a [u8])> {
130-
if self.0.data.len() == 0 {
130+
if self.0.data.is_empty() {
131131
return None
132132
}
133133

src/librustc_codegen_llvm/back/write.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,15 +1440,12 @@ fn execute_copy_from_cache_work_item(cgcx: &CodegenContext,
14401440
module.name,
14411441
source_file,
14421442
obj_out.display());
1443-
match link_or_copy(&source_file, &obj_out) {
1444-
Ok(_) => { }
1445-
Err(err) => {
1446-
let diag_handler = cgcx.create_diag_handler();
1447-
diag_handler.err(&format!("unable to copy {} to {}: {}",
1448-
source_file.display(),
1449-
obj_out.display(),
1450-
err));
1451-
}
1443+
if let Err(err) = link_or_copy(&source_file, &obj_out) {
1444+
let diag_handler = cgcx.create_diag_handler();
1445+
diag_handler.err(&format!("unable to copy {} to {}: {}",
1446+
source_file.display(),
1447+
obj_out.display(),
1448+
err));
14521449
}
14531450
}
14541451

src/librustc_codegen_utils/linker.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,13 @@ impl<'a> Linker for GccLinker<'a> {
343343
}
344344

345345
fn debuginfo(&mut self) {
346-
match self.sess.opts.debuginfo {
347-
DebugInfo::None => {
348-
// If we are building without debuginfo enabled and we were called with
349-
// `-Zstrip-debuginfo-if-disabled=yes`, tell the linker to strip any debuginfo
350-
// found when linking to get rid of symbols from libstd.
351-
match self.sess.opts.debugging_opts.strip_debuginfo_if_disabled {
352-
Some(true) => { self.linker_arg("-S"); },
353-
_ => {},
354-
}
355-
},
356-
_ => {},
346+
if let DebugInfo::None = self.sess.opts.debuginfo {
347+
// If we are building without debuginfo enabled and we were called with
348+
// `-Zstrip-debuginfo-if-disabled=yes`, tell the linker to strip any debuginfo
349+
// found when linking to get rid of symbols from libstd.
350+
if let Some(true) = self.sess.opts.debugging_opts.strip_debuginfo_if_disabled {
351+
self.linker_arg("-S");
352+
}
357353
};
358354
}
359355

0 commit comments

Comments
 (0)