diff --git a/examples/extract.rs b/examples/extract.rs index 05c5a4aa..b02eb4cd 100644 --- a/examples/extract.rs +++ b/examples/extract.rs @@ -59,5 +59,6 @@ fn real_main() -> i32 { } } } - return 0; + + 0 } diff --git a/examples/extract_lorem.rs b/examples/extract_lorem.rs index 89e33ef9..a34a04f4 100644 --- a/examples/extract_lorem.rs +++ b/examples/extract_lorem.rs @@ -27,5 +27,5 @@ fn real_main() -> i32 { file.read_to_string(&mut contents).unwrap(); println!("{}", contents); - return 0; + 0 } diff --git a/examples/file_info.rs b/examples/file_info.rs index 315b5c38..824278df 100644 --- a/examples/file_info.rs +++ b/examples/file_info.rs @@ -49,5 +49,6 @@ fn real_main() -> i32 { ); } } - return 0; + + 0 } diff --git a/examples/stdin_info.rs b/examples/stdin_info.rs index 606944ce..10d7aa8b 100644 --- a/examples/stdin_info.rs +++ b/examples/stdin_info.rs @@ -30,5 +30,6 @@ fn real_main() -> i32 { } } } - return 0; + + 0 } diff --git a/examples/write_dir.rs b/examples/write_dir.rs index 793bd6ba..6aaaed3a 100644 --- a/examples/write_dir.rs +++ b/examples/write_dir.rs @@ -54,7 +54,7 @@ fn real_main() -> i32 { } } - return 0; + 0 } fn zip_dir( diff --git a/examples/write_sample.rs b/examples/write_sample.rs index 4ef5ce34..163bfe14 100644 --- a/examples/write_sample.rs +++ b/examples/write_sample.rs @@ -18,7 +18,7 @@ fn real_main() -> i32 { Err(e) => println!("Error: {:?}", e), } - return 0; + 0 } fn doit(filename: &str) -> zip::result::ZipResult<()> { diff --git a/src/read.rs b/src/read.rs index 7b503ed6..6704c070 100644 --- a/src/read.rs +++ b/src/read.rs @@ -189,11 +189,11 @@ fn make_crypto_reader<'a>( Ok(Ok(reader)) } -fn make_reader<'a>( +fn make_reader( compression_method: CompressionMethod, crc32: u32, - reader: CryptoReader<'a>, -) -> ZipFileReader<'a> { + reader: CryptoReader, +) -> ZipFileReader { match compression_method { CompressionMethod::Stored => ZipFileReader::Stored(Crc32Reader::new(reader, crc32)), #[cfg(any( @@ -303,7 +303,7 @@ impl ZipArchive { let directory_start = footer .central_directory_offset .checked_add(archive_offset) - .ok_or_else(|| { + .ok_or({ ZipError::InvalidArchive("Invalid central directory size or offset") })?; @@ -332,7 +332,7 @@ impl ZipArchive { let mut files = Vec::new(); let mut names_map = HashMap::new(); - if let Err(_) = reader.seek(io::SeekFrom::Start(directory_start)) { + if reader.seek(io::SeekFrom::Start(directory_start)).is_err() { return Err(ZipError::InvalidArchive( "Could not seek to start of central directory", )); @@ -457,14 +457,14 @@ impl ZipArchive { } /// Get a contained file by index - pub fn by_index<'a>(&'a mut self, file_number: usize) -> ZipResult> { + pub fn by_index(&mut self, file_number: usize) -> ZipResult { Ok(self .by_index_with_optional_password(file_number, None)? .unwrap()) } /// Get a contained file by index without decompressing it - pub fn by_index_raw<'a>(&'a mut self, file_number: usize) -> ZipResult> { + pub fn by_index_raw(&mut self, file_number: usize) -> ZipResult { let reader = &mut self.reader; self.files .get_mut(file_number) @@ -1020,7 +1020,7 @@ mod test { let mut v = Vec::new(); v.extend_from_slice(include_bytes!("../tests/data/zip64_demo.zip")); let reader = ZipArchive::new(io::Cursor::new(v)).unwrap(); - assert!(reader.len() == 1); + assert_eq!(reader.len(), 1); } #[test] @@ -1031,7 +1031,7 @@ mod test { let mut v = Vec::new(); v.extend_from_slice(include_bytes!("../tests/data/mimetype.zip")); let mut reader = ZipArchive::new(io::Cursor::new(v)).unwrap(); - assert!(reader.comment() == b""); + assert_eq!(reader.comment(), b""); assert_eq!(reader.by_index(0).unwrap().central_header_start(), 77); } @@ -1044,9 +1044,8 @@ mod test { v.extend_from_slice(include_bytes!("../tests/data/mimetype.zip")); let mut reader = io::Cursor::new(v); loop { - match read_zipfile_from_stream(&mut reader).unwrap() { - None => break, - _ => (), + if read_zipfile_from_stream(&mut reader).unwrap().is_none() { + break; } } } @@ -1089,7 +1088,7 @@ mod test { assert_eq!(buf1, buf2); assert_eq!(buf3, buf4); - assert!(buf1 != buf3); + assert_ne!(buf1, buf3); } #[test] diff --git a/src/types.rs b/src/types.rs index f99bc06b..529844e9 100644 --- a/src/types.rs +++ b/src/types.rs @@ -65,7 +65,7 @@ impl DateTime { let seconds = (timepart & 0b0000000000011111) << 1; let minutes = (timepart & 0b0000011111100000) >> 5; let hours = (timepart & 0b1111100000000000) >> 11; - let days = (datepart & 0b0000000000011111) >> 0; + let days = datepart & 0b0000000000011111; let months = (datepart & 0b0000000111100000) >> 5; let years = (datepart & 0b1111111000000000) >> 9; @@ -256,10 +256,7 @@ impl ZipFileData { ::std::path::Path::new(&filename) .components() - .filter(|component| match *component { - ::std::path::Component::Normal(..) => true, - _ => false, - }) + .filter(|component| matches!(*component, ::std::path::Component::Normal(..))) .fold(::std::path::PathBuf::new(), |mut path, ref cur| { path.push(cur.as_os_str()); path @@ -329,15 +326,15 @@ mod test { use super::DateTime; let dt = DateTime::default(); assert_eq!(dt.timepart(), 0); - assert_eq!(dt.datepart(), 0b0000000_0001_00001); + assert_eq!(dt.datepart(), 0b0000_0000_0010_0001); } #[test] fn datetime_max() { use super::DateTime; let dt = DateTime::from_date_and_time(2107, 12, 31, 23, 59, 60).unwrap(); - assert_eq!(dt.timepart(), 0b10111_111011_11110); - assert_eq!(dt.datepart(), 0b1111111_1100_11111); + assert_eq!(dt.timepart(), 0b1011_1111_0111_1110); + assert_eq!(dt.datepart(), 0b1111_1111_1001_1111); } #[test] @@ -394,7 +391,7 @@ mod test { #[cfg(feature = "time")] assert_eq!( - format!("{}", dt.to_time().unwrap().format(&Rfc3339).unwrap()), + dt.to_time().unwrap().format(&Rfc3339).unwrap(), "2018-11-17T10:38:30Z" ); } diff --git a/src/write.rs b/src/write.rs index 2f904a61..30cfa40b 100644 --- a/src/write.rs +++ b/src/write.rs @@ -237,7 +237,10 @@ impl ZipWriter { let (archive_offset, directory_start, number_of_files) = ZipArchive::get_directory_counts(&mut readwriter, &footer, cde_start_pos)?; - if let Err(_) = readwriter.seek(io::SeekFrom::Start(directory_start)) { + if readwriter + .seek(io::SeekFrom::Start(directory_start)) + .is_err() + { return Err(ZipError::InvalidArchive( "Could not seek to start of central directory", )); @@ -307,7 +310,7 @@ impl ZipWriter { { self.finish_file()?; - let raw_values = raw_values.unwrap_or_else(|| ZipRawValues { + let raw_values = raw_values.unwrap_or(ZipRawValues { crc32: 0, compressed_size: 0, uncompressed_size: 0, @@ -548,7 +551,7 @@ impl ZipWriter { } let file = self.files.last_mut().unwrap(); - validate_extra_data(&file)?; + validate_extra_data(file)?; if !self.writing_to_central_extra_field_only { let writer = self.inner.get_plain(); @@ -858,10 +861,7 @@ impl GenericZipWriter { } fn is_closed(&self) -> bool { - match *self { - GenericZipWriter::Closed => true, - _ => false, - } + matches!(*self, GenericZipWriter::Closed) } fn get_plain(&mut self) -> &mut W { @@ -935,7 +935,7 @@ fn write_local_file_header(writer: &mut T, file: &ZipFileData) -> ZipR writer.write_all(file.file_name.as_bytes())?; // zip64 extra field if file.large_file { - write_local_zip64_extra_field(writer, &file)?; + write_local_zip64_extra_field(writer, file)?; } Ok(()) @@ -1053,7 +1053,7 @@ fn validate_extra_data(file: &ZipFileData) -> ZipResult<()> { ))); } - while data.len() > 0 { + while !data.is_empty() { let left = data.len(); if left < 4 { return Err(ZipError::Io(io::Error::new( diff --git a/src/zipcrypto.rs b/src/zipcrypto.rs index 3196ea36..91d40395 100644 --- a/src/zipcrypto.rs +++ b/src/zipcrypto.rs @@ -47,7 +47,7 @@ impl ZipCryptoKeys { } fn crc32(crc: Wrapping, input: u8) -> Wrapping { - return (crc >> 8) ^ Wrapping(CRCTABLE[((crc & Wrapping(0xff)).0 as u8 ^ input) as usize]); + (crc >> 8) ^ Wrapping(CRCTABLE[((crc & Wrapping(0xff)).0 as u8 ^ input) as usize]) } } @@ -71,7 +71,7 @@ impl ZipCryptoReader { /// password byte sequence that is unrepresentable in UTF-8. pub fn new(file: R, password: &[u8]) -> ZipCryptoReader { let mut result = ZipCryptoReader { - file: file, + file, keys: ZipCryptoKeys::new(), }; @@ -129,11 +129,11 @@ pub struct ZipCryptoReaderValid { } impl std::io::Read for ZipCryptoReaderValid { - fn read(&mut self, mut buf: &mut [u8]) -> std::io::Result { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { // Note: There might be potential for optimization. Inspiration can be found at: // https://github.com/kornelski/7z/blob/master/CPP/7zip/Crypto/ZipCrypto.cpp - let result = self.reader.file.read(&mut buf); + let result = self.reader.file.read(buf); for byte in buf.iter_mut() { *byte = self.reader.keys.decrypt_byte(*byte); } diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs index baebd287..556f9abf 100644 --- a/tests/end_to_end.rs +++ b/tests/end_to_end.rs @@ -100,7 +100,7 @@ fn read_zip(zip_file: R) -> zip::result::ZipResult>(); assert_eq!(file_names, expected_file_names); @@ -134,7 +134,7 @@ fn check_zip_contents(zip_file: &mut Cursor>, name: &str) { fn check_zip_file_contents(archive: &mut zip::ZipArchive, name: &str) { let file_contents: String = read_zip_file(archive, name).unwrap(); - assert!(file_contents.as_bytes() == LOREM_IPSUM); + assert_eq!(file_contents.as_bytes(), LOREM_IPSUM); } const LOREM_IPSUM : &'static [u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tellus elit, tristique vitae mattis egestas, ultricies vitae risus. Quisque sit amet quam ut urna aliquet @@ -144,7 +144,7 @@ vitae tristique consectetur, neque lectus pulvinar dui, sed feugiat purus diam i inceptos himenaeos. Maecenas feugiat velit in ex ultrices scelerisque id id neque. "; -const EXTRA_DATA: &'static [u8] = b"Extra Data"; +const EXTRA_DATA: &[u8] = b"Extra Data"; const ENTRY_NAME: &str = "test/lorem_ipsum.txt";