Lint; Remove Result from infallible functions

This commit is contained in:
Simon Ask Ulsnes 2024-02-02 10:38:33 +01:00
parent cac3d2d788
commit 8a080a1ce7
6 changed files with 113 additions and 178 deletions

View file

@ -204,24 +204,19 @@ pub fn yaml_token_delete(token: &mut yaml_token_t) {
}
/// Create the STREAM-START event.
pub fn yaml_stream_start_event_initialize(
event: &mut yaml_event_t,
encoding: yaml_encoding_t,
) -> Result<(), ()> {
pub fn yaml_stream_start_event_initialize(event: &mut yaml_event_t, encoding: yaml_encoding_t) {
*event = yaml_event_t {
data: YamlEventData::StreamStart { encoding },
..Default::default()
};
Ok(())
}
/// Create the STREAM-END event.
pub fn yaml_stream_end_event_initialize(event: &mut yaml_event_t) -> Result<(), ()> {
pub fn yaml_stream_end_event_initialize(event: &mut yaml_event_t) {
*event = yaml_event_t {
data: YamlEventData::StreamEnd,
..Default::default()
};
Ok(())
}
/// Create the DOCUMENT-START event.
@ -233,7 +228,7 @@ pub fn yaml_document_start_event_initialize(
version_directive: Option<yaml_version_directive_t>,
tag_directives_in: &[yaml_tag_directive_t],
implicit: bool,
) -> Result<(), ()> {
) {
let tag_directives = Vec::from_iter(tag_directives_in.iter().cloned());
*event = yaml_event_t {
@ -244,34 +239,27 @@ pub fn yaml_document_start_event_initialize(
},
..Default::default()
};
Ok(())
}
/// Create the DOCUMENT-END event.
///
/// The `implicit` argument is considered as a stylistic parameter and may be
/// ignored by the emitter.
pub fn yaml_document_end_event_initialize(
event: &mut yaml_event_t,
implicit: bool,
) -> Result<(), ()> {
pub fn yaml_document_end_event_initialize(event: &mut yaml_event_t, implicit: bool) {
*event = yaml_event_t {
data: YamlEventData::DocumentEnd { implicit },
..Default::default()
};
Ok(())
}
/// Create an ALIAS event.
pub fn yaml_alias_event_initialize(event: &mut yaml_event_t, anchor: &str) -> Result<(), ()> {
pub fn yaml_alias_event_initialize(event: &mut yaml_event_t, anchor: &str) {
*event = yaml_event_t {
data: YamlEventData::Alias {
anchor: String::from(anchor),
},
..Default::default()
};
Ok(())
}
/// Create a SCALAR event.
@ -289,7 +277,7 @@ pub fn yaml_scalar_event_initialize(
plain_implicit: bool,
quoted_implicit: bool,
style: yaml_scalar_style_t,
) -> Result<(), ()> {
) {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
@ -317,7 +305,6 @@ pub fn yaml_scalar_event_initialize(
start_mark: mark,
end_mark: mark,
};
Ok(())
}
/// Create a SEQUENCE-START event.
@ -331,7 +318,7 @@ pub fn yaml_sequence_start_event_initialize(
tag: Option<&str>,
implicit: bool,
style: yaml_sequence_style_t,
) -> Result<(), ()> {
) {
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
@ -351,16 +338,14 @@ pub fn yaml_sequence_start_event_initialize(
},
..Default::default()
};
Ok(())
}
/// Create a SEQUENCE-END event.
pub fn yaml_sequence_end_event_initialize(event: &mut yaml_event_t) -> Result<(), ()> {
pub fn yaml_sequence_end_event_initialize(event: &mut yaml_event_t) {
*event = yaml_event_t {
data: YamlEventData::SequenceEnd,
..Default::default()
};
Ok(())
}
/// Create a MAPPING-START event.
@ -374,7 +359,7 @@ pub fn yaml_mapping_start_event_initialize(
tag: Option<&str>,
implicit: bool,
style: yaml_mapping_style_t,
) -> Result<(), ()> {
) {
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
@ -395,17 +380,14 @@ pub fn yaml_mapping_start_event_initialize(
},
..Default::default()
};
Ok(())
}
/// Create a MAPPING-END event.
pub fn yaml_mapping_end_event_initialize(event: &mut yaml_event_t) -> Result<(), ()> {
pub fn yaml_mapping_end_event_initialize(event: &mut yaml_event_t) {
*event = yaml_event_t {
data: YamlEventData::MappingEnd,
..Default::default()
};
Ok(())
}
/// Free any memory allocated for an event object.
@ -420,7 +402,7 @@ pub fn yaml_document_initialize(
tag_directives_in: &[yaml_tag_directive_t],
start_implicit: bool,
end_implicit: bool,
) -> Result<(), ()> {
) {
let nodes = Vec::with_capacity(16);
let tag_directives = Vec::from_iter(tag_directives_in.iter().cloned());
@ -432,8 +414,6 @@ pub fn yaml_document_initialize(
end_implicit,
..Default::default()
};
Ok(())
}
/// Delete a YAML document and all its nodes.
@ -576,7 +556,7 @@ pub fn yaml_document_append_sequence_item(
document: &mut yaml_document_t,
sequence: libc::c_int,
item: libc::c_int,
) -> Result<(), ()> {
) {
__assert!(sequence > 0 && sequence as usize - 1 < document.nodes.len());
__assert!(matches!(
&document.nodes[sequence as usize - 1].data,
@ -588,7 +568,6 @@ pub fn yaml_document_append_sequence_item(
{
items.push(item);
}
Ok(())
}
/// Add a pair of a key and a value to a MAPPING node.
@ -597,7 +576,7 @@ pub fn yaml_document_append_mapping_pair(
mapping: libc::c_int,
key: libc::c_int,
value: libc::c_int,
) -> Result<(), ()> {
) {
__assert!(mapping > 0 && mapping as usize - 1 < document.nodes.len());
__assert!(matches!(
&document.nodes[mapping as usize - 1].data,
@ -611,5 +590,4 @@ pub fn yaml_document_append_mapping_pair(
{
pairs.push(pair);
}
Ok(())
}

View file

@ -56,7 +56,7 @@ pub(crate) fn test_main(
}
let line = line_buffer.strip_suffix('\n').unwrap_or(&line_buffer);
let result = if line.starts_with("+STR") {
if line.starts_with("+STR") {
yaml_stream_start_event_initialize(&mut event, YAML_UTF8_ENCODING)
} else if line.starts_with("-STR") {
yaml_stream_end_event_initialize(&mut event)
@ -105,9 +105,6 @@ pub(crate) fn test_main(
break Err(format!("Unknown event: '{line}'").into());
};
if result.is_err() {
break Err("Memory error: Not enough memory for creating an event".into());
}
if let Err(err) = yaml_emitter_emit(&mut emitter, event) {
break Err(err.into());
}

View file

@ -2,7 +2,7 @@ use alloc::string::String;
use crate::api::OUTPUT_BUFFER_SIZE;
use crate::macros::{
is_alpha, is_ascii, is_blank, is_blankz, is_bom, is_break, is_printable, is_space,
is_alpha, is_ascii, is_blank, is_blankz, is_bom, is_break, is_breakz, is_printable, is_space,
};
use crate::ops::ForceMul as _;
use crate::yaml::YamlEventData;
@ -338,7 +338,7 @@ fn yaml_emitter_emit_document_start(
];
let mut implicit = implicit;
if let Some(version_directive) = version_directive {
yaml_emitter_analyze_version_directive(emitter, &version_directive)?;
yaml_emitter_analyze_version_directive(emitter, version_directive)?;
}
for tag_directive in tag_directives {
yaml_emitter_analyze_tag_directive(emitter, tag_directive)?;
@ -477,7 +477,10 @@ fn yaml_emitter_emit_flow_mapping_key(
emitter.flow_level += 1;
}
if let YamlEventData::MappingEnd = &event.data {
assert!(!emitter.indents.is_empty(), "emitter.indents should not be empty");
assert!(
!emitter.indents.is_empty(),
"emitter.indents should not be empty"
);
emitter.flow_level -= 1;
emitter.indent = emitter.indents.pop().unwrap();
if emitter.canonical && !first {
@ -720,16 +723,8 @@ fn yaml_emitter_check_empty_sequence(emitter: &yaml_emitter_t, event: &yaml_even
if emitter.events.is_empty() {
return false;
}
let start = if let YamlEventData::SequenceStart { .. } = event.data {
true
} else {
false
};
let end = if let YamlEventData::SequenceEnd = emitter.events[0].data {
true
} else {
false
};
let start = matches!(event.data, YamlEventData::SequenceStart { .. });
let end = matches!(emitter.events[0].data, YamlEventData::SequenceEnd);
start && end
}
@ -737,16 +732,8 @@ fn yaml_emitter_check_empty_mapping(emitter: &yaml_emitter_t, event: &yaml_event
if emitter.events.is_empty() {
return false;
}
let start = if let YamlEventData::MappingStart { .. } = event.data {
true
} else {
false
};
let end = if let YamlEventData::MappingEnd = emitter.events[0].data {
true
} else {
false
};
let start = matches!(event.data, YamlEventData::MappingStart { .. });
let end = matches!(emitter.events[0].data, YamlEventData::MappingEnd);
start && end
}
@ -762,16 +749,11 @@ fn yaml_emitter_check_simple_key(
} = analysis;
let mut length = anchor.as_ref().map_or(0, |a| a.anchor.len())
+ tag
.as_ref()
.map_or(0, |t| t.handle.len() + t.suffix.len());
+ tag.as_ref().map_or(0, |t| t.handle.len() + t.suffix.len());
match event.data {
YamlEventData::Alias { .. } => {
length = analysis
.anchor
.as_ref()
.map_or(0, |a| a.anchor.len());
length = analysis.anchor.as_ref().map_or(0, |a| a.anchor.len());
}
YamlEventData::Scalar { .. } => {
let Some(scalar) = scalar else {
@ -921,39 +903,27 @@ fn yaml_emitter_process_scalar(
) -> Result<(), EmitterError> {
match analysis.style {
YAML_PLAIN_SCALAR_STYLE => {
yaml_emitter_write_plain_scalar(
emitter,
analysis.value,
!emitter.simple_key_context,
)
}
YAML_SINGLE_QUOTED_SCALAR_STYLE => {
yaml_emitter_write_single_quoted_scalar(
emitter,
analysis.value,
!emitter.simple_key_context,
)
}
YAML_DOUBLE_QUOTED_SCALAR_STYLE => {
yaml_emitter_write_double_quoted_scalar(
emitter,
analysis.value,
!emitter.simple_key_context,
)
}
YAML_LITERAL_SCALAR_STYLE => {
yaml_emitter_write_literal_scalar(emitter, analysis.value)
}
YAML_FOLDED_SCALAR_STYLE => {
yaml_emitter_write_folded_scalar(emitter, analysis.value)
yaml_emitter_write_plain_scalar(emitter, analysis.value, !emitter.simple_key_context)
}
YAML_SINGLE_QUOTED_SCALAR_STYLE => yaml_emitter_write_single_quoted_scalar(
emitter,
analysis.value,
!emitter.simple_key_context,
),
YAML_DOUBLE_QUOTED_SCALAR_STYLE => yaml_emitter_write_double_quoted_scalar(
emitter,
analysis.value,
!emitter.simple_key_context,
),
YAML_LITERAL_SCALAR_STYLE => yaml_emitter_write_literal_scalar(emitter, analysis.value),
YAML_FOLDED_SCALAR_STYLE => yaml_emitter_write_folded_scalar(emitter, analysis.value),
YAML_ANY_SCALAR_STYLE => unreachable!("No scalar style chosen"),
}
}
fn yaml_emitter_analyze_version_directive(
emitter: &mut yaml_emitter_t,
version_directive: &yaml_version_directive_t,
version_directive: yaml_version_directive_t,
) -> Result<(), EmitterError> {
if version_directive.major != 1 || version_directive.minor != 1 && version_directive.minor != 2
{
@ -1570,7 +1540,7 @@ fn yaml_emitter_write_double_quoted_scalar(
(b'U', 8)
};
PUT(emitter, prefix)?;
let mut k = ((width - 1) * 4) as i32;
let mut k = (width - 1) * 4;
let value_0 = ch as u32;
while k >= 0 {
let digit = (value_0 >> k) & 0x0F;
@ -1640,10 +1610,7 @@ fn yaml_emitter_write_block_scalar_hints(
if !is_break(ch) {
chomp_hint = Some("-");
} else if next.is_none() {
chomp_hint = Some("+");
emitter.open_ended = 2;
} else if is_break(next) {
} else if is_breakz(next) {
chomp_hint = Some("+");
emitter.open_ended = 2;
}

View file

@ -11,7 +11,7 @@
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_ptr_alignment,
// clippy::cast_ptr_alignment,
clippy::cast_sign_loss,
clippy::collapsible_if,
clippy::doc_markdown,
@ -20,22 +20,18 @@
clippy::items_after_statements,
clippy::let_underscore_untyped,
clippy::manual_range_contains,
clippy::manual_swap,
clippy::missing_panics_doc,
clippy::missing_safety_doc,
clippy::missing_errors_doc,
clippy::module_name_repetitions,
clippy::must_use_candidate,
clippy::needless_pass_by_value,
clippy::nonminimal_bool,
clippy::ptr_as_ptr,
clippy::redundant_else,
clippy::similar_names,
clippy::single_match,
clippy::single_match_else,
clippy::struct_excessive_bools,
clippy::too_many_arguments,
clippy::too_many_lines,
clippy::unnecessary_cast,
clippy::unreadable_literal,
clippy::while_immutable_condition, // https://github.com/rust-lang/rust-clippy/issues/3548
clippy::unnecessary_wraps
)]
extern crate alloc;
@ -260,9 +256,9 @@ foo: bar
yaml_emitter_set_output_string(&mut emitter, &mut output);
let mut event = yaml_event_t::default();
yaml_stream_start_event_initialize(&mut event, YAML_UTF8_ENCODING).unwrap();
yaml_stream_start_event_initialize(&mut event, YAML_UTF8_ENCODING);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_document_start_event_initialize(&mut event, None, &[], true).unwrap();
yaml_document_start_event_initialize(&mut event, None, &[], true);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_scalar_event_initialize(
&mut event,
@ -272,12 +268,11 @@ foo: bar
true,
true,
YAML_PLAIN_SCALAR_STYLE,
)
.unwrap();
);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_document_end_event_initialize(&mut event, true).unwrap();
yaml_document_end_event_initialize(&mut event, true);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_stream_end_event_initialize(&mut event).unwrap();
yaml_stream_end_event_initialize(&mut event);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
assert_eq!(

View file

@ -67,7 +67,7 @@ fn yaml_parser_update_raw_buffer(parser: &mut yaml_parser_t) -> Result<(), Reade
.expect("non-null read handler")
.read(write_to)?;
let valid_size = len_before + size_read as usize;
let valid_size = len_before + size_read;
parser.raw_buffer.truncate(valid_size);
if size_read == 0 {
parser.eof = true;
@ -107,13 +107,12 @@ fn read_char_utf8(raw: &mut VecDeque<u8>) -> Option<Result<char, Utf8Error>> {
if raw.len() < width {
return Some(Err(Utf8Error::Incomplete));
}
for i in 1..width {
let trailing = raw[i];
for (i, trailing) in raw.iter().enumerate().take(width).skip(1) {
if trailing & 0xc0 != 0x80 {
return Some(Err(Utf8Error::InvalidTrailingOctet(i)));
}
value <<= 6;
value += trailing as u32 & 0x3f;
value += *trailing as u32 & 0x3f;
}
if !(width == 1
|| width == 2 && value >= 0x80
@ -246,7 +245,6 @@ pub(crate) fn yaml_parser_update_buffer(
);
} else {
// Read more
();
}
}
Some(Err(Utf8Error::InvalidLeadingOctet)) => {
@ -305,7 +303,6 @@ pub(crate) fn yaml_parser_update_buffer(
);
} else {
// Read more
();
}
}
Some(Err(Utf16Error::UnexpectedLowSurrogateArea(value))) => {

View file

@ -339,7 +339,10 @@ fn yaml_parser_increase_flow_level(parser: &mut yaml_parser_t) -> Result<(), Sca
},
};
parser.simple_keys.push(empty_simple_key);
assert!(!(parser.flow_level == libc::c_int::MAX), "parser.flow_level integer overflow");
assert!(
!(parser.flow_level == libc::c_int::MAX),
"parser.flow_level integer overflow"
);
parser.flow_level += 1;
Ok(())
}
@ -363,7 +366,10 @@ fn yaml_parser_roll_indent(
}
if (parser.indent as libc::c_long) < column {
parser.indents.push(parser.indent);
assert!(!(column > ptrdiff_t::from(libc::c_int::MAX)), "integer overflow");
assert!(
!(column > ptrdiff_t::from(libc::c_int::MAX)),
"integer overflow"
);
parser.indent = column as libc::c_int;
let token = yaml_token_t {
data,
@ -1116,57 +1122,55 @@ fn yaml_parser_scan_tag_uri(
let mut length = head.len();
let mut string = String::new();
loop {
if length > 1 {
string = String::from(&head[1..]);
}
CACHE(parser, 1_u64)?;
if length > 1 {
string = String::from(&head[1..]);
}
CACHE(parser, 1_u64)?;
while IS_ALPHA!(parser.buffer)
|| CHECK!(parser.buffer, ';')
|| CHECK!(parser.buffer, '/')
|| CHECK!(parser.buffer, '?')
|| CHECK!(parser.buffer, ':')
|| CHECK!(parser.buffer, '@')
|| CHECK!(parser.buffer, '&')
|| CHECK!(parser.buffer, '=')
|| CHECK!(parser.buffer, '+')
|| CHECK!(parser.buffer, '$')
|| CHECK!(parser.buffer, '.')
|| CHECK!(parser.buffer, '%')
|| CHECK!(parser.buffer, '!')
|| CHECK!(parser.buffer, '~')
|| CHECK!(parser.buffer, '*')
|| CHECK!(parser.buffer, '\'')
|| CHECK!(parser.buffer, '(')
|| CHECK!(parser.buffer, ')')
|| uri_char
&& (CHECK!(parser.buffer, ',')
|| CHECK!(parser.buffer, '[')
|| CHECK!(parser.buffer, ']'))
{
if CHECK!(parser.buffer, '%') {
yaml_parser_scan_uri_escapes(parser, directive, start_mark, &mut string)?;
} else {
READ_STRING(parser, &mut string);
}
length = length.force_add(1);
CACHE(parser, 1_u64)?;
}
if length == 0 {
return yaml_parser_set_scanner_error(
parser,
if directive {
"while parsing a %TAG directive"
} else {
"while parsing a tag"
},
start_mark,
"did not find expected tag URI",
);
while IS_ALPHA!(parser.buffer)
|| CHECK!(parser.buffer, ';')
|| CHECK!(parser.buffer, '/')
|| CHECK!(parser.buffer, '?')
|| CHECK!(parser.buffer, ':')
|| CHECK!(parser.buffer, '@')
|| CHECK!(parser.buffer, '&')
|| CHECK!(parser.buffer, '=')
|| CHECK!(parser.buffer, '+')
|| CHECK!(parser.buffer, '$')
|| CHECK!(parser.buffer, '.')
|| CHECK!(parser.buffer, '%')
|| CHECK!(parser.buffer, '!')
|| CHECK!(parser.buffer, '~')
|| CHECK!(parser.buffer, '*')
|| CHECK!(parser.buffer, '\'')
|| CHECK!(parser.buffer, '(')
|| CHECK!(parser.buffer, ')')
|| uri_char
&& (CHECK!(parser.buffer, ',')
|| CHECK!(parser.buffer, '[')
|| CHECK!(parser.buffer, ']'))
{
if CHECK!(parser.buffer, '%') {
yaml_parser_scan_uri_escapes(parser, directive, start_mark, &mut string)?;
} else {
return Ok(string);
READ_STRING(parser, &mut string);
}
length = length.force_add(1);
CACHE(parser, 1_u64)?;
}
if length == 0 {
yaml_parser_set_scanner_error(
parser,
if directive {
"while parsing a %TAG directive"
} else {
"while parsing a tag"
},
start_mark,
"did not find expected tag URI",
)
} else {
Ok(string)
}
}
@ -1460,7 +1464,6 @@ fn yaml_parser_scan_flow_scalar(
token: &mut yaml_token_t,
single: bool,
) -> Result<(), ScannerError> {
let mut string = String::new();
let mut leading_break = String::new();
let mut trailing_breaks = String::new();
@ -1613,10 +1616,8 @@ fn yaml_parser_scan_flow_scalar(
"did not find expected hexdecimal number",
);
} else {
value =
(value << 4)
.force_add(AS_HEX_AT!(parser.buffer, k as usize)
as libc::c_uint);
value = (value << 4)
.force_add(AS_HEX_AT!(parser.buffer, k as usize));
k = k.force_add(1);
}
}