Skip to content

Commit 5f4f355

Browse files
author
bors-servo
authored
Auto merge of servo#19537 - mbrubeck:try, r=nox
style: Use the ? operator for Option This is stable in Rust 1.22 (servo#19532). --- - [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 it is 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/19537) <!-- Reviewable:end -->
2 parents 051eb6b + 3005a26 commit 5f4f355

File tree

13 files changed

+47
-129
lines changed

13 files changed

+47
-129
lines changed

components/layout/fragment.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -1750,22 +1750,17 @@ impl Fragment {
17501750

17511751
let character_breaking_strategy =
17521752
text_fragment_info.run.character_slices_in_range(&text_fragment_info.range);
1753-
match self.calculate_split_position_using_breaking_strategy(character_breaking_strategy,
1754-
max_inline_size,
1755-
SplitOptions::empty()) {
1756-
None => None,
1757-
Some(split_info) => {
1758-
match split_info.inline_start {
1759-
None => None,
1760-
Some(split) => {
1761-
Some(TruncationResult {
1762-
split: split,
1763-
text_run: split_info.text_run.clone(),
1764-
})
1765-
}
1766-
}
1767-
}
1768-
}
1753+
1754+
let split_info = self.calculate_split_position_using_breaking_strategy(
1755+
character_breaking_strategy,
1756+
max_inline_size,
1757+
SplitOptions::empty())?;
1758+
1759+
let split = split_info.inline_start?;
1760+
Some(TruncationResult {
1761+
split: split,
1762+
text_run: split_info.text_run.clone(),
1763+
})
17691764
}
17701765

17711766
/// A helper method that uses the breaking strategy described by `slice_iterator` (at present,

components/style/animation.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,7 @@ impl PropertyAnimation {
339339
longhand,
340340
old_style,
341341
new_style,
342-
);
343-
344-
let animated_property = match animated_property {
345-
Some(p) => p,
346-
None => return None,
347-
};
342+
)?;
348343

349344
let property_animation = PropertyAnimation {
350345
property: animated_property,

components/style/bloom.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ impl<E: TElement> StyleBloom<E> {
169169
/// Pop the last element in the bloom filter and return it.
170170
#[inline]
171171
fn pop(&mut self) -> Option<E> {
172-
let (popped_element, num_hashes) = match self.elements.pop() {
173-
None => return None,
174-
Some(x) => (*x.element, x.num_hashes),
175-
};
172+
let PushedElement { element, num_hashes } = self.elements.pop()?;
173+
let popped_element = *element;
176174

177175
// Verify that the pushed hashes match the ones we'd get from the element.
178176
let mut expected_hashes = vec![];

components/style/custom_properties.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ where
153153
K: Borrow<Q>,
154154
Q: PrecomputedHash + Hash + Eq,
155155
{
156-
let index = match self.index.iter().position(|k| k.borrow() == key) {
157-
Some(p) => p,
158-
None => return None,
159-
};
156+
let index = self.index.iter().position(|k| k.borrow() == key)?;
160157
self.index.remove(index);
161158
self.values.remove(key)
162159
}
@@ -194,10 +191,7 @@ where
194191
type Item = (&'a K, &'a V);
195192

196193
fn next(&mut self) -> Option<Self::Item> {
197-
let key = match self.inner.index.get(self.pos) {
198-
Some(k) => k,
199-
None => return None,
200-
};
194+
let key = self.inner.index.get(self.pos)?;
201195

202196
self.pos += 1;
203197
let value = &self.inner.values[key];

components/style/dom.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,10 @@ where
7878

7979
fn next(&mut self) -> Option<N> {
8080
loop {
81-
match self.0.next() {
82-
Some(n) => {
83-
// Filter out nodes that layout should ignore.
84-
if n.is_text_node() || n.is_element() {
85-
return Some(n)
86-
}
87-
}
88-
None => return None,
81+
let n = self.0.next()?;
82+
// Filter out nodes that layout should ignore.
83+
if n.is_text_node() || n.is_element() {
84+
return Some(n)
8985
}
9086
}
9187
}
@@ -100,13 +96,9 @@ where
10096
type Item = N;
10197

10298
fn next(&mut self) -> Option<N> {
103-
match self.0.take() {
104-
Some(n) => {
105-
self.0 = n.next_sibling();
106-
Some(n)
107-
}
108-
None => None,
109-
}
99+
let n = self.0.take()?;
100+
self.0 = n.next_sibling();
101+
Some(n)
110102
}
111103
}
112104

@@ -124,11 +116,7 @@ where
124116

125117
#[inline]
126118
fn next(&mut self) -> Option<N> {
127-
let prev = match self.previous.take() {
128-
None => return None,
129-
Some(n) => n,
130-
};
131-
119+
let prev = self.previous.take()?;
132120
self.previous = prev.next_in_preorder(Some(self.scope));
133121
self.previous
134122
}

components/style/gecko/wrapper.rs

+6-25
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,7 @@ impl<'lb> GeckoXBLBinding<'lb> {
428428
if !binding.anon_content().is_null() {
429429
return Some(binding);
430430
}
431-
binding = match binding.base_binding() {
432-
Some(b) => b,
433-
None => return None,
434-
};
431+
binding = binding.base_binding()?;
435432
}
436433
}
437434

@@ -1006,20 +1003,14 @@ impl<'le> TElement for GeckoElement<'le> {
10061003

10071004
fn closest_non_native_anonymous_ancestor(&self) -> Option<Self> {
10081005
debug_assert!(self.is_native_anonymous());
1009-
let mut parent = match self.traversal_parent() {
1010-
Some(e) => e,
1011-
None => return None,
1012-
};
1006+
let mut parent = self.traversal_parent()?;
10131007

10141008
loop {
10151009
if !parent.is_native_anonymous() {
10161010
return Some(parent);
10171011
}
10181012

1019-
parent = match parent.traversal_parent() {
1020-
Some(p) => p,
1021-
None => return None,
1022-
};
1013+
parent = parent.traversal_parent()?;
10231014
}
10241015
}
10251016

@@ -1054,16 +1045,10 @@ impl<'le> TElement for GeckoElement<'le> {
10541045

10551046
fn get_smil_override(&self) -> Option<ArcBorrow<Locked<PropertyDeclarationBlock>>> {
10561047
unsafe {
1057-
let slots = match self.get_extended_slots() {
1058-
Some(s) => s,
1059-
None => return None,
1060-
};
1048+
let slots = self.get_extended_slots()?;
10611049

10621050
let base_declaration: &structs::DeclarationBlock =
1063-
match slots.mSMILOverrideStyleDeclaration.mRawPtr.as_ref() {
1064-
Some(decl) => decl,
1065-
None => return None,
1066-
};
1051+
slots.mSMILOverrideStyleDeclaration.mRawPtr.as_ref()?;
10671052

10681053
assert_eq!(base_declaration.mType, structs::StyleBackendType_Servo);
10691054
let declaration: &structs::ServoDeclarationBlock =
@@ -1074,11 +1059,7 @@ impl<'le> TElement for GeckoElement<'le> {
10741059
base_declaration as *const structs::DeclarationBlock
10751060
);
10761061

1077-
let raw: &structs::RawServoDeclarationBlock =
1078-
match declaration.mRaw.mRawPtr.as_ref() {
1079-
Some(decl) => decl,
1080-
None => return None,
1081-
};
1062+
let raw: &structs::RawServoDeclarationBlock = declaration.mRaw.mRawPtr.as_ref()?;
10821063

10831064
Some(Locked::<PropertyDeclarationBlock>::as_arc(
10841065
&*(&raw as *const &structs::RawServoDeclarationBlock)

components/style/invalidation/element/element_wrapper.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ impl<'a, E> ElementWrapper<'a, E>
127127
if lang.is_some() {
128128
return lang;
129129
}
130-
match current.parent_element() {
131-
Some(parent) => current = parent,
132-
None => return None,
133-
}
130+
current = current.parent_element()?;
134131
}
135132
}
136133
}

components/style/rule_cache.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,16 @@ impl RuleCache {
102102
return None;
103103
}
104104

105-
let rules = match builder_with_early_props.rules {
106-
Some(ref rules) => rules,
107-
None => return None,
108-
};
105+
let rules = builder_with_early_props.rules.as_ref()?;
106+
let cached_values = self.map.get(rules)?;
109107

110-
self.map.get(rules).and_then(|cached_values| {
111-
for &(ref conditions, ref values) in cached_values.iter() {
112-
if conditions.matches(builder_with_early_props) {
113-
debug!("Using cached reset style with conditions {:?}", conditions);
114-
return Some(&**values)
115-
}
108+
for &(ref conditions, ref values) in cached_values.iter() {
109+
if conditions.matches(builder_with_early_props) {
110+
debug!("Using cached reset style with conditions {:?}", conditions);
111+
return Some(&**values)
116112
}
117-
None
118-
})
113+
}
114+
None
119115
}
120116

121117
/// Inserts a node into the rules cache if possible.

components/style/style_resolver.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,7 @@ where
376376
originating_element_style.style(),
377377
pseudo,
378378
VisitedHandlingMode::AllLinksUnvisited
379-
);
380-
let rules = match rules {
381-
Some(rules) => rules,
382-
None => return None,
383-
};
379+
)?;
384380

385381
let mut visited_rules = None;
386382
if originating_element_style.style().visited_style().is_some() {

components/style/stylesheet_set.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ where
8080
fn next(&mut self) -> Option<Self::Item> {
8181
loop {
8282
if self.current.is_none() {
83-
let next_origin = match self.origins.next() {
84-
Some(o) => o,
85-
None => return None,
86-
};
83+
let next_origin = self.origins.next()?;
8784

8885
self.current =
8986
Some((next_origin, self.collections.borrow_for_origin(&next_origin).iter()));
@@ -238,10 +235,7 @@ where
238235
use std::mem;
239236

240237
loop {
241-
let potential_sheet = match self.iter.next() {
242-
Some(s) => s,
243-
None => return None,
244-
};
238+
let potential_sheet = self.iter.next()?;
245239

246240
let dirty = mem::replace(&mut potential_sheet.dirty, false);
247241
if dirty {

components/style/stylesheets/origin.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ impl Iterator for OriginSetIterator {
8989

9090
fn next(&mut self) -> Option<Origin> {
9191
loop {
92-
let origin = match Origin::from_index(self.cur) {
93-
Some(origin) => origin,
94-
None => return None,
95-
};
92+
let origin = Origin::from_index(self.cur)?;
9693

9794
self.cur += 1;
9895

@@ -184,10 +181,7 @@ impl<'a, T> Iterator for PerOriginIter<'a, T> where T: 'a {
184181
type Item = (&'a T, Origin);
185182

186183
fn next(&mut self) -> Option<Self::Item> {
187-
let origin = match Origin::from_index(self.cur) {
188-
Some(origin) => origin,
189-
None => return None,
190-
};
184+
let origin = Origin::from_index(self.cur)?;
191185

192186
self.cur += if self.rev { -1 } else { 1 };
193187

@@ -211,10 +205,7 @@ impl<'a, T> Iterator for PerOriginIterMut<'a, T> where T: 'a {
211205
type Item = (&'a mut T, Origin);
212206

213207
fn next(&mut self) -> Option<Self::Item> {
214-
let origin = match Origin::from_index(self.cur) {
215-
Some(origin) => origin,
216-
None => return None,
217-
};
208+
let origin = Origin::from_index(self.cur)?;
218209

219210
self.cur += 1;
220211

components/style/stylist.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,7 @@ impl<'a> Iterator for DocumentCascadeDataIter<'a> {
196196
type Item = (&'a CascadeData, Origin);
197197

198198
fn next(&mut self) -> Option<Self::Item> {
199-
let (_, origin) = match self.iter.next() {
200-
Some(o) => o,
201-
None => return None,
202-
};
203-
199+
let (_, origin) = self.iter.next()?;
204200
Some((self.cascade_data.borrow_for_origin(origin), origin))
205201
}
206202
}

components/style_derive/to_css.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ fn to_css_identifier(mut camel_case: &str) -> String {
145145

146146
/// Given "FooBar", returns "Foo" and sets `camel_case` to "Bar".
147147
fn split_camel_segment<'input>(camel_case: &mut &'input str) -> Option<&'input str> {
148-
let index = match camel_case.chars().next() {
149-
None => return None,
150-
Some(ch) => ch.len_utf8(),
151-
};
148+
let index = camel_case.chars().next()?.len_utf8();
152149
let end_position = camel_case[index..]
153150
.find(char::is_uppercase)
154151
.map_or(camel_case.len(), |pos| index + pos);

0 commit comments

Comments
 (0)