Skip to content

Commit 3024c14

Browse files
committed
Use Try syntax for Option in place of macros or match
1 parent c7b6d82 commit 3024c14

File tree

28 files changed

+92
-230
lines changed

28 files changed

+92
-230
lines changed

src/liballoc/allocator.rs

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,8 @@ impl Layout {
217217
/// On arithmetic overflow, returns `None`.
218218
#[inline]
219219
pub fn repeat(&self, n: usize) -> Option<(Self, usize)> {
220-
let padded_size = match self.size.checked_add(self.padding_needed_for(self.align)) {
221-
None => return None,
222-
Some(padded_size) => padded_size,
223-
};
224-
let alloc_size = match padded_size.checked_mul(n) {
225-
None => return None,
226-
Some(alloc_size) => alloc_size,
227-
};
220+
let padded_size = self.size.checked_add(self.padding_needed_for(self.align))?;
221+
let alloc_size = padded_size.checked_mul(n)?;
228222

229223
// We can assume that `self.align` is a power-of-two that does
230224
// not exceed 2<sup>31</sup>. Furthermore, `alloc_size` has already been
@@ -246,26 +240,14 @@ impl Layout {
246240
/// On arithmetic overflow, returns `None`.
247241
pub fn extend(&self, next: Self) -> Option<(Self, usize)> {
248242
let new_align = cmp::max(self.align, next.align);
249-
let realigned = match Layout::from_size_align(self.size, new_align) {
250-
None => return None,
251-
Some(l) => l,
252-
};
243+
let realigned = Layout::from_size_align(self.size, new_align)?;
253244

254245
let pad = realigned.padding_needed_for(next.align);
255246

256-
let offset = match self.size.checked_add(pad) {
257-
None => return None,
258-
Some(offset) => offset,
259-
};
260-
let new_size = match offset.checked_add(next.size) {
261-
None => return None,
262-
Some(new_size) => new_size,
263-
};
247+
let offset = self.size.checked_add(pad)?;
248+
let new_size = offset.checked_add(next.size)?;
264249

265-
let layout = match Layout::from_size_align(new_size, new_align) {
266-
None => return None,
267-
Some(l) => l,
268-
};
250+
let layout = Layout::from_size_align(new_size, new_align)?;
269251
Some((layout, offset))
270252
}
271253

@@ -282,11 +264,7 @@ impl Layout {
282264
///
283265
/// On arithmetic overflow, returns `None`.
284266
pub fn repeat_packed(&self, n: usize) -> Option<Self> {
285-
let size = match self.size().checked_mul(n) {
286-
None => return None,
287-
Some(scaled) => scaled,
288-
};
289-
267+
let size = self.size().checked_mul(n)?;
290268
Layout::from_size_align(size, self.align)
291269
}
292270

@@ -306,14 +284,8 @@ impl Layout {
306284
///
307285
/// On arithmetic overflow, returns `None`.
308286
pub fn extend_packed(&self, next: Self) -> Option<(Self, usize)> {
309-
let new_size = match self.size().checked_add(next.size()) {
310-
None => return None,
311-
Some(new_size) => new_size,
312-
};
313-
let layout = match Layout::from_size_align(new_size, self.align) {
314-
None => return None,
315-
Some(l) => l,
316-
};
287+
let new_size = self.size().checked_add(next.size())?;
288+
let layout = Layout::from_size_align(new_size, self.align)?;
317289
Some((layout, self.size()))
318290
}
319291

src/liballoc/btree/set.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,21 +1067,15 @@ impl<'a, T: Ord> Iterator for Intersection<'a, T> {
10671067

10681068
fn next(&mut self) -> Option<&'a T> {
10691069
loop {
1070-
let o_cmp = match (self.a.peek(), self.b.peek()) {
1071-
(None, _) => None,
1072-
(_, None) => None,
1073-
(Some(a1), Some(b1)) => Some(a1.cmp(b1)),
1074-
};
1075-
match o_cmp {
1076-
None => return None,
1077-
Some(Less) => {
1070+
match Ord::cmp(self.a.peek()?, self.b.peek()?) {
1071+
Less => {
10781072
self.a.next();
10791073
}
1080-
Some(Equal) => {
1074+
Equal => {
10811075
self.b.next();
10821076
return self.a.next();
10831077
}
1084-
Some(Greater) => {
1078+
Greater => {
10851079
self.b.next();
10861080
}
10871081
}

src/liballoc/string.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,7 @@ impl String {
10441044
#[inline]
10451045
#[stable(feature = "rust1", since = "1.0.0")]
10461046
pub fn pop(&mut self) -> Option<char> {
1047-
let ch = match self.chars().rev().next() {
1048-
Some(ch) => ch,
1049-
None => return None,
1050-
};
1047+
let ch = self.chars().rev().next()?;
10511048
let newlen = self.len() - ch.len_utf8();
10521049
unsafe {
10531050
self.vec.set_len(newlen);

src/liballoc/vec.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,10 +1423,7 @@ impl<T: PartialEq> Vec<T> {
14231423
/// ```
14241424
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
14251425
pub fn remove_item(&mut self, item: &T) -> Option<T> {
1426-
let pos = match self.iter().position(|x| *x == *item) {
1427-
Some(x) => x,
1428-
None => return None,
1429-
};
1426+
let pos = self.iter().position(|x| *x == *item)?;
14301427
Some(self.remove(pos))
14311428
}
14321429
}

src/libcore/str/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,10 @@ fn unwrap_or_0(opt: Option<&u8>) -> u8 {
494494
#[inline]
495495
pub fn next_code_point<'a, I: Iterator<Item = &'a u8>>(bytes: &mut I) -> Option<u32> {
496496
// Decode UTF-8
497-
let x = match bytes.next() {
498-
None => return None,
499-
Some(&next_byte) if next_byte < 128 => return Some(next_byte as u32),
500-
Some(&next_byte) => next_byte,
501-
};
497+
let x = *bytes.next()?;
498+
if x < 128 {
499+
return Some(x as u32)
500+
}
502501

503502
// Multibyte case follows
504503
// Decode from a byte combination out of: [[[x y] z] w]

src/librustc/hir/map/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,8 @@ impl<'a, 'hir> NodesMatchingSuffix<'a, 'hir> {
978978
// chain, then returns `None`.
979979
fn find_first_mod_parent<'a>(map: &'a Map, mut id: NodeId) -> Option<(NodeId, Name)> {
980980
loop {
981-
match map.find(id) {
982-
None => return None,
983-
Some(NodeItem(item)) if item_is_mod(&item) =>
981+
match map.find(id)? {
982+
NodeItem(item) if item_is_mod(&item) =>
984983
return Some((id, item.name)),
985984
_ => {}
986985
}

src/librustc/infer/error_reporting/util.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
9191
.iter()
9292
.enumerate()
9393
.filter_map(|(index, arg)| {
94-
let ty = match tables.borrow().node_id_to_type_opt(arg.hir_id) {
95-
Some(v) => v,
96-
None => return None, // sometimes the tables are not yet populated
97-
};
94+
// May return None; sometimes the tables are not yet populated.
95+
let ty = tables.borrow().node_id_to_type_opt(arg.hir_id)?;
9896
let mut found_anon_region = false;
9997
let new_arg_ty = self.tcx
10098
.fold_regions(&ty, &mut false, |r, _| if *r == *anon_region {

src/librustc/ty/util.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
439439
}
440440
});
441441

442-
let dtor_did = match dtor_did {
443-
Some(dtor) => dtor,
444-
None => return None,
445-
};
446-
447-
Some(ty::Destructor { did: dtor_did })
442+
Some(ty::Destructor { did: dtor_did? })
448443
}
449444

450445
/// Return the set of types that are required to be alive in

src/librustc/util/common.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,24 +215,18 @@ pub fn record_time<T, F>(accu: &Cell<Duration>, f: F) -> T where
215215
rv
216216
}
217217

218-
// Like std::macros::try!, but for Option<>.
219-
#[cfg(unix)]
220-
macro_rules! option_try(
221-
($e:expr) => (match $e { Some(e) => e, None => return None })
222-
);
223-
224218
// Memory reporting
225219
#[cfg(unix)]
226220
fn get_resident() -> Option<usize> {
227221
use std::fs::File;
228222
use std::io::Read;
229223

230224
let field = 1;
231-
let mut f = option_try!(File::open("/proc/self/statm").ok());
225+
let mut f = File::open("/proc/self/statm").ok()?;
232226
let mut contents = String::new();
233-
option_try!(f.read_to_string(&mut contents).ok());
234-
let s = option_try!(contents.split_whitespace().nth(field));
235-
let npages = option_try!(s.parse::<usize>().ok());
227+
f.read_to_string(&mut contents).ok()?;
228+
let s = contents.split_whitespace().nth(field)?;
229+
let npages = s.parse::<usize>().ok()?;
236230
Some(npages * 4096)
237231
}
238232

src/librustc_data_structures/indexed_set.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,8 @@ impl<'a, T: Idx> Iterator for Iter<'a, T> {
291291
}
292292
}
293293

294-
match self.iter.next() {
295-
Some((i, word)) => self.cur = Some((*word, word_bits * i)),
296-
None => return None,
297-
}
294+
let (i, word) = self.iter.next()?;
295+
self.cur = Some((*word, word_bits * i));
298296
}
299297
}
300298
}

0 commit comments

Comments
 (0)