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

View file

@ -233,14 +233,12 @@ fn yaml_parser_load_alias(
document: &mut Document,
ctx: &[i32],
) -> Result<(), ComposerError> {
let anchor: &str = if let EventData::Alias { anchor } = &event.data {
anchor
} else {
let EventData::Alias { anchor } = &event.data else {
unreachable!()
};
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);
}
}