about summary refs log tree commit diff
path: root/src/librustc_save_analysis
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-10-20 11:22:48 +0000
committerbors <bors@rust-lang.org>2018-10-20 11:22:48 +0000
commitca2639e82ec4a18d7359efbfb555ea69dd644c97 (patch)
tree3dfa59589214b0157714150a785dd9cc4ccdc240 /src/librustc_save_analysis
parent94273f4d8e463cac45486328294bb1c2bbc10170 (diff)
parentd28aed6dc45ffccc790469cb04f3f775ddb2283a (diff)
downloadrust-ca2639e82ec4a18d7359efbfb555ea69dd644c97.tar.gz
rust-ca2639e82ec4a18d7359efbfb555ea69dd644c97.zip
Auto merge of #55014 - ljedrz:lazyboye_unwraps, r=matthewjasper
Prefer unwrap_or_else to unwrap_or in case of function calls/allocations

The contents of `unwrap_or` are evaluated eagerly, so it's not a good pick in case of function calls and allocations. This PR also changes a few `unwrap_or`s with `unwrap_or_default`.

An added bonus is that in some cases this change also reveals if the object it's called on is an `Option` or a `Result` (based on whether the closure takes an argument).
Diffstat (limited to 'src/librustc_save_analysis')
-rw-r--r--src/librustc_save_analysis/dump_visitor.rs8
-rw-r--r--src/librustc_save_analysis/lib.rs4
2 files changed, 6 insertions, 6 deletions
diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs
index d77a8bd2a52..97bdb9e5fa3 100644
--- a/src/librustc_save_analysis/dump_visitor.rs
+++ b/src/librustc_save_analysis/dump_visitor.rs
@@ -163,7 +163,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
                     .to_fingerprint()
                     .as_value(),
             },
-            crate_root: crate_root.unwrap_or("<no source>".to_owned()),
+            crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
             external_crates: self.save_ctxt.get_external_crates(),
             span: self.span_from_span(krate.span),
         };
@@ -650,7 +650,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
                         .iter()
                         .enumerate()
                         .map(|(i, f)| {
-                            f.ident.map(|i| i.to_string()).unwrap_or(i.to_string())
+                            f.ident.map(|i| i.to_string()).unwrap_or_else(|| i.to_string())
                         })
                         .collect::<Vec<_>>()
                         .join(", ");
@@ -1030,7 +1030,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
                         .tables
                         .node_id_to_type_opt(hir_id)
                         .map(|t| t.to_string())
-                        .unwrap_or(String::new());
+                        .unwrap_or_default();
                     value.push_str(": ");
                     value.push_str(&typ);
 
@@ -1737,7 +1737,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
         let value = l.init
             .as_ref()
             .map(|i| self.span.snippet(i.span))
-            .unwrap_or(String::new());
+            .unwrap_or_default();
         self.process_var_decl(&l.pat, value);
 
         // Just walk the initialiser and type (don't want to walk the pattern again).
diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs
index 1148daef8e3..4b43a1a6270 100644
--- a/src/librustc_save_analysis/lib.rs
+++ b/src/librustc_save_analysis/lib.rs
@@ -367,7 +367,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
                                 .as_ref()
                                 .and_then(|t| self.lookup_ref_id(t.ref_id))
                                 .map(id_from_def_id)
-                                .unwrap_or(null_id()),
+                                .unwrap_or_else(|| null_id()),
                         },
                         Impl {
                             id: impl_id,
@@ -632,7 +632,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
                     ref_id: def_id
                         .or(decl_id)
                         .map(|id| id_from_def_id(id))
-                        .unwrap_or(null_id()),
+                        .unwrap_or_else(|| null_id()),
                 }))
             }
             ast::ExprKind::Path(_, ref path) => {