Auto merge of #18968 - mbrubeck:try, r=emilio

Use try syntax for Option where appropriate

- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes do not require tests because they are refactoring only

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18968)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-10-21 03:31:21 -05:00 committed by GitHub
commit 2b03a9974c
19 changed files with 65 additions and 180 deletions

View file

@ -2089,10 +2089,9 @@ fn get_registrable_domain_suffix_of_or_is_equal_to(host_suffix_string: &str, ori
};
// Step 4.2
let (prefix, suffix) = match original_host.len().checked_sub(host.len()) {
Some(index) => original_host.split_at(index),
None => return None,
};
let index = original_host.len().checked_sub(host.len())?;
let (prefix, suffix) = original_host.split_at(index);
if !prefix.ends_with(".") {
return None;
}
@ -2336,11 +2335,7 @@ impl Document {
///
/// Also, shouldn't return an option, I'm quite sure.
pub fn device(&self) -> Option<Device> {
let window_size = match self.window().window_size() {
Some(ws) => ws,
None => return None,
};
let window_size = self.window().window_size()?;
let viewport_size = window_size.initial_viewport;
let device_pixel_ratio = window_size.device_pixel_ratio;
Some(Device::new(MediaType::screen(), viewport_size, device_pixel_ratio))
@ -4034,12 +4029,9 @@ impl PendingInOrderScriptVec {
fn take_next_ready_to_be_executed(&self) -> Option<(DomRoot<HTMLScriptElement>, ScriptResult)> {
let mut scripts = self.scripts.borrow_mut();
let pair = scripts.front_mut().and_then(PendingScript::take_result);
if pair.is_none() {
return None;
}
let pair = scripts.front_mut()?.take_result()?;
scripts.pop_front();
pair
Some(pair)
}
fn clear(&self) {

View file

@ -1040,24 +1040,20 @@ impl Element {
// https://dom.spec.whatwg.org/#locate-a-namespace-prefix
pub fn lookup_prefix(&self, namespace: Namespace) -> Option<DOMString> {
for node in self.upcast::<Node>().inclusive_ancestors() {
match node.downcast::<Element>() {
Some(element) => {
// Step 1.
if *element.namespace() == namespace {
if let Some(prefix) = element.GetPrefix() {
return Some(prefix);
}
}
let element = node.downcast::<Element>()?;
// Step 1.
if *element.namespace() == namespace {
if let Some(prefix) = element.GetPrefix() {
return Some(prefix);
}
}
// Step 2.
for attr in element.attrs.borrow().iter() {
if attr.prefix() == Some(&namespace_prefix!("xmlns")) &&
**attr.value() == *namespace {
return Some(attr.LocalName());
}
}
},
None => return None,
// Step 2.
for attr in element.attrs.borrow().iter() {
if attr.prefix() == Some(&namespace_prefix!("xmlns")) &&
**attr.value() == *namespace {
return Some(attr.LocalName());
}
}
}
None

View file

@ -219,12 +219,7 @@ impl HTMLCanvasElement {
let msg = CanvasMsg::FromScript(FromScriptMsg::SendPixels(sender));
context.get_ipc_renderer().send(msg).unwrap();
match receiver.recv().unwrap() {
Some(pixels) => pixels,
None => {
return None;
}
}
receiver.recv().unwrap()?
},
Some(&CanvasContext::WebGL(_)) => {
// TODO: add a method in WebGLRenderingContext to get the pixels.

View file

@ -588,17 +588,13 @@ impl HTMLIFrameElementMethods for HTMLIFrameElement {
// https://html.spec.whatwg.org/multipage/#concept-bcc-content-document
fn GetContentDocument(&self) -> Option<DomRoot<Document>> {
// Step 1.
let pipeline_id = match self.pipeline_id.get() {
None => return None,
Some(pipeline_id) => pipeline_id,
};
let pipeline_id = self.pipeline_id.get()?;
// Step 2-3.
// Note that this lookup will fail if the document is dissimilar-origin,
// so we should return None in that case.
let document = match ScriptThread::find_document(pipeline_id) {
None => return None,
Some(document) => document,
};
let document = ScriptThread::find_document(pipeline_id)?;
// Step 4.
let current = GlobalScope::current().expect("No current global object").as_window().Document();
if !current.origin().same_origin_domain(document.origin()) {

View file

@ -661,10 +661,7 @@ impl HTMLImageElement {
}
pub fn areas(&self) -> Option<Vec<DomRoot<HTMLAreaElement>>> {
let elem = self.upcast::<Element>();
let usemap_attr = match elem.get_attribute(&ns!(), &local_name!("usemap")) {
Some(attr) => attr,
None => return None,
};
let usemap_attr = elem.get_attribute(&ns!(), &local_name!("usemap"))?;
let value = usemap_attr.value();

View file

@ -1206,11 +1206,7 @@ pub struct FollowingNodeIterator {
impl FollowingNodeIterator {
/// Skips iterating the children of the current node
pub fn next_skipping_children(&mut self) -> Option<DomRoot<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
};
let current = self.current.take()?;
self.next_skipping_children_impl(current)
}
@ -1244,10 +1240,7 @@ impl Iterator for FollowingNodeIterator {
// https://dom.spec.whatwg.org/#concept-tree-following
fn next(&mut self) -> Option<DomRoot<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
};
let current = self.current.take()?;
if let Some(first_child) = current.GetFirstChild() {
self.current = Some(first_child);
@ -1268,10 +1261,7 @@ impl Iterator for PrecedingNodeIterator {
// https://dom.spec.whatwg.org/#concept-tree-preceding
fn next(&mut self) -> Option<DomRoot<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
};
let current = self.current.take()?;
self.current = if self.root == current {
None
@ -1323,10 +1313,7 @@ impl TreeIterator {
}
pub fn next_skipping_children(&mut self) -> Option<DomRoot<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
};
let current = self.current.take()?;
self.next_skipping_children_impl(current)
}
@ -1353,10 +1340,7 @@ impl Iterator for TreeIterator {
// https://dom.spec.whatwg.org/#concept-tree-order
fn next(&mut self) -> Option<DomRoot<Node>> {
let current = match self.current.take() {
None => return None,
Some(current) => current,
};
let current = self.current.take()?;
if let Some(first_child) = current.GetFirstChild() {
self.current = Some(first_child);
self.depth += 1;

View file

@ -480,10 +480,7 @@ impl<I> Iterator for FragmentParsingResult<I>
type Item = DomRoot<Node>;
fn next(&mut self) -> Option<DomRoot<Node>> {
let next = match self.inner.next() {
Some(next) => next,
None => return None,
};
let next = self.inner.next()?;
next.remove_self();
Some(next)
}

View file

@ -395,13 +395,9 @@ impl TreeWalker {
None => {
let mut candidate = DomRoot::from_ref(node);
while !self.is_root_node(&candidate) && candidate.GetNextSibling().is_none() {
match candidate.GetParentNode() {
None =>
// This can happen if the user set the current node to somewhere
// outside of the tree rooted at the original root.
return None,
Some(n) => candidate = n
}
// This can return None if the user set the current node to somewhere
// outside of the tree rooted at the original root.
candidate = candidate.GetParentNode()?;
}
if self.is_root_node(&candidate) {
None

View file

@ -591,15 +591,11 @@ impl WindowMethods for Window {
// https://html.spec.whatwg.org/multipage/#dom-frameelement
fn GetFrameElement(&self) -> Option<DomRoot<Element>> {
// Steps 1-3.
let window_proxy = match self.window_proxy.get() {
None => return None,
Some(window_proxy) => window_proxy,
};
let window_proxy = self.window_proxy.get()?;
// Step 4-5.
let container = match window_proxy.frame_element() {
None => return None,
Some(container) => container,
};
let container = window_proxy.frame_element()?;
// Step 6.
let container_doc = document_from_node(container);
let current_doc = GlobalScope::current().expect("No current global object").as_window().Document();
@ -687,10 +683,8 @@ impl WindowMethods for Window {
// https://html.spec.whatwg.org/multipage/#dom-parent
fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
// Steps 1-3.
let window_proxy = match self.undiscarded_window_proxy() {
Some(window_proxy) => window_proxy,
None => return None,
};
let window_proxy = self.undiscarded_window_proxy()?;
// Step 4.
if let Some(parent) = window_proxy.parent() {
return Some(DomRoot::from_ref(parent));
@ -702,10 +696,8 @@ impl WindowMethods for Window {
// https://html.spec.whatwg.org/multipage/#dom-top
fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
// Steps 1-3.
let window_proxy = match self.undiscarded_window_proxy() {
Some(window_proxy) => window_proxy,
None => return None,
};
let window_proxy = self.undiscarded_window_proxy()?;
// Steps 4-5.
Some(DomRoot::from_ref(window_proxy.top()))
}