Skip to content

Commit c7a178e

Browse files
Rollup merge of rust-lang#52658 - Wallacoloo:topics/use-option-methods, r=cramertj
Prefer `Option::map`/etc over `match` wherever it improves clarity This isn't intended to change behavior anywhere. A lot of times statements like `match x { None => None, Some(y) => [...] }` can be rewritten using `Option::map` or `Option::and_then` in a way that preserves or improves clarity, so that's what I've done here. I think it's particularly valuable to keep things in `libcore` and `libstd` pretty/idiomatic since it's not uncommon to follow the `[src]` links when browsing the rust-lang.org docs for std/core. If there's any concern about pushing style-based changes though, I'll happily back out the non-std/core commits here.
2 parents 28f8cb5 + cbe5f1c commit c7a178e

File tree

8 files changed

+30
-51
lines changed

8 files changed

+30
-51
lines changed

src/libcore/str/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,10 @@ impl<'a> Iterator for CharIndices<'a> {
696696
impl<'a> DoubleEndedIterator for CharIndices<'a> {
697697
#[inline]
698698
fn next_back(&mut self) -> Option<(usize, char)> {
699-
match self.iter.next_back() {
700-
None => None,
701-
Some(ch) => {
702-
let index = self.front_offset + self.iter.iter.len();
703-
Some((index, ch))
704-
}
705-
}
699+
self.iter.next_back().map(|ch| {
700+
let index = self.front_offset + self.iter.iter.len();
701+
(index, ch)
702+
})
706703
}
707704
}
708705

src/librustc/traits/on_unimplemented.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,10 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
190190
for command in self.subcommands.iter().chain(Some(self)).rev() {
191191
if let Some(ref condition) = command.condition {
192192
if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| {
193-
options.contains(&(c.name().as_str().to_string(),
194-
match c.value_str().map(|s| s.as_str().to_string()) {
195-
Some(s) => Some(s),
196-
None => None
197-
}))
193+
options.contains(&(
194+
c.name().as_str().to_string(),
195+
c.value_str().map(|s| s.as_str().to_string())
196+
))
198197
}) {
199198
debug!("evaluate: skipping {:?} due to condition", command);
200199
continue

src/librustc/ty/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -2697,15 +2697,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26972697
self.opt_associated_item(def_id)
26982698
};
26992699

2700-
match item {
2701-
Some(trait_item) => {
2702-
match trait_item.container {
2703-
TraitContainer(_) => None,
2704-
ImplContainer(def_id) => Some(def_id),
2705-
}
2700+
item.and_then(|trait_item|
2701+
match trait_item.container {
2702+
TraitContainer(_) => None,
2703+
ImplContainer(def_id) => Some(def_id),
27062704
}
2707-
None => None
2708-
}
2705+
)
27092706
}
27102707

27112708
/// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err`

src/librustc_metadata/locator.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -824,17 +824,14 @@ impl<'a> Context<'a> {
824824
if rlib.is_none() && rmeta.is_none() && dylib.is_none() {
825825
return None;
826826
}
827-
match slot {
828-
Some((_, metadata)) => {
829-
Some(Library {
830-
dylib,
831-
rlib,
832-
rmeta,
833-
metadata,
834-
})
827+
slot.map(|(_, metadata)|
828+
Library {
829+
dylib,
830+
rlib,
831+
rmeta,
832+
metadata,
835833
}
836-
None => None,
837-
}
834+
)
838835
}
839836
}
840837

src/libstd/net/parser.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ impl<'a> Parser<'a> {
5353
F: FnOnce(&mut Parser) -> Option<T>,
5454
{
5555
self.read_atomically(move |p| {
56-
match cb(p) {
57-
Some(x) => if p.is_eof() {Some(x)} else {None},
58-
None => None,
59-
}
56+
cb(p).filter(|_| p.is_eof())
6057
})
6158
}
6259

src/libstd/path.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,7 @@ impl<'a> Iterator for Ancestors<'a> {
10651065

10661066
fn next(&mut self) -> Option<Self::Item> {
10671067
let next = self.next;
1068-
self.next = match next {
1069-
Some(path) => path.parent(),
1070-
None => None,
1071-
};
1068+
self.next = next.and_then(Path::parent);
10721069
next
10731070
}
10741071
}

src/libstd/sys_common/backtrace.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,15 @@ pub fn log_enabled() -> Option<PrintFormat> {
156156
_ => return Some(PrintFormat::Full),
157157
}
158158

159-
let val = match env::var_os("RUST_BACKTRACE") {
160-
Some(x) => if &x == "0" {
159+
let val = env::var_os("RUST_BACKTRACE").and_then(|x|
160+
if &x == "0" {
161161
None
162162
} else if &x == "full" {
163163
Some(PrintFormat::Full)
164164
} else {
165165
Some(PrintFormat::Short)
166-
},
167-
None => None,
168-
};
166+
}
167+
);
169168
ENABLED.store(match val {
170169
Some(v) => v as isize,
171170
None => 1,

src/libsyntax_ext/deriving/generic/ty.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,13 @@ pub fn nil_ty<'r>() -> Ty<'r> {
138138
}
139139

140140
fn mk_lifetime(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
141-
match *lt {
142-
Some(s) => Some(cx.lifetime(span, Ident::from_str(s))),
143-
None => None,
144-
}
141+
lt.map(|s|
142+
cx.lifetime(span, Ident::from_str(s))
143+
)
145144
}
146145

147146
fn mk_lifetimes(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
148-
match *lt {
149-
Some(s) => vec![cx.lifetime(span, Ident::from_str(s))],
150-
None => vec![],
151-
}
147+
mk_lifetime(cx, span, lt).into_iter().collect()
152148
}
153149

154150
impl<'a> Ty<'a> {

0 commit comments

Comments
 (0)