about summary refs log tree commit diff
path: root/src/libsyntax
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/libsyntax
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/libsyntax')
-rw-r--r--src/libsyntax/parse/parser.rs2
-rw-r--r--src/libsyntax/print/pprust.rs2
-rw-r--r--src/libsyntax/source_map.rs3
-rw-r--r--src/libsyntax/tokenstream.rs2
4 files changed, 5 insertions, 4 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index c7089a295fc..9c47589a0bd 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -3228,7 +3228,7 @@ impl<'a> Parser<'a> {
                         }));
 
                         let expr_str = self.sess.source_map().span_to_snippet(expr.span)
-                                                .unwrap_or(pprust::expr_to_string(&expr));
+                                                .unwrap_or_else(|_| pprust::expr_to_string(&expr));
                         err.span_suggestion_with_applicability(
                             expr.span,
                             &format!("try {} the cast value", op_verb),
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 83a05921510..ce7708cc42e 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -3119,7 +3119,7 @@ impl<'a> State<'a> {
             if cmnt.style != comments::Trailing { return Ok(()) }
             let span_line = cm.lookup_char_pos(span.hi());
             let comment_line = cm.lookup_char_pos(cmnt.pos);
-            let next = next_pos.unwrap_or(cmnt.pos + BytePos(1));
+            let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1));
             if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
                 self.print_comment(cmnt)?;
             }
diff --git a/src/libsyntax/source_map.rs b/src/libsyntax/source_map.rs
index 5054f18b020..17586a442da 100644
--- a/src/libsyntax/source_map.rs
+++ b/src/libsyntax/source_map.rs
@@ -937,7 +937,8 @@ impl SourceMap {
                 } else {
                     format!("{}<", &snippet[..offset])
                 };
-                new_snippet.push_str(&self.span_to_snippet(span).unwrap_or("T".to_string()));
+                new_snippet.push_str(
+                    &self.span_to_snippet(span).unwrap_or_else(|_| "T".to_string()));
                 new_snippet.push('>');
 
                 return Some((sugg_span, new_snippet));
diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs
index 70867f9e42f..29bd63d28c5 100644
--- a/src/libsyntax/tokenstream.rs
+++ b/src/libsyntax/tokenstream.rs
@@ -606,7 +606,7 @@ impl Cursor {
             CursorKind::JointTree(ref tree, _) => tree.clone().joint(),
             CursorKind::Stream(ref cursor) => TokenStream::concat_rc_vec({
                 cursor.stack.get(0).cloned().map(|(stream, _)| stream)
-                    .unwrap_or(cursor.stream.clone())
+                    .unwrap_or_else(|| cursor.stream.clone())
             }),
         }
     }