Stylistic: Prefer let-else

This commit is contained in:
Simon Ask Ulsnes 2024-02-04 09:43:06 +01:00
parent 7071ddd276
commit 1ffb036601
3 changed files with 109 additions and 123 deletions

View file

@ -167,7 +167,9 @@ fn yaml_emitter_dump_scalar(
let plain_implicit = node.tag.as_deref() == Some(DEFAULT_SCALAR_TAG); let plain_implicit = node.tag.as_deref() == Some(DEFAULT_SCALAR_TAG);
let quoted_implicit = node.tag.as_deref() == Some(DEFAULT_SCALAR_TAG); // TODO: Why compare twice?! (even the C code does this) let quoted_implicit = node.tag.as_deref() == Some(DEFAULT_SCALAR_TAG); // TODO: Why compare twice?! (even the C code does this)
if let NodeData::Scalar { value, style } = node.data { let NodeData::Scalar { value, style } = node.data else {
unreachable!()
};
let event = Event { let event = Event {
data: EventData::Scalar { data: EventData::Scalar {
anchor, anchor,
@ -180,9 +182,6 @@ fn yaml_emitter_dump_scalar(
..Default::default() ..Default::default()
}; };
yaml_emitter_emit(emitter, event) yaml_emitter_emit(emitter, event)
} else {
unreachable!()
}
} }
fn yaml_emitter_dump_sequence( fn yaml_emitter_dump_sequence(
@ -193,7 +192,9 @@ fn yaml_emitter_dump_sequence(
) -> Result<(), EmitterError> { ) -> Result<(), EmitterError> {
let implicit = node.tag.as_deref() == Some(DEFAULT_SEQUENCE_TAG); let implicit = node.tag.as_deref() == Some(DEFAULT_SEQUENCE_TAG);
if let NodeData::Sequence { items, style } = node.data { let NodeData::Sequence { items, style } = node.data else {
unreachable!()
};
let event = Event { let event = Event {
data: EventData::SequenceStart { data: EventData::SequenceStart {
anchor, anchor,
@ -213,9 +214,6 @@ fn yaml_emitter_dump_sequence(
..Default::default() ..Default::default()
}; };
yaml_emitter_emit(emitter, event) yaml_emitter_emit(emitter, event)
} else {
unreachable!()
}
} }
fn yaml_emitter_dump_mapping( fn yaml_emitter_dump_mapping(
@ -226,7 +224,9 @@ fn yaml_emitter_dump_mapping(
) -> Result<(), EmitterError> { ) -> Result<(), EmitterError> {
let implicit = node.tag.as_deref() == Some(DEFAULT_MAPPING_TAG); let implicit = node.tag.as_deref() == Some(DEFAULT_MAPPING_TAG);
if let NodeData::Mapping { pairs, style } = node.data { let NodeData::Mapping { pairs, style } = node.data else {
unreachable!()
};
let event = Event { let event = Event {
data: EventData::MappingStart { data: EventData::MappingStart {
anchor, anchor,
@ -247,7 +247,4 @@ fn yaml_emitter_dump_mapping(
..Default::default() ..Default::default()
}; };
yaml_emitter_emit(emitter, event) yaml_emitter_emit(emitter, event)
} else {
unreachable!()
}
} }

View file

@ -642,15 +642,13 @@ fn yaml_emitter_emit_sequence_start(
yaml_emitter_process_anchor(emitter, anchor)?; yaml_emitter_process_anchor(emitter, anchor)?;
yaml_emitter_process_tag(emitter, tag)?; yaml_emitter_process_tag(emitter, tag)?;
let style = if let EventData::SequenceStart { style, .. } = &event.data { let EventData::SequenceStart { style, .. } = &event.data else {
*style
} else {
unreachable!() unreachable!()
}; };
if emitter.flow_level != 0 if emitter.flow_level != 0
|| emitter.canonical || emitter.canonical
|| style == SequenceStyle::Flow || *style == SequenceStyle::Flow
|| yaml_emitter_check_empty_sequence(emitter, event) || yaml_emitter_check_empty_sequence(emitter, event)
{ {
emitter.state = EmitterState::FlowSequenceFirstItem; emitter.state = EmitterState::FlowSequenceFirstItem;
@ -669,15 +667,13 @@ fn yaml_emitter_emit_mapping_start(
yaml_emitter_process_anchor(emitter, anchor)?; yaml_emitter_process_anchor(emitter, anchor)?;
yaml_emitter_process_tag(emitter, tag)?; yaml_emitter_process_tag(emitter, tag)?;
let style = if let EventData::MappingStart { style, .. } = &event.data { let EventData::MappingStart { style, .. } = &event.data else {
*style
} else {
unreachable!() unreachable!()
}; };
if emitter.flow_level != 0 if emitter.flow_level != 0
|| emitter.canonical || emitter.canonical
|| style == MappingStyle::Flow || *style == MappingStyle::Flow
|| yaml_emitter_check_empty_mapping(emitter, event) || yaml_emitter_check_empty_mapping(emitter, event)
{ {
emitter.state = EmitterState::FlowMappingFirstKey; emitter.state = EmitterState::FlowMappingFirstKey;
@ -759,20 +755,20 @@ fn yaml_emitter_select_scalar_style(
scalar_analysis: &mut ScalarAnalysis, scalar_analysis: &mut ScalarAnalysis,
tag_analysis: &mut Option<TagAnalysis>, tag_analysis: &mut Option<TagAnalysis>,
) -> Result<(), EmitterError> { ) -> Result<(), EmitterError> {
if let EventData::Scalar { let EventData::Scalar {
plain_implicit, plain_implicit,
quoted_implicit, quoted_implicit,
style, style,
.. ..
} = &event.data } = &event.data
{ else {
unreachable!()
};
let mut style: ScalarStyle = *style; let mut style: ScalarStyle = *style;
let no_tag = tag_analysis.is_none(); let no_tag = tag_analysis.is_none();
if no_tag && !*plain_implicit && !*quoted_implicit { if no_tag && !*plain_implicit && !*quoted_implicit {
yaml_emitter_set_emitter_error( yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified")?;
emitter,
"neither tag nor implicit flags are specified",
)?;
} }
if style == ScalarStyle::Any { if style == ScalarStyle::Any {
style = ScalarStyle::Plain; style = ScalarStyle::Plain;
@ -802,9 +798,7 @@ fn yaml_emitter_select_scalar_style(
style = ScalarStyle::DoubleQuoted; style = ScalarStyle::DoubleQuoted;
} }
if (style == ScalarStyle::Literal || style == ScalarStyle::Folded) if (style == ScalarStyle::Literal || style == ScalarStyle::Folded)
&& (!scalar_analysis.block_allowed && (!scalar_analysis.block_allowed || emitter.flow_level != 0 || emitter.simple_key_context)
|| emitter.flow_level != 0
|| emitter.simple_key_context)
{ {
style = ScalarStyle::DoubleQuoted; style = ScalarStyle::DoubleQuoted;
} }
@ -816,9 +810,6 @@ fn yaml_emitter_select_scalar_style(
} }
scalar_analysis.style = style; scalar_analysis.style = style;
Ok(()) Ok(())
} else {
unreachable!()
}
} }
fn yaml_emitter_process_anchor( fn yaml_emitter_process_anchor(

View file

@ -233,14 +233,12 @@ fn yaml_parser_load_alias(
document: &mut Document, document: &mut Document,
ctx: &[i32], ctx: &[i32],
) -> Result<(), ComposerError> { ) -> Result<(), ComposerError> {
let anchor: &str = if let EventData::Alias { anchor } = &event.data { let EventData::Alias { anchor } = &event.data else {
anchor
} else {
unreachable!() unreachable!()
}; };
for alias_data in &parser.aliases { for alias_data in &parser.aliases {
if alias_data.anchor == anchor { if alias_data.anchor == *anchor {
return yaml_parser_load_node_add(document, ctx, alias_data.index); return yaml_parser_load_node_add(document, ctx, alias_data.index);
} }
} }