Skip to content

Commit 9853bef

Browse files
authored
Simplify a lot of unwrap_or usages (#850)
1 parent 2563ee8 commit 9853bef

6 files changed

Lines changed: 44 additions & 77 deletions

File tree

librubyfmt/src/file_comments.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ impl FileComments {
169169
debug_assert!(
170170
self.other_comments
171171
.last()
172-
.map(|(last_line_number, _)| *last_line_number < line_number)
173-
.unwrap_or(true),
172+
.is_none_or(|(last_line_number, _)| *last_line_number < line_number),
174173
"Expected comments to be inserted in order"
175174
);
176175

librubyfmt/src/format_prism.rs

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ pub fn format_node<'src>(ps: &mut ParserState<'src>, node: prism::Node<'src>) {
2626
&& !(matches!(node, Node::StatementsNode { .. })
2727
|| node
2828
.as_begin_node()
29-
.map(|b| b.begin_keyword_loc().is_none())
30-
.unwrap_or(false));
29+
.is_some_and(|b| b.begin_keyword_loc().is_none()));
3130

3231
if needs_handling {
3332
ps.emit_indent();
@@ -661,7 +660,7 @@ fn format_string_node<'src>(ps: &mut ParserState<'src>, string_node: prism::Stri
661660
// (e.g. the inner contents of a heredoc)
662661
let opener = string_node.opening_loc().map(|s| s.as_slice().trim_ascii());
663662
let closer = string_node.closing_loc().map(|s| s.as_slice().trim_ascii());
664-
let is_heredoc = opener.map(|s| s.starts_with(b"<")).unwrap_or(false);
663+
let is_heredoc = opener.is_some_and(|s| s.starts_with(b"<"));
665664

666665
if is_heredoc {
667666
format_heredoc(
@@ -680,7 +679,7 @@ fn format_string_node<'src>(ps: &mut ParserState<'src>, string_node: prism::Stri
680679

681680
// If opener is nil, we must be in some kind of interpolated string context, which
682681
// means the contents must already be appropriately escaped -- hence we default to `true` here
683-
let in_escaped_context = is_heredoc || opener.map(|s| s.starts_with(b"\"")).unwrap_or(true);
682+
let in_escaped_context = is_heredoc || opener.is_none_or(|s| s.starts_with(b"\""));
684683
let string_content = if in_escaped_context {
685684
Cow::Borrowed(string_node.content_loc().as_slice())
686685
} else {
@@ -721,10 +720,8 @@ fn format_interpolated_string_node<'src>(
721720
let closer = interpolated_string_node
722721
.closing_loc()
723722
.map(|s| s.as_slice().trim_ascii());
724-
let is_heredoc = opener.map(|s| s.starts_with(b"<")).unwrap_or(false);
725-
let needs_escape = opener
726-
.map(|s| !s.starts_with(b"\"") && !is_heredoc)
727-
.unwrap_or(false);
723+
let is_heredoc = opener.is_some_and(|s| s.starts_with(b"<"));
724+
let needs_escape = opener.is_some_and(|s| !s.starts_with(b"\"") && !is_heredoc);
728725

729726
// Prism actually handles string concatenation when using "\", so it treats
730727
// ```ruby
@@ -742,12 +739,10 @@ fn format_interpolated_string_node<'src>(
742739
&& interpolated_string_node.parts().len() > 1
743740
&& interpolated_string_node.parts().iter().any(|node| {
744741
node.as_string_node()
745-
.map(|node| node.opening_loc().is_some())
746-
.unwrap_or(false)
742+
.is_some_and(|node| node.opening_loc().is_some())
747743
|| node
748744
.as_interpolated_string_node()
749-
.map(|node| node.opening_loc().is_some())
750-
.unwrap_or(false)
745+
.is_some_and(|node| node.opening_loc().is_some())
751746
});
752747

753748
ps.at_offset(interpolated_string_node.location().start_offset());
@@ -879,13 +874,10 @@ fn maybe_render_heredocs_in_string<'src: 'a, 'a>(
879874
ps: &mut ParserState<'src>,
880875
peekable: &mut std::iter::Peekable<impl Iterator<Item = &'a prism::Node<'src>>>,
881876
) {
882-
let should_render = peekable
883-
.peek()
884-
.and_then(|node| {
885-
node.as_string_node()
886-
.map(|sn| sn.content_loc().as_slice().starts_with(b"\n"))
887-
})
888-
.unwrap_or(false);
877+
let should_render = peekable.peek().is_some_and(|node| {
878+
node.as_string_node()
879+
.is_some_and(|sn| sn.content_loc().as_slice().starts_with(b"\n"))
880+
});
889881
if should_render {
890882
ps.render_heredocs(true)
891883
}
@@ -1671,14 +1663,11 @@ fn use_parens_for_call_node<'src>(
16711663
// Foo # class reference
16721664
// Foo() # method call
16731665
// ```
1674-
let has_arguments = call_node
1675-
.arguments()
1676-
.map(|args| {
1677-
!(args.arguments().is_empty()
1678-
|| (args.arguments().len() == 1
1679-
&& is_empty_parentheses_node(&args.arguments().first().unwrap())))
1680-
})
1681-
.unwrap_or(false);
1666+
let has_arguments = call_node.arguments().is_some_and(|args| {
1667+
!(args.arguments().is_empty()
1668+
|| (args.arguments().len() == 1
1669+
&& is_empty_parentheses_node(&args.arguments().first().unwrap())))
1670+
});
16821671

16831672
if is_terminal_call && method_name.first().is_some_and(|c| c.is_ascii_uppercase()) {
16841673
if !has_arguments && call_node.block().is_some() {
@@ -1731,8 +1720,7 @@ fn use_parens_for_call_node<'src>(
17311720
let has_brace_block = call_node
17321721
.block()
17331722
.and_then(|b| b.as_block_node())
1734-
.map(|block| block.opening_loc().as_slice() != b"do")
1735-
.unwrap_or(false);
1723+
.is_some_and(|block| block.opening_loc().as_slice() != b"do");
17361724

17371725
if has_arguments && has_brace_block {
17381726
// Brace blocks require parens, eliding is a syntax error
@@ -1948,8 +1936,7 @@ fn format_call_node<'src>(
19481936
&& (!skip_receiver
19491937
|| call_node
19501938
.receiver()
1951-
.map(|r| r.as_self_node().is_some())
1952-
.unwrap_or(false))
1939+
.is_some_and(|r| r.as_self_node().is_some()))
19531940
{
19541941
// Check if we need parens in two cases: we're the
19551942
// first/only item in a call chain (thus `!skip_receiver`)
@@ -2048,8 +2035,7 @@ fn format_unary_operator<'src>(
20482035
let is_not_with_parens = method_name == b"!"
20492036
&& call_node
20502037
.message_loc()
2051-
.map(|loc| loc.as_slice() == b"not")
2052-
.unwrap_or(false)
2038+
.is_some_and(|loc| loc.as_slice() == b"not")
20532039
&& call_node.opening_loc().is_some();
20542040

20552041
let operator_symbol: &[u8] = match method_name {
@@ -2265,8 +2251,7 @@ fn extract_trailing_arefs(mut elements: Vec<prism::Node>) -> (Vec<prism::Node>,
22652251
let mut trailing_arefs = Vec::new();
22662252
let has_dot_calls = elements.iter().any(|elem| {
22672253
elem.as_call_node()
2268-
.map(|c| c.call_operator_loc().is_some())
2269-
.unwrap_or(false)
2254+
.is_some_and(|c| c.call_operator_loc().is_some())
22702255
});
22712256

22722257
if has_dot_calls {
@@ -2444,12 +2429,10 @@ fn call_chain_elements_are_user_multilined(
24442429
// to first skip it if it's not a CallNode and there's an aref following it.
24452430
let first_is_not_call = call_chain_elements
24462431
.first()
2447-
.map(|n| n.as_call_node().is_none())
2448-
.unwrap_or(false);
2432+
.is_some_and(|n| n.as_call_node().is_none());
24492433
let second_is_aref = call_chain_elements.get(1).is_some_and(|n| {
24502434
n.as_call_node()
2451-
.map(|c| c.call_operator_loc().is_none())
2452-
.unwrap_or(false)
2435+
.is_some_and(|c| c.call_operator_loc().is_none())
24532436
});
24542437

24552438
if first_is_not_call && second_is_aref {
@@ -2481,8 +2464,9 @@ fn call_chain_elements_are_user_multilined(
24812464
.as_call_node()
24822465
.unwrap()
24832466
.call_operator_loc()
2484-
.map(|loc| ps.get_line_number_for_offset(loc.start_offset()))
2485-
.unwrap_or(start_line)
2467+
.map_or(start_line, |loc| {
2468+
ps.get_line_number_for_offset(loc.start_offset())
2469+
})
24862470
})
24872471
}
24882472

@@ -2589,7 +2573,7 @@ fn format_symbol_node<'src>(ps: &mut ParserState<'src>, symbol_node: prism::Symb
25892573

25902574
// Check if this is a quoted symbol that needs normalization to double quotes
25912575
// Symbols like :'"foo"' (single-quoted) should become :"\"foo\""
2592-
let is_single_quoted = opener.map(|s| s == b":'").unwrap_or(false);
2576+
let is_single_quoted = opener.is_some_and(|s| s == b":'");
25932577

25942578
if is_single_quoted {
25952579
ps.emit_ident(b":");
@@ -2716,8 +2700,7 @@ fn format_block_node<'src>(ps: &mut ParserState<'src>, block_node: prism::BlockN
27162700
if let Some(body) = block_node.body() {
27172701
let has_multiple_statements = body
27182702
.as_statements_node()
2719-
.map(|statements_node| statements_node.body().len() > 1)
2720-
.unwrap_or(false);
2703+
.is_some_and(|statements_node| statements_node.body().len() > 1);
27212704
if has_multiple_statements {
27222705
ps.emit_newline();
27232706
ps.emit_indent();
@@ -2816,7 +2799,7 @@ fn format_array_node<'src>(ps: &mut ParserState<'src>, array_node: prism::ArrayN
28162799
let opening = array_node
28172800
.opening_loc()
28182801
.map(|loc| loc.as_slice().trim_ascii());
2819-
let is_word_array = opening.map(|s| s.starts_with(b"%")).unwrap_or(false);
2802+
let is_word_array = opening.is_some_and(|s| s.starts_with(b"%"));
28202803

28212804
let orig_delim = opening.and_then(|s| s.get(2).copied()).unwrap_or(b'[');
28222805

@@ -3620,8 +3603,8 @@ fn format_hash_pattern_node<'src>(
36203603
}
36213604

36223605
let opener = hash_pattern_node.opening_loc().map(|loc| loc.as_slice());
3623-
let use_parens = hash_pattern_node.constant().is_some()
3624-
|| opener.map(|s| s.starts_with(b"(")).unwrap_or(false);
3606+
let use_parens =
3607+
hash_pattern_node.constant().is_some() || opener.is_some_and(|s| s.starts_with(b"("));
36253608

36263609
let elements = hash_pattern_node.elements();
36273610

@@ -3721,8 +3704,7 @@ impl<'pr> Conditional<'pr> {
37213704
match self {
37223705
Conditional::If(node) => node
37233706
.if_keyword_loc()
3724-
.map(|loc| loc.start_offset())
3725-
.unwrap_or(node.location().start_offset()),
3707+
.map_or(node.location().start_offset(), |loc| loc.start_offset()),
37263708
Conditional::Unless(node) => node.keyword_loc().start_offset(),
37273709
Conditional::While(node) => node.keyword_loc().start_offset(),
37283710
Conditional::Until(node) => node.keyword_loc().start_offset(),
@@ -4864,18 +4846,17 @@ fn format_yield_node<'src>(ps: &mut ParserState<'src>, yield_node: prism::YieldN
48644846
let use_parens = ps.current_formatting_context_requires_parens()
48654847
|| yield_node.lparen_loc().is_some()
48664848
// For single assoc values (`yield a: b`) we keep parens
4867-
|| (yield_node
4849+
|| yield_node
48684850
.arguments()
4869-
.map(|args| {
4851+
.is_some_and(|args| {
48704852
args.arguments().len() == 1
48714853
&& args
48724854
.arguments()
48734855
.last()
48744856
.unwrap()
48754857
.as_keyword_hash_node()
48764858
.is_some()
4877-
})
4878-
.unwrap_or(false));
4859+
});
48794860

48804861
let delims = if use_parens {
48814862
BreakableDelims::for_method_call()

librubyfmt/src/intermediary.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ impl<'src> Intermediary<'src> {
105105
}
106106
}
107107
ConcreteLineToken::MethodName { name } => {
108-
if *name == b"require" && self.tokens.last().map(|t| t.is_indent()).unwrap_or(false)
109-
{
108+
if *name == b"require" && self.tokens.last().is_some_and(|t| t.is_indent()) {
110109
self.current_line_metadata.set_has_require();
111110
}
112111
}

librubyfmt/src/parser_state.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ impl<'src> ParserState<'src> {
153153
if self
154154
.comments_to_insert
155155
.as_ref()
156-
.map(|comments| comments.has_comments())
157-
.unwrap_or(false)
156+
.is_some_and(|comments| comments.has_comments())
158157
{
159158
self.push_concrete_token(ConcreteLineToken::HardNewLine);
160159
}
@@ -431,10 +430,7 @@ impl<'src> ParserState<'src> {
431430
pub(crate) fn wind_dumping_comments(&mut self, maybe_max_line_number: Option<LineNumber>) {
432431
// Return early if we're already at/past
433432
// the max line number
434-
if maybe_max_line_number
435-
.map(|ln| ln <= self.current_orig_line_number)
436-
.unwrap_or(false)
437-
{
433+
if maybe_max_line_number.is_some_and(|ln| ln <= self.current_orig_line_number) {
438434
return;
439435
}
440436

@@ -443,18 +439,13 @@ impl<'src> ParserState<'src> {
443439
// If we have a max line number, it will be the last token
444440
// of an expression (e.g. the `end` of a `do`/`end` block), so it's
445441
// fine if we wind forward to that line
446-
if maybe_max_line_number
447-
.map(|max| ln + 1 == max)
448-
.unwrap_or(false)
449-
{
442+
if maybe_max_line_number.is_some_and(|max| ln + 1 == max) {
450443
return true;
451444
}
452445

453446
ps.comments_hash.still_in_file(ln + 1)
454447
&& (ps.comments_hash.has_line(ln + 1) || ps.comments_hash.is_empty_line(ln + 1))
455-
&& maybe_max_line_number
456-
.map(|max_line| ln + 1 < max_line)
457-
.unwrap_or(true)
448+
&& maybe_max_line_number.is_none_or(|max_line| ln + 1 < max_line)
458449
};
459450
while should_iter(self, self.current_orig_line_number) {
460451
// If the next line is empty (no comment, no Ruby code), and we have

librubyfmt/src/render_queue_writer.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ impl<'src> RenderQueueWriter<'src> {
7878
ConcreteLineTokenAndTargets::ConcreteLineToken(ConcreteLineToken::DirectPart {
7979
part,
8080
}) => {
81-
if current_heredoc_kind
82-
.map(|k| k.is_squiggly())
83-
.unwrap_or(false)
84-
{
81+
if current_heredoc_kind.is_some_and(|k| k.is_squiggly()) {
8582
let indent = get_indent(accum.additional_indent as usize * 2);
8683
let indent_bytes = indent.as_ref();
8784
let mut new_contents = Vec::new();
@@ -107,7 +104,7 @@ impl<'src> RenderQueueWriter<'src> {
107104
) => {
108105
// Bare heredocs (e.g. <<FOO) must have the closing ident completely unindented, so
109106
// ignore them in this case
110-
if current_heredoc_kind.map(|k| !k.is_bare()).unwrap_or(false) {
107+
if current_heredoc_kind.is_some_and(|k| !k.is_bare()) {
111108
let indent = get_indent(accum.additional_indent as usize * 2);
112109
let mut new_contents = indent.into_owned();
113110
new_contents.extend_from_slice(symbol);

librubyfmt/src/render_targets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'src> BaseQueue<'src> {
3636
}
3737

3838
pub fn last_token_is_a_newline(&self) -> bool {
39-
self.tokens.last().map(|x| x.is_newline()).unwrap_or(false)
39+
self.tokens.last().is_some_and(|x| x.is_newline())
4040
}
4141

4242
pub fn index_of_prev_newline(&self) -> Option<usize> {
@@ -572,7 +572,7 @@ impl<'src> ConditionalLayoutEntry<'src> {
572572
ConditionalLayoutPhase::Predicate => &self.predicate_tokens,
573573
ConditionalLayoutPhase::Statement => &self.statement_tokens,
574574
};
575-
tokens.last().map(|x| x.is_newline()).unwrap_or(false)
575+
tokens.last().is_some_and(|x| x.is_newline())
576576
}
577577

578578
pub fn index_of_prev_newline(&self) -> Option<usize> {

0 commit comments

Comments
 (0)