Skip to content

Commit

Permalink
Clippy suggestions (#470)
Browse files Browse the repository at this point in the history
Incorporates `clippy` feedback on `src` and `tests`.
  • Loading branch information
zslayton authored Feb 21, 2023
1 parent c74a4e7 commit 396a973
Show file tree
Hide file tree
Showing 43 changed files with 278 additions and 380 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ jobs:
with:
# Adding comments to the PR requires the GITHUB_TOKEN secret.
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features
args: --all-features --tests

15 changes: 4 additions & 11 deletions src/binary/binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ impl<W: Write> IonWriter for BinaryWriter<W> {
if self.symbol_table.sid_is_valid(symbol_id) {
symbol_id
} else {
panic!(
"Cannot set symbol ID ${} as annotation. It is undefined.",
symbol_id
);
panic!("Cannot set symbol ID ${symbol_id} as annotation. It is undefined.");
}
}
RawSymbolTokenRef::Text(text) => self.get_or_create_symbol_id(text),
Expand All @@ -144,8 +141,7 @@ impl<W: Write> IonWriter for BinaryWriter<W> {
symbol_id
} else {
return illegal_operation(format!(
"Cannot write symbol ID ${} as a symbol value. It is undefined.",
symbol_id
"Cannot write symbol ID ${symbol_id} as a symbol value. It is undefined."
));
}
}
Expand All @@ -160,10 +156,7 @@ impl<W: Write> IonWriter for BinaryWriter<W> {
if self.symbol_table.sid_is_valid(symbol_id) {
symbol_id
} else {
panic!(
"Cannot set symbol ID ${} as field name. It is undefined.",
symbol_id
);
panic!("Cannot set symbol ID ${symbol_id} as field name. It is undefined.");
}
}
RawSymbolTokenRef::Text(text) => self.get_or_create_symbol_id(text),
Expand Down Expand Up @@ -235,7 +228,7 @@ mod tests {
fn intern_annotations() -> IonResult<()> {
let mut buffer = Vec::new();
let mut binary_writer = BinaryWriterBuilder::new().build(&mut buffer)?;
binary_writer.set_annotations(&["foo", "bar"]);
binary_writer.set_annotations(["foo", "bar"]);
binary_writer.write_i64(5)?;
binary_writer.flush()?;

Expand Down
3 changes: 1 addition & 2 deletions src/binary/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ impl DecodedInt {
});
} else if length > MAX_INT_SIZE_IN_BYTES {
return decoding_error(format!(
"Found a {}-byte Int. Max supported size is {} bytes.",
length, MAX_INT_SIZE_IN_BYTES
"Found a {length}-byte Int. Max supported size is {MAX_INT_SIZE_IN_BYTES} bytes."
));
}

Expand Down
17 changes: 6 additions & 11 deletions src/binary/non_blocking/binary_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<A: AsRef<[u8]>> BinaryBuffer<A> {
self.consume(IVM.len());
Ok(version)
}
invalid_ivm => decoding_error(format!("invalid IVM: {:?}", invalid_ivm)),
invalid_ivm => decoding_error(format!("invalid IVM: {invalid_ivm:?}")),
}
}

Expand Down Expand Up @@ -243,8 +243,7 @@ impl<A: AsRef<[u8]>> BinaryBuffer<A> {

if encoded_size_in_bytes > MAX_ENCODED_SIZE_IN_BYTES {
return decoding_error(format!(
"Found a {}-byte VarInt. Max supported size is {} bytes.",
encoded_size_in_bytes, MAX_ENCODED_SIZE_IN_BYTES
"Found a {encoded_size_in_bytes}-byte VarInt. Max supported size is {MAX_ENCODED_SIZE_IN_BYTES} bytes."
));
}

Expand Down Expand Up @@ -308,8 +307,7 @@ impl<A: AsRef<[u8]>> BinaryBuffer<A> {
// compile to a non-trivial number of instructions.
fn value_too_large<T>(label: &str, length: usize, max_length: usize) -> IonResult<T> {
decoding_error(format!(
"found {} that was too large; size = {}, max size = {}",
label, length, max_length
"found {label} that was too large; size = {length}, max size = {max_length}"
))
}

Expand All @@ -323,8 +321,7 @@ impl<A: AsRef<[u8]>> BinaryBuffer<A> {
return Ok(DecodedInt::new(Integer::I64(0), false, 0));
} else if length > MAX_INT_SIZE_IN_BYTES {
return decoding_error(format!(
"Found a {}-byte Int. Max supported size is {} bytes.",
length, MAX_INT_SIZE_IN_BYTES
"Found a {length}-byte Int. Max supported size is {MAX_INT_SIZE_IN_BYTES} bytes."
));
}

Expand Down Expand Up @@ -594,7 +591,7 @@ mod tests {
let mut buffer = BinaryBuffer::new(&[0b0111_1001, 0b0000_1111]);
match buffer.read_var_uint() {
Err(IonError::Incomplete { .. }) => Ok(()),
other => panic!("expected IonError::Incomplete, but found: {:?}", other),
other => panic!("expected IonError::Incomplete, but found: {other:?}"),
}
}

Expand Down Expand Up @@ -725,9 +722,7 @@ mod tests {
#[test]
fn test_read_uint_too_large() {
let mut buffer = Vec::with_capacity(MAX_UINT_SIZE_IN_BYTES + 1);
for _ in 0..(MAX_UINT_SIZE_IN_BYTES + 1) {
buffer.push(1);
}
buffer.resize(MAX_UINT_SIZE_IN_BYTES + 1, 1);
let mut buffer = BinaryBuffer::new(buffer);
let _uint = buffer
.read_uint(buffer.remaining())
Expand Down
20 changes: 9 additions & 11 deletions src/binary/non_blocking/raw_binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,7 @@ impl<A: AsRef<[u8]>> IonReader for RawBinaryBufferReader<A> {
Ok(ion_type)
} else {
illegal_operation(format!(
"cannot read null; reader is currently positioned on a non-null {}",
ion_type
"cannot read null; reader is currently positioned on a non-null {ion_type}"
))
};
}
Expand Down Expand Up @@ -1237,13 +1236,12 @@ impl<'a, A: AsRef<[u8]>> TxReader<'a, A> {
fn read_ivm(&mut self) -> IonResult<RawStreamItem> {
if let Some(container) = self.parent {
return decoding_error(format!(
"found an Ion version marker inside a {:?}",
container
"found an Ion version marker inside a {container:?}"
));
};
let (major, minor) = self.tx_buffer.read_ivm()?;
if !matches!((major, minor), (1, 0)) {
return decoding_error(format!("unsupported Ion version {:X}.{:X}", major, minor));
return decoding_error(format!("unsupported Ion version {major:X}.{minor:X}"));
}
*self.state = ReaderState::OnIvm;
Ok(RawStreamItem::VersionMarker(major, minor))
Expand Down Expand Up @@ -1293,23 +1291,23 @@ mod tests {
if let Err(IonError::Incomplete { .. }) = result {
// do nothing
} else {
panic!("expected incomplete, found: {:?}", result)
panic!("expected incomplete, found: {result:?}")
}
}

fn expect_eof(result: IonResult<RawStreamItem>) {
if let Ok(RawStreamItem::Nothing) = result {
// do nothing
} else {
panic!("expected RawStreamItem::Nothing, found: {:?}", result)
panic!("expected RawStreamItem::Nothing, found: {result:?}")
}
}

fn expect_value(result: IonResult<RawStreamItem>, ion_type: IonType) {
if let Ok(RawStreamItem::Value(result_ion_type)) = result {
assert_eq!(result_ion_type, ion_type);
} else {
panic!("expected a value, but got: {:?}", result);
panic!("expected a value, but got: {result:?}");
}
}

Expand Down Expand Up @@ -1604,13 +1602,13 @@ mod tests {
let mut reader = RawBinaryBufferReader::new(data);

expect_value(reader.next(), IonType::Integer);
expect_annotations(&reader, &[4]);
expect_annotations(&reader, [4]);

expect_value(reader.next(), IonType::Integer);
expect_annotations(&reader, &[5]);
expect_annotations(&reader, [5]);

expect_value(reader.next(), IonType::Integer);
expect_annotations(&reader, &[6, 7, 8]);
expect_annotations(&reader, [6, 7, 8]);
// Nothing else in the buffer
expect_eof(reader.next());
Ok(())
Expand Down
30 changes: 13 additions & 17 deletions src/binary/raw_binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<R: IonDataSource> IonReader for RawBinaryReader<R> {

if nop_range.end > container_range.end {
// This NOP is malformed, let's assemble data for error reporting
return decoding_error(&format!(
return decoding_error(format!(
"{bytes}-byte NOP padding on byte range {nop_range:?} is {over} \
byte{s} past container content range {container_range:?}",
bytes = number_of_bytes,
Expand Down Expand Up @@ -362,7 +362,7 @@ impl<R: IonDataSource> IonReader for RawBinaryReader<R> {
None => return Ok(self.set_current_item(RawStreamItem::Nothing)),
};
if header.is_nop() {
return decoding_error(&format!(
return decoding_error(format!(
"The annotation wrapper starting at byte {} contains NOP padding, which is illegal.",
self.cursor.bytes_read
));
Expand Down Expand Up @@ -455,9 +455,8 @@ impl<R: IonDataSource> IonReader for RawBinaryReader<R> {
match representation {
0 => Ok(false),
1 => Ok(true),
_ => decoding_error(&format!(
"Found a boolean value with an illegal representation: {}",
representation
_ => decoding_error(format!(
"Found a boolean value with an illegal representation: {representation}"
)),
}
}
Expand Down Expand Up @@ -505,9 +504,8 @@ impl<R: IonDataSource> IonReader for RawBinaryReader<R> {
4 => f64::from(BigEndian::read_f32(buffer)),
8 => BigEndian::read_f64(buffer),
_ => {
return decoding_error(&format!(
"Encountered an illegal value for a Float length: {}",
number_of_bytes
return decoding_error(format!(
"Encountered an illegal value for a Float length: {number_of_bytes}"
))
}
};
Expand Down Expand Up @@ -547,9 +545,8 @@ impl<R: IonDataSource> IonReader for RawBinaryReader<R> {
{
self.map_string_bytes(|buffer| match std::str::from_utf8(buffer) {
Ok(utf8_text) => Ok(f(utf8_text)),
Err(utf8_error) => decoding_error(&format!(
"The requested string was not valid UTF-8: {:?}",
utf8_error
Err(utf8_error) => decoding_error(format!(
"The requested string was not valid UTF-8: {utf8_error:?}"
)),
})?
}
Expand Down Expand Up @@ -914,14 +911,13 @@ where
let (major, minor) = self.read_slice(3, |bytes| match *bytes {
[major, minor, 0xEA] => Ok((major, minor)),
[major, minor, other] => decoding_error(format!(
"Illegal IVM: 0xE0 0x{:X} 0x{:X} 0x{:X}",
major, minor, other
"Illegal IVM: 0xE0 0x{major:X} 0x{minor:X} 0x{other:X}"
)),
_ => unreachable!("read_slice did not return the requested number of bytes"),
})?;

if !matches!((major, minor), (1, 0)) {
decoding_error(format!("Unsupported Ion version {:X}.{:X}", major, minor))
decoding_error(format!("Unsupported Ion version {major:X}.{minor:X}"))
} else {
self.cursor.ion_version = (major, minor);
Ok(RawStreamItem::VersionMarker(major, minor))
Expand Down Expand Up @@ -1263,15 +1259,15 @@ mod tests {
fn test_read_bool_false() -> IonResult<()> {
let mut cursor = ion_cursor_for(&[0x10]);
assert_eq!(cursor.next()?, Value(IonType::Boolean));
assert_eq!(cursor.read_bool()?, false);
assert!(!(cursor.read_bool()?));
Ok(())
}

#[test]
fn test_read_bool_true() -> IonResult<()> {
let mut cursor = ion_cursor_for(&[0x11]);
assert_eq!(cursor.next()?, Value(IonType::Boolean));
assert_eq!(cursor.read_bool()?, true);
assert!(cursor.read_bool()?);
Ok(())
}

Expand Down Expand Up @@ -1771,7 +1767,7 @@ mod tests {
cursor.raw_value_bytes(),
Some(&ion_data[37..37] /* empty */)
);
assert_eq!(cursor.read_bool()?, false);
assert!(!(cursor.read_bool()?));

Ok(())
}
Expand Down
33 changes: 14 additions & 19 deletions src/binary/raw_binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,7 @@ impl<W: Write> RawBinaryWriter<W> {
let symbol_id = match annotation.as_raw_symbol_token_ref() {
RawSymbolTokenRef::SymbolId(symbol_id) => symbol_id,
RawSymbolTokenRef::Text(text) => panic!(
"The RawBinaryWriter can only accept symbol ID annotations, not text ('{}').",
text
"The RawBinaryWriter can only accept symbol ID annotations, not text ('{text}')."
),
};
self.annotations_all_levels.push(symbol_id);
Expand Down Expand Up @@ -776,8 +775,7 @@ impl<W: Write> IonWriter for RawBinaryWriter<W> {
match name.as_raw_symbol_token_ref() {
RawSymbolTokenRef::SymbolId(sid) => self.set_field_id(sid),
RawSymbolTokenRef::Text(text) => panic!(
"The RawBinaryWriter can only accept Symbol ID field names, not text ('{}').",
text
"The RawBinaryWriter can only accept Symbol ID field names, not text ('{text}')."
),
}
}
Expand Down Expand Up @@ -1125,14 +1123,11 @@ mod writer_tests {
expected_value: T,
) {
let next = reader.next().unwrap_or_else(|_| {
panic!(
"Expected to read {:?}, but the stream was empty.",
expected_value
)
panic!("Expected to read {expected_value:?}, but the stream was empty.")
});
assert_eq!(next, StreamItem::Value(ion_type));
let value = read_fn(reader)
.unwrap_or_else(|_| panic!("Failed to read in expected value: {:?}", expected_value));
.unwrap_or_else(|_| panic!("Failed to read in expected value: {expected_value:?}"));
assert_eq!(value, expected_value);
}

Expand Down Expand Up @@ -1214,7 +1209,7 @@ mod writer_tests {

fn write_lst<W: Write>(writer: &mut RawBinaryWriter<W>, symbols: &[&str]) -> IonResult<()> {
// $ion_symbol_table::{symbols: ["your", "strings", "here"]}
writer.set_annotations(&[3]); // $ion_symbol_table
writer.set_annotations([3]); // $ion_symbol_table
writer.step_in(IonType::Struct)?;
writer.set_field_id(7); // symbols
writer.step_in(IonType::List)?;
Expand Down Expand Up @@ -1286,13 +1281,13 @@ mod writer_tests {
|writer| {
write_lst(writer, &["foo", "bar", "baz", "quux", "quuz", "waldo"])?;

writer.set_annotations(&[10]);
writer.set_annotations([10]);
writer.write_bool(true)?;

writer.set_annotations(&[11, 12]);
writer.set_annotations([11, 12]);
writer.write_i64(42)?;

writer.set_annotations(&[13, 14, 15]);
writer.set_annotations([13, 14, 15]);
writer.write_string("Hello")
},
|reader| {
Expand All @@ -1319,19 +1314,19 @@ mod writer_tests {
)?;

// foo::(true)
writer.set_annotations(&[10]);
writer.set_annotations([10]);
writer.step_in(IonType::SExpression)?;
writer.write_bool(true)?;
writer.step_out()?;

// bar::baz::[11]
writer.set_annotations(&[11, 12]);
writer.set_annotations([11, 12]);
writer.step_in(IonType::List)?;
writer.write_i64(11)?;
writer.step_out()?;

// quux::quuz::waldo::{gary: "foo"}
writer.set_annotations(&[13, 14, 15]);
writer.set_annotations([13, 14, 15]);
writer.step_in(IonType::Struct)?;
writer.set_field_id(16);
writer.write_string("foo")?;
Expand Down Expand Up @@ -1367,12 +1362,12 @@ mod writer_tests {
|writer| {
write_lst(writer, &["foo", "bar", "baz", "quux"])?;
// foo::{bar: baz::[quux::"quuz"]]}
writer.set_annotations(&[10]);
writer.set_annotations([10]);
writer.step_in(IonType::Struct)?;
writer.set_field_id(11);
writer.set_annotations(&[12]);
writer.set_annotations([12]);
writer.step_in(IonType::List)?;
writer.set_annotations(&[13]);
writer.set_annotations([13]);
writer.write_string("quuz")?;
writer.step_out()?; // End of list
writer.step_out() // End of struct
Expand Down
Loading

0 comments on commit 396a973

Please sign in to comment.