From 4f3ab4986ec96d9c93f34dc53d0a4a1279288451 Mon Sep 17 00:00:00 2001
From: Colin Wallace <colin@mooooo.ooo>
Date: Mon, 23 Jul 2018 22:00:51 -0700
Subject: [PATCH 1/4] libstd: Prefer `Option::map`/etc over `match` where
 applicable

---
 src/libstd/net/parser.rs           | 5 +----
 src/libstd/path.rs                 | 5 +----
 src/libstd/sys_common/backtrace.rs | 9 ++++-----
 3 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/src/libstd/net/parser.rs b/src/libstd/net/parser.rs
index ae5037cc44e80..008c5da171ffc 100644
--- a/src/libstd/net/parser.rs
+++ b/src/libstd/net/parser.rs
@@ -53,10 +53,7 @@ impl<'a> Parser<'a> {
         F: FnOnce(&mut Parser) -> Option<T>,
     {
         self.read_atomically(move |p| {
-            match cb(p) {
-                Some(x) => if p.is_eof() {Some(x)} else {None},
-                None => None,
-            }
+            cb(p).filter(|_| p.is_eof())
         })
     }
 
diff --git a/src/libstd/path.rs b/src/libstd/path.rs
index 0c60129494d5c..688a7e99f10ed 100644
--- a/src/libstd/path.rs
+++ b/src/libstd/path.rs
@@ -1065,10 +1065,7 @@ impl<'a> Iterator for Ancestors<'a> {
 
     fn next(&mut self) -> Option<Self::Item> {
         let next = self.next;
-        self.next = match next {
-            Some(path) => path.parent(),
-            None => None,
-        };
+        self.next = next.and_then(Path::parent);
         next
     }
 }
diff --git a/src/libstd/sys_common/backtrace.rs b/src/libstd/sys_common/backtrace.rs
index 61d7ed463dd38..2db47bd59475e 100644
--- a/src/libstd/sys_common/backtrace.rs
+++ b/src/libstd/sys_common/backtrace.rs
@@ -156,16 +156,15 @@ pub fn log_enabled() -> Option<PrintFormat> {
         _ => return Some(PrintFormat::Full),
     }
 
-    let val = match env::var_os("RUST_BACKTRACE") {
-        Some(x) => if &x == "0" {
+    let val = env::var_os("RUST_BACKTRACE").and_then(|x|
+        if &x == "0" {
             None
         } else if &x == "full" {
             Some(PrintFormat::Full)
         } else {
             Some(PrintFormat::Short)
-        },
-        None => None,
-    };
+        }
+    );
     ENABLED.store(match val {
         Some(v) => v as isize,
         None => 1,

From 727bd7de7e6332482ee2765d46bfd00d89386d4b Mon Sep 17 00:00:00 2001
From: Colin Wallace <colin@mooooo.ooo>
Date: Mon, 23 Jul 2018 22:04:33 -0700
Subject: [PATCH 2/4] libcore: Prefer `Option::map` over `match` where
 applicable

---
 src/libcore/str/mod.rs | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index 3e215de58dd21..86b8349fa3c89 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -696,13 +696,10 @@ impl<'a> Iterator for CharIndices<'a> {
 impl<'a> DoubleEndedIterator for CharIndices<'a> {
     #[inline]
     fn next_back(&mut self) -> Option<(usize, char)> {
-        match self.iter.next_back() {
-            None => None,
-            Some(ch) => {
-                let index = self.front_offset + self.iter.iter.len();
-                Some((index, ch))
-            }
-        }
+        self.iter.next_back().map(|ch| {
+            let index = self.front_offset + self.iter.iter.len();
+            (index, ch)
+        })
     }
 }
 

From 10d82137b3fe45a3b0f0c1bd9080ee46b5259ac1 Mon Sep 17 00:00:00 2001
From: Colin Wallace <colin@mooooo.ooo>
Date: Mon, 23 Jul 2018 22:05:45 -0700
Subject: [PATCH 3/4] librustc: Prefer `Option::map`/etc over `match` where
 applicable

---
 src/librustc/traits/on_unimplemented.rs |  9 ++++-----
 src/librustc/ty/mod.rs                  | 13 +++++--------
 src/librustc_metadata/locator.rs        | 17 +++++++----------
 3 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/src/librustc/traits/on_unimplemented.rs b/src/librustc/traits/on_unimplemented.rs
index 925d3504f75fc..e1395c3fa4427 100644
--- a/src/librustc/traits/on_unimplemented.rs
+++ b/src/librustc/traits/on_unimplemented.rs
@@ -190,11 +190,10 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
         for command in self.subcommands.iter().chain(Some(self)).rev() {
             if let Some(ref condition) = command.condition {
                 if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| {
-                    options.contains(&(c.name().as_str().to_string(),
-                                      match c.value_str().map(|s| s.as_str().to_string()) {
-                                          Some(s) => Some(s),
-                                          None => None
-                                      }))
+                    options.contains(&(
+                        c.name().as_str().to_string(),
+                        c.value_str().map(|s| s.as_str().to_string())
+                    ))
                 }) {
                     debug!("evaluate: skipping {:?} due to condition", command);
                     continue
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index bd24b93f0293f..c2795bae01029 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -2697,15 +2697,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
             self.opt_associated_item(def_id)
         };
 
-        match item {
-            Some(trait_item) => {
-                match trait_item.container {
-                    TraitContainer(_) => None,
-                    ImplContainer(def_id) => Some(def_id),
-                }
+        item.and_then(|trait_item|
+            match trait_item.container {
+                TraitContainer(_) => None,
+                ImplContainer(def_id) => Some(def_id),
             }
-            None => None
-        }
+        )
     }
 
     /// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err`
diff --git a/src/librustc_metadata/locator.rs b/src/librustc_metadata/locator.rs
index b747624338c83..42af5db82942a 100644
--- a/src/librustc_metadata/locator.rs
+++ b/src/librustc_metadata/locator.rs
@@ -824,17 +824,14 @@ impl<'a> Context<'a> {
         if rlib.is_none() && rmeta.is_none() && dylib.is_none() {
             return None;
         }
-        match slot {
-            Some((_, metadata)) => {
-                Some(Library {
-                    dylib,
-                    rlib,
-                    rmeta,
-                    metadata,
-                })
+        slot.map(|(_, metadata)|
+            Library {
+                dylib,
+                rlib,
+                rmeta,
+                metadata,
             }
-            None => None,
-        }
+        )
     }
 }
 

From cbe5f1c4207673b9059e832ef2f134b4f87b380d Mon Sep 17 00:00:00 2001
From: Colin Wallace <colin@mooooo.ooo>
Date: Mon, 23 Jul 2018 22:06:45 -0700
Subject: [PATCH 4/4] libsyntax_ext: Prefer `Option::map` over `match` where
 applicable

---
 src/libsyntax_ext/deriving/generic/ty.rs | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/src/libsyntax_ext/deriving/generic/ty.rs b/src/libsyntax_ext/deriving/generic/ty.rs
index dcccb187bef4b..a0845e0982d2a 100644
--- a/src/libsyntax_ext/deriving/generic/ty.rs
+++ b/src/libsyntax_ext/deriving/generic/ty.rs
@@ -138,17 +138,13 @@ pub fn nil_ty<'r>() -> Ty<'r> {
 }
 
 fn mk_lifetime(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
-    match *lt {
-        Some(s) => Some(cx.lifetime(span, Ident::from_str(s))),
-        None => None,
-    }
+    lt.map(|s|
+        cx.lifetime(span, Ident::from_str(s))
+    )
 }
 
 fn mk_lifetimes(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
-    match *lt {
-        Some(s) => vec![cx.lifetime(span, Ident::from_str(s))],
-        None => vec![],
-    }
+    mk_lifetime(cx, span, lt).into_iter().collect()
 }
 
 impl<'a> Ty<'a> {