Skip to content

Commit 8f6e09a

Browse files
committed
Simplify if let/match expressions
1 parent 6226747 commit 8f6e09a

File tree

8 files changed

+211
-319
lines changed

8 files changed

+211
-319
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,10 @@ pub fn load_attrs(cx: &DocContext, tcx: &TyCtxt,
138138
/// These names are used later on by HTML rendering to generate things like
139139
/// source links back to the original item.
140140
pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) {
141-
match cx.tcx_opt() {
142-
Some(tcx) => {
143-
let fqn = tcx.sess.cstore.extern_item_path(did);
144-
let fqn = fqn.into_iter().map(|i| i.to_string()).collect();
145-
cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, kind));
146-
}
147-
None => {}
141+
if let Some(tcx) = cx.tcx_opt() {
142+
let fqn = tcx.sess.cstore.extern_item_path(did);
143+
let fqn = fqn.into_iter().map(|i| i.to_string()).collect();
144+
cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, kind));
148145
}
149146
}
150147

@@ -230,12 +227,9 @@ pub fn build_impls(cx: &DocContext, tcx: &TyCtxt,
230227
tcx.populate_inherent_implementations_for_type_if_necessary(did);
231228
let mut impls = Vec::new();
232229

233-
match tcx.inherent_impls.borrow().get(&did) {
234-
None => {}
235-
Some(i) => {
236-
for &did in i.iter() {
237-
build_impl(cx, tcx, did, &mut impls);
238-
}
230+
if let Some(i) = tcx.inherent_impls.borrow().get(&did) {
231+
for &did in i.iter() {
232+
build_impl(cx, tcx, did, &mut impls);
239233
}
240234
}
241235

@@ -464,9 +458,8 @@ fn build_module(cx: &DocContext, tcx: &TyCtxt,
464458
}
465459
cstore::DlDef(def) if item.vis == hir::Public => {
466460
if !visited.insert(def) { continue }
467-
match try_inline_def(cx, tcx, def) {
468-
Some(i) => items.extend(i),
469-
None => {}
461+
if let Some(i) = try_inline_def(cx, tcx, def) {
462+
items.extend(i)
470463
}
471464
}
472465
cstore::DlDef(..) => {}

src/librustdoc/clean/mod.rs

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ impl<T: Clean<U>, U> Clean<U> for Rc<T> {
100100

101101
impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
102102
fn clean(&self, cx: &DocContext) -> Option<U> {
103-
match self {
104-
&None => None,
105-
&Some(ref v) => Some(v.clean(cx))
106-
}
103+
self.as_ref().map(|v| v.clean(cx))
107104
}
108105
}
109106

@@ -332,27 +329,20 @@ impl Item {
332329
}
333330

334331
pub fn stability_class(&self) -> String {
335-
match self.stability {
336-
Some(ref s) => {
337-
let mut base = match s.level {
338-
stability::Unstable => "unstable".to_string(),
339-
stability::Stable => String::new(),
340-
};
341-
if !s.deprecated_since.is_empty() {
342-
base.push_str(" deprecated");
343-
}
344-
base
332+
self.stability.as_ref().map(|ref s| {
333+
let mut base = match s.level {
334+
stability::Unstable => "unstable".to_string(),
335+
stability::Stable => String::new(),
336+
};
337+
if !s.deprecated_since.is_empty() {
338+
base.push_str(" deprecated");
345339
}
346-
_ => String::new(),
347-
}
340+
base
341+
}).unwrap_or(String::new())
348342
}
349343

350344
pub fn stable_since(&self) -> Option<&str> {
351-
if let Some(ref s) = self.stability {
352-
return Some(&s.since[..]);
353-
}
354-
355-
None
345+
self.stability.as_ref().map(|s| &s.since[..])
356346
}
357347
}
358348

@@ -711,7 +701,7 @@ impl<'tcx> Clean<TyParamBound> for ty::TraitRef<'tcx> {
711701
if let &ty::Region::ReLateBound(_, _) = *reg {
712702
debug!(" hit an ReLateBound {:?}", reg);
713703
if let Some(lt) = reg.clean(cx) {
714-
late_bounds.push(lt)
704+
late_bounds.push(lt);
715705
}
716706
}
717707
}
@@ -780,8 +770,7 @@ impl Clean<Option<Lifetime>> for ty::Region {
780770
fn clean(&self, cx: &DocContext) -> Option<Lifetime> {
781771
match *self {
782772
ty::ReStatic => Some(Lifetime::statik()),
783-
ty::ReLateBound(_, ty::BrNamed(_, name)) =>
784-
Some(Lifetime(name.to_string())),
773+
ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
785774
ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
786775

787776
ty::ReLateBound(..) |
@@ -1151,12 +1140,12 @@ impl<'tcx> Clean<Type> for ty::FnOutput<'tcx> {
11511140
impl<'a, 'tcx> Clean<FnDecl> for (DefId, &'a ty::PolyFnSig<'tcx>) {
11521141
fn clean(&self, cx: &DocContext) -> FnDecl {
11531142
let (did, sig) = *self;
1154-
let mut names = if let Some(_) = cx.map.as_local_node_id(did) {
1143+
let mut names = if cx.map.as_local_node_id(did).is_some() {
11551144
vec![].into_iter()
11561145
} else {
11571146
cx.tcx().sess.cstore.method_arg_names(did).into_iter()
11581147
}.peekable();
1159-
if names.peek().map(|s| &**s) == Some("self") {
1148+
if let Some("self") = names.peek().map(|s| &s[..]) {
11601149
let _ = names.next();
11611150
}
11621151
FnDecl {
@@ -1627,15 +1616,9 @@ impl Clean<Type> for hir::Ty {
16271616
}
16281617
}
16291618
TyBareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
1630-
TyPolyTraitRef(ref bounds) => {
1631-
PolyTraitRef(bounds.clean(cx))
1632-
},
1633-
TyInfer => {
1634-
Infer
1635-
},
1636-
TyTypeof(..) => {
1637-
panic!("Unimplemented type {:?}", self.node)
1638-
},
1619+
TyPolyTraitRef(ref bounds) => PolyTraitRef(bounds.clean(cx)),
1620+
TyInfer => Infer,
1621+
TyTypeof(..) => panic!("Unimplemented type {:?}", self.node),
16391622
}
16401623
}
16411624
}
@@ -2253,7 +2236,7 @@ impl Clean<Vec<Item>> for doctree::Impl {
22532236
polarity: Some(self.polarity.clean(cx)),
22542237
}),
22552238
});
2256-
return ret;
2239+
ret
22572240
}
22582241
}
22592242

@@ -2393,9 +2376,8 @@ impl Clean<Vec<Item>> for doctree::Import {
23932376
}
23942377
hir::ViewPathSimple(name, ref p) => {
23952378
if !denied {
2396-
match inline::try_inline(cx, self.id, Some(name)) {
2397-
Some(items) => return items,
2398-
None => {}
2379+
if let Some(items) = inline::try_inline(cx, self.id, Some(name)) {
2380+
return items;
23992381
}
24002382
}
24012383
(vec![], SimpleImport(name.clean(cx),
@@ -2460,9 +2442,8 @@ impl Clean<Vec<Item>> for hir::ForeignMod {
24602442
fn clean(&self, cx: &DocContext) -> Vec<Item> {
24612443
let mut items = self.items.clean(cx);
24622444
for item in &mut items {
2463-
match item.inner {
2464-
ForeignFunctionItem(ref mut f) => f.abi = self.abi,
2465-
_ => {}
2445+
if let ForeignFunctionItem(ref mut f) = item.inner {
2446+
f.abi = self.abi;
24662447
}
24672448
}
24682449
items
@@ -2598,11 +2579,7 @@ fn resolve_type(cx: &DocContext,
25982579
};
25992580
}
26002581
};
2601-
let def = match tcx.def_map.borrow().get(&id) {
2602-
Some(k) => k.full_def(),
2603-
None => panic!("unresolved id not in defmap")
2604-
};
2605-
2582+
let def = tcx.def_map.borrow().get(&id).expect("unresolved id not in defmap").full_def();
26062583
debug!("resolve_type: def={:?}", def);
26072584

26082585
let is_generic = match def {
@@ -2659,7 +2636,7 @@ fn register_def(cx: &DocContext, def: Def) -> DefId {
26592636
let t = inline::build_external_trait(cx, tcx, did);
26602637
cx.external_traits.borrow_mut().as_mut().unwrap().insert(did, t);
26612638
}
2662-
return did;
2639+
did
26632640
}
26642641

26652642
fn resolve_use_source(cx: &DocContext, path: Path, id: ast::NodeId) -> ImportSource {
@@ -2732,12 +2709,10 @@ impl Clean<Stability> for attr::Stability {
27322709
_=> "".to_string(),
27332710
},
27342711
reason: {
2735-
if let Some(ref depr) = self.rustc_depr {
2736-
depr.reason.to_string()
2737-
} else if let attr::Unstable {reason: Some(ref reason), ..} = self.level {
2738-
reason.to_string()
2739-
} else {
2740-
"".to_string()
2712+
match (&self.rustc_depr, &self.level) {
2713+
(&Some(ref depr), _) => depr.reason.to_string(),
2714+
(&None, &attr::Unstable {reason: Some(ref reason), ..}) => reason.to_string(),
2715+
_ => "".to_string(),
27412716
}
27422717
},
27432718
issue: match self.level {

src/librustdoc/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub trait DocFolder : Sized {
8181
c.module = match replace(&mut c.module, None) {
8282
Some(module) => self.fold_item(module), None => None
8383
};
84+
8485
let external_traits = replace(&mut c.external_traits, HashMap::new());
8586
c.external_traits = external_traits.into_iter().map(|(k, mut v)| {
8687
let items = replace(&mut v.items, Vec::new());

0 commit comments

Comments
 (0)