Repository: clap-rs/clap
Pull request #1509 (2019-06-24 08:13:17)

Minor Refactoring


Patch:
diff --git a/src/output/help.rs b/src/output/help.rs
index 5fd115ff..0a6b533b 100644
--- a/src/output/help.rs
+++ b/src/output/help.rs
@@ -74,10 +74,10 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
             next_line_help: nlh,
             hide_pv,
             term_w,
-            color: color,
+            color,
             longest: 0,
             force_next_line: false,
-            cizer: cizer,
+            cizer,
             use_long,
         }
     }
diff --git a/src/parse/features/suggestions.rs b/src/parse/features/suggestions.rs
index a7b38ce2..73fb0420 100644
--- a/src/parse/features/suggestions.rs
+++ b/src/parse/features/suggestions.rs
@@ -25,10 +25,8 @@ where
             candidate = Some((confidence, pv.as_ref().to_owned()));
         }
     }
-    match candidate {
-        None => None,
-        Some((_, candidate)) => Some(candidate.to_owned()),
-    }
+
+    candidate.map(|(_, candidate)| candidate)
 }
 
 #[cfg(not(feature = "suggestions"))]
diff --git a/src/parse/matches/arg_matches.rs b/src/parse/matches/arg_matches.rs
index 7821bb60..26c1c237 100644
--- a/src/parse/matches/arg_matches.rs
+++ b/src/parse/matches/arg_matches.rs
@@ -213,14 +213,14 @@ impl ArgMatches {
     /// [`Values`]: ./struct.Values.html
     /// [`Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
     pub fn values_of<T: Key>(&self, id: T) -> Option<Values<'_>> {
-        if let Some(arg) = self.args.get(&id.key()) {
+        self.args.get(&id.key()).map(|arg| {
             fn to_str_slice(o: &OsString) -> &str { o.to_str().expect(INVALID_UTF8) }
             let to_str_slice: fn(&OsString) -> &str = to_str_slice; // coerce to fn pointer
-            return Some(Values {
+
+            Values {
                 iter: arg.vals.iter().map(to_str_slice),
-            });
-        }
-        None
+            }
+        })
     }
 
     /// Gets the lossy values of a specific argument. If the option wasn't present at runtime
@@ -248,15 +248,12 @@ impl ArgMatches {
     /// assert_eq!(itr.next(), None);
     /// ```
     pub fn values_of_lossy<T: Key>(&self, id: T) -> Option<Vec<String>> {
-        if let Some(arg) = self.args.get(&id.key()) {
-            return Some(
-                arg.vals
-                    .iter()
-                    .map(|v| v.to_string_lossy().into_owned())
-                    .collect(),
-            );
-        }
-        None
+        self.args.get(&id.key()).map(|arg| {
+            arg.vals
+                .iter()
+                .map(|v| v.to_string_lossy().into_owned())
+                .collect()
+        })
     }
 
     /// Gets a [`OsValues`] struct which is implements [`Iterator`] for [`OsString`] values of a
@@ -293,12 +290,10 @@ impl ArgMatches {
     pub fn values_of_os<'a, T: Key>(&'a self, id: T) -> Option<OsValues<'a>> {
         fn to_str_slice(o: &OsString) -> &OsStr { &*o }
         let to_str_slice: fn(&'a OsString) -> &'a OsStr = to_str_slice; // coerce to fn pointer
-        if let Some(arg) = self.args.get(&id.key()) {
-            return Some(OsValues {
-                iter: arg.vals.iter().map(to_str_slice),
-            });
-        }
-        None
+
+        self.args.get(&id.key()).map(|arg| OsValues {
+            iter: arg.vals.iter().map(to_str_slice),
+        })
     }
 
     /// Returns `true` if an argument was present at runtime, otherwise `false`.
@@ -584,12 +579,9 @@ impl ArgMatches {
     /// [`ArgMatches::index_of`]: ./struct.ArgMatches.html#method.index_of
     /// [delimiter]: ./struct.Arg.html#method.value_delimiter
     pub fn indices_of<T: Key>(&self, id: T) -> Option<Indices<'_>> {
-        if let Some(arg) = self.args.get(&id.key()) {
-            return Some(Indices {
-                iter: arg.indices.iter().cloned(),
-            });
-        }
-        None
+        self.args.get(&id.key()).map(|arg| Indices {
+            iter: arg.indices.iter().cloned(),
+        })
     }
 
     /// Because [`Subcommand`]s are essentially "sub-[`App`]s" they have their own [`ArgMatches`]
diff --git a/src/util/strext.rs b/src/util/strext.rs
index 6f81367a..5886d6cb 100644
--- a/src/util/strext.rs
+++ b/src/util/strext.rs
@@ -8,9 +8,9 @@ impl _StrExt for str {
         if index == self.len() {
             return true;
         }
-        match self.as_bytes().get(index) {
-            None => false,
-            Some(&b) => b < 128 || b >= 192,
-        }
+
+        self.as_bytes()
+            .get(index)
+            .map_or(false, |&b| b < 128 || b >= 192)
     }
 }


Test patch:
diff --git a/tests/propagate_globals.rs b/tests/propagate_globals.rs
index 961f1f5d..cdd23241 100644
--- a/tests/propagate_globals.rs
+++ b/tests/propagate_globals.rs
@@ -30,12 +30,12 @@ mod tests {
         app.get_matches_from(argv.split(" ").collect::<Vec<_>>())
     }
 
-    fn get_outer_matches<'a>(m: &'a ArgMatches) -> &'a ArgMatches {
+    fn get_outer_matches(m: &ArgMatches) -> &ArgMatches {
         m.subcommand_matches("outer")
             .expect("could not access outer subcommand")
     }
 
-    fn get_inner_matches<'a>(m: &'a ArgMatches) -> &'a ArgMatches {
+    fn get_inner_matches(m: &ArgMatches) -> &ArgMatches {
         get_outer_matches(m)
             .subcommand_matches("inner")
             .expect("could not access inner subcommand")