From 72921d03b6d8460b8567c6c0bcd6da5f88f16644 Mon Sep 17 00:00:00 2001 From: Ron Kuris Date: Tue, 28 Nov 2023 09:41:37 -0800 Subject: [PATCH] Unwrap warning addition (#384) --- growth-ring/Cargo.toml | 1 + growth-ring/examples/demo1.rs | 21 ++++++++++----------- growth-ring/src/lib.rs | 23 +++++++++++++++++++++++ growth-ring/src/wal.rs | 8 ++++++++ growth-ring/tests/common/mod.rs | 11 +++++++++++ 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/growth-ring/Cargo.toml b/growth-ring/Cargo.toml index 7a3ae56fe..603c85cac 100644 --- a/growth-ring/Cargo.toml +++ b/growth-ring/Cargo.toml @@ -34,4 +34,5 @@ path = "src/lib.rs" crate-type = ["dylib", "rlib", "staticlib"] [lints.clippy] +unwrap_used = "warn" missing_const_for_fn = "warn" diff --git a/growth-ring/examples/demo1.rs b/growth-ring/examples/demo1.rs index 474ef25a6..0c4616f9c 100644 --- a/growth-ring/examples/demo1.rs +++ b/growth-ring/examples/demo1.rs @@ -37,8 +37,8 @@ fn main() -> Result<(), WalError> { let mut loader = WalLoader::new(); loader.file_nbit(9).block_nbit(8); - let store = WalStoreImpl::new(wal_dir, true).unwrap(); - let mut wal = block_on(loader.load(store, recover, 0)).unwrap(); + let store = WalStoreImpl::new(wal_dir, true)?; + let mut wal = block_on(loader.load(store, recover, 0))?; for _ in 0..3 { let _ = test( ["hi", "hello", "lol"] @@ -55,8 +55,8 @@ fn main() -> Result<(), WalError> { ); } - let store = WalStoreImpl::new(wal_dir, false).unwrap(); - let mut wal = block_on(loader.load(store, recover, 0)).unwrap(); + let store = WalStoreImpl::new(wal_dir, false)?; + let mut wal = block_on(loader.load(store, recover, 0))?; for _ in 0..3 { let _ = test( vec![ @@ -69,8 +69,8 @@ fn main() -> Result<(), WalError> { ); } - let store = WalStoreImpl::new(wal_dir, false).unwrap(); - let mut wal = block_on(loader.load(store, recover, 100)).unwrap(); + let store = WalStoreImpl::new(wal_dir, false)?; + let mut wal = block_on(loader.load(store, recover, 100))?; let mut history = std::collections::VecDeque::new(); for _ in 0..3 { let mut ids = Vec::new(); @@ -94,17 +94,16 @@ fn main() -> Result<(), WalError> { ids.shuffle(&mut rng); for e in ids.chunks(20) { println!("peel(20)"); - futures::executor::block_on(wal.peel(e, 100)).unwrap(); + futures::executor::block_on(wal.peel(e, 100))?; } } for (rec, ans) in - block_on(wal.read_recent_records(100, &growthring::wal::RecoverPolicy::Strict)) - .unwrap() + block_on(wal.read_recent_records(100, &growthring::wal::RecoverPolicy::Strict))? .into_iter() .zip(history.into_iter().rev()) { - assert_eq!(std::str::from_utf8(&rec).unwrap(), &ans); - println!("{}", std::str::from_utf8(&rec).unwrap()); + assert_eq!(&String::from_utf8_lossy(&rec), &ans); + println!("{}", String::from_utf8_lossy(&rec)); } Ok(()) diff --git a/growth-ring/src/lib.rs b/growth-ring/src/lib.rs index 2df79968d..b27844f69 100644 --- a/growth-ring/src/lib.rs +++ b/growth-ring/src/lib.rs @@ -179,6 +179,7 @@ impl WalStore for WalStoreImpl { fn enumerate_files(&self) -> Result { let mut filenames = Vec::new(); + #[allow(clippy::unwrap_used)] for path in fs::read_dir(&self.root_dir)?.filter_map(|entry| entry.ok()) { filenames.push(path.path()); } @@ -198,6 +199,7 @@ mod tests { tokio::fs::remove_file(&walfile_path).await.ok(); + #[allow(clippy::unwrap_used)] let walfile = RawWalFile::open(walfile_path).await.unwrap(); let walfile_impl = WalFileImpl::from(walfile); @@ -211,9 +213,12 @@ mod tests { .chain(second_half.iter().copied()) .collect(); + #[allow(clippy::unwrap_used)] walfile_impl.write(0, data).await.unwrap(); + #[allow(clippy::unwrap_used)] walfile_impl.truncate(HALF_LENGTH).await.unwrap(); + #[allow(clippy::unwrap_used)] let result = walfile_impl.read(0, HALF_LENGTH).await.unwrap(); assert_eq!(result, Some(first_half.into())) @@ -227,17 +232,21 @@ mod tests { tokio::fs::remove_file(&walfile_path).await.ok(); + #[allow(clippy::unwrap_used)] let walfile = RawWalFile::open(walfile_path).await.unwrap(); let walfile_impl = WalFileImpl::from(walfile); + #[allow(clippy::unwrap_used)] walfile_impl .write(0, vec![1u8; LENGTH].into()) .await .unwrap(); + #[allow(clippy::unwrap_used)] walfile_impl.truncate(2 * LENGTH).await.unwrap(); + #[allow(clippy::unwrap_used)] let result = walfile_impl.read(LENGTH as u64, LENGTH).await.unwrap(); assert_eq!(result, Some(vec![0u8; LENGTH].into())) @@ -248,6 +257,7 @@ mod tests { let walfile = { let walfile_path = get_temp_walfile_path(file!(), line!()); tokio::fs::remove_file(&walfile_path).await.ok(); + #[allow(clippy::unwrap_used)] RawWalFile::open(walfile_path).await.unwrap() }; @@ -255,8 +265,10 @@ mod tests { let data: Vec = (0..=u8::MAX).collect(); + #[allow(clippy::unwrap_used)] walfile_impl.write(0, data.clone().into()).await.unwrap(); + #[allow(clippy::unwrap_used)] let result = walfile_impl.read(0, data.len()).await.unwrap(); assert_eq!(result, Some(data.into())); @@ -267,17 +279,21 @@ mod tests { let walfile = { let walfile_path = get_temp_walfile_path(file!(), line!()); tokio::fs::remove_file(&walfile_path).await.ok(); + #[allow(clippy::unwrap_used)] RawWalFile::open(walfile_path).await.unwrap() }; let walfile_impl = WalFileImpl::from(walfile); let data: Vec = (0..=u8::MAX).collect(); + #[allow(clippy::unwrap_used)] walfile_impl.write(0, data.clone().into()).await.unwrap(); let mid = data.len() / 2; let (start, end) = data.split_at(mid); + #[allow(clippy::unwrap_used)] let read_start_result = walfile_impl.read(0, mid).await.unwrap(); + #[allow(clippy::unwrap_used)] let read_end_result = walfile_impl.read(mid as u64, mid).await.unwrap(); assert_eq!(read_start_result, Some(start.into())); @@ -289,6 +305,7 @@ mod tests { let walfile = { let walfile_path = get_temp_walfile_path(file!(), line!()); tokio::fs::remove_file(&walfile_path).await.ok(); + #[allow(clippy::unwrap_used)] RawWalFile::open(walfile_path).await.unwrap() }; @@ -296,8 +313,10 @@ mod tests { let data: Vec = (0..=u8::MAX).collect(); + #[allow(clippy::unwrap_used)] walfile_impl.write(0, data.clone().into()).await.unwrap(); + #[allow(clippy::unwrap_used)] let result = walfile_impl .read((data.len() / 2) as u64, data.len()) .await @@ -313,6 +332,7 @@ mod tests { let walfile = { let walfile_path = get_temp_walfile_path(file!(), line!()); tokio::fs::remove_file(&walfile_path).await.ok(); + #[allow(clippy::unwrap_used)] RawWalFile::open(walfile_path).await.unwrap() }; @@ -320,11 +340,13 @@ mod tests { let data: Vec = (0..=u8::MAX).collect(); + #[allow(clippy::unwrap_used)] walfile_impl .write(OFFSET, data.clone().into()) .await .unwrap(); + #[allow(clippy::unwrap_used)] let result = walfile_impl .read(0, data.len() + OFFSET as usize) .await @@ -338,6 +360,7 @@ mod tests { assert_eq!(result, Some(data.into())); } + #[allow(clippy::unwrap_used)] fn get_temp_walfile_path(file: &str, line: u32) -> PathBuf { let path = option_env!("CARGO_TARGET_TMPDIR") .map(PathBuf::from) diff --git a/growth-ring/src/wal.rs b/growth-ring/src/wal.rs index 738a79a82..aa20681ea 100644 --- a/growth-ring/src/wal.rs +++ b/growth-ring/src/wal.rs @@ -289,8 +289,10 @@ impl> WalFilePool { }) } + #[allow(clippy::unwrap_used)] async fn read_header(&self) -> Result { let bytes = self.header_file.read(0, HEADER_SIZE).await?.unwrap(); + #[allow(clippy::unwrap_used)] let bytes: [u8; HEADER_SIZE] = (&*bytes).try_into().unwrap(); let header: Header = cast_slice(&bytes)[0]; Ok(header) @@ -450,6 +452,7 @@ impl> WalFilePool { let mut removes: Vec>>>> = Vec::new(); + #[allow(clippy::unwrap_used)] while state.pending_removal.len() > 1 { let (fid, counter) = state.pending_removal.front().unwrap(); @@ -707,6 +710,7 @@ impl> WalWriter { break; } + #[allow(clippy::unwrap_used)] let mut m = state.io_complete.pop().unwrap(); let block_remain = block_size - (m.end & (block_size - 1)); @@ -741,6 +745,7 @@ impl> WalWriter { self.file_pool.in_use_len() } + #[allow(clippy::unwrap_used)] pub async fn read_recent_records<'a>( &'a self, nrecords: usize, @@ -774,6 +779,7 @@ impl> WalWriter { for ring in rings.into_iter().rev() { let ring = ring.map_err(|_| WalError::Other("error mapping ring".to_string()))?; let (header, payload) = ring; + #[allow(clippy::unwrap_used)] let payload = payload.unwrap(); match header.rtype.try_into() { Ok(WalRingType::Full) => { @@ -853,6 +859,7 @@ pub struct WalLoader { } impl Default for WalLoader { + #[allow(clippy::unwrap_used)] fn default() -> Self { WalLoader { file_nbit: 22, // 4MB @@ -1316,6 +1323,7 @@ impl WalLoader { pub const CRC32: crc::Crc = crc::Crc::::new(&crc::CRC_32_CKSUM); #[cfg(test)] +#[allow(clippy::unwrap_used)] mod test { use super::*; use test_case::test_case; diff --git a/growth-ring/tests/common/mod.rs b/growth-ring/tests/common/mod.rs index d461c8216..4f308b355 100644 --- a/growth-ring/tests/common/mod.rs +++ b/growth-ring/tests/common/mod.rs @@ -261,15 +261,19 @@ impl PaintStrokes { assert!(raw.len() & 3 == 0); let is = std::mem::size_of::(); let (len_raw, mut rest) = raw.split_at(is); + #[allow(clippy::unwrap_used)] let len = u32::from_le_bytes(len_raw.try_into().unwrap()); let mut res = Vec::new(); for _ in 0..len { let (s_raw, rest1) = rest.split_at(is); let (e_raw, rest2) = rest1.split_at(is); let (c_raw, rest3) = rest2.split_at(is); + #[allow(clippy::unwrap_used)] res.push(( u32::from_le_bytes(s_raw.try_into().unwrap()), + #[allow(clippy::unwrap_used)] u32::from_le_bytes(e_raw.try_into().unwrap()), + #[allow(clippy::unwrap_used)] u32::from_le_bytes(c_raw.try_into().unwrap()), )); rest = rest3 @@ -390,6 +394,7 @@ impl Canvas { return None; } let idx = rng.gen_range(0..self.queue.len()); + #[allow(clippy::unwrap_used)] let (pos, _) = self.queue.get_index(idx).unwrap(); let pos = *pos; Some((self.paint(pos), pos)) @@ -400,6 +405,7 @@ impl Canvas { self.waiting.clear(); } + #[allow(clippy::unwrap_used)] pub fn paint_all(&mut self) { for (pos, q) in self.queue.iter() { self.canvas[*pos as usize] = q.back().unwrap().0; @@ -411,8 +417,10 @@ impl Canvas { self.queue.is_empty() } + #[allow(clippy::unwrap_used)] pub fn paint(&mut self, pos: u32) -> Option { let q = self.queue.get_mut(&pos).unwrap(); + #[allow(clippy::unwrap_used)] let (c, rid) = q.pop_front().unwrap(); if q.is_empty() { self.queue.remove(&pos); @@ -574,6 +582,7 @@ impl PaintingSim { pub fn get_walloader(&self) -> WalLoader { let mut loader = WalLoader::new(); + #[allow(clippy::unwrap_used)] loader .file_nbit(self.file_nbit) .block_nbit(self.block_nbit) @@ -586,6 +595,7 @@ impl PaintingSim { let mut ops: Vec = Vec::new(); let mut ringid_map = HashMap::new(); let fgen = Rc::new(CountFailGen::new()); + #[allow(clippy::unwrap_used)] self.run( state, &mut canvas, @@ -612,6 +622,7 @@ impl PaintingSim { let mut last_idx = 0; let mut napplied = 0; canvas.clear_queued(); + #[allow(clippy::unwrap_used)] block_on(wal.load( WalStoreEmul::new(state, Rc::new(ZeroFailGen)), |payload, ringid| {