From 7a543caa2ebf38986b5a2cea4b1b774304736301 Mon Sep 17 00:00:00 2001 From: Dennis Kempin Date: Fri, 4 Feb 2022 14:56:45 -0800 Subject: [PATCH] clippy: fix disk crate Just a few nit fixes and we can now enable clippy for this crate. BUG=b:192373803 TEST=./tools/clippy Change-Id: Ia6ab90fa33e979066d9eeae2372630d93cf4026a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3441343 Reviewed-by: Daniel Verkamp Tested-by: kokoro Commit-Queue: Dennis Kempin --- disk/src/composite.rs | 12 ++++------ disk/src/disk.rs | 8 +++---- disk/src/qcow/mod.rs | 46 +++++++++++++++++++------------------- disk/src/qcow/vec_cache.rs | 2 +- tools/clippy | 1 - 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/disk/src/composite.rs b/disk/src/composite.rs index 9241f4958d..09000629d7 100644 --- a/disk/src/composite.rs +++ b/disk/src/composite.rs @@ -725,7 +725,7 @@ mod tests { file2.as_raw_descriptor(), file3.as_raw_descriptor(), ]; - in_fds.sort(); + in_fds.sort_unstable(); let disk_part1 = ComponentDiskPart { file: Box::new(file1), offset: 0, @@ -743,7 +743,7 @@ mod tests { }; let composite = CompositeDiskFile::new(vec![disk_part1, disk_part2, disk_part3]).unwrap(); let mut out_fds = composite.as_raw_descriptors(); - out_fds.sort(); + out_fds.sort_unstable(); assert_eq!(in_fds, out_fds); } @@ -816,9 +816,7 @@ mod tests { .read_exact_at_volatile(output_volatile_memory.get_slice(0, 300).unwrap(), 0) .unwrap(); - for i in 50..250 { - input_memory[i] = 0; - } + input_memory[50..250].iter_mut().for_each(|x| *x = 0); assert!(input_memory.iter().eq(output_memory.iter())); } @@ -861,9 +859,7 @@ mod tests { .read_exact_at_volatile(output_volatile_memory.get_slice(0, 300).unwrap(), 0) .unwrap(); - for i in 50..250 { - input_memory[i] = 0; - } + input_memory[50..250].iter_mut().for_each(|x| *x = 0); for i in 0..300 { println!( "input[{0}] = {1}, output[{0}] = {2}", diff --git a/disk/src/disk.rs b/disk/src/disk.rs index ca82cf116a..5aa89de2ee 100644 --- a/disk/src/disk.rs +++ b/disk/src/disk.rs @@ -590,7 +590,7 @@ mod tests { // detect_image_type is ever updated to validate more of the header, this test would need // to be updated. let buf: &[u8] = &[0x51, 0x46, 0x49, 0xfb]; - t.write_all(&buf).unwrap(); + t.write_all(buf).unwrap(); let image_type = detect_image_type(&t).expect("failed to detect image type"); assert_eq!(image_type, ImageType::Qcow2); } @@ -602,7 +602,7 @@ mod tests { // detect_image_type is ever updated to validate more of the header, this test would need // to be updated. let buf: &[u8] = &[0x3a, 0xff, 0x26, 0xed]; - t.write_all(&buf).unwrap(); + t.write_all(buf).unwrap(); let image_type = detect_image_type(&t).expect("failed to detect image type"); assert_eq!(image_type, ImageType::AndroidSparse); } @@ -615,7 +615,7 @@ mod tests { // detect_image_type is ever updated to validate more of the header, this test would need // to be updated. let buf = "composite_disk\x1d".as_bytes(); - t.write_all(&buf).unwrap(); + t.write_all(buf).unwrap(); let image_type = detect_image_type(&t).expect("failed to detect image type"); assert_eq!(image_type, ImageType::CompositeDisk); } @@ -626,7 +626,7 @@ mod tests { // Write a file smaller than the four-byte qcow2/sparse magic to ensure the small file logic // works correctly and handles it as a raw file. let buf: &[u8] = &[0xAA, 0xBB]; - t.write_all(&buf).unwrap(); + t.write_all(buf).unwrap(); let image_type = detect_image_type(&t).expect("failed to detect image type"); assert_eq!(image_type, ImageType::Raw); } diff --git a/disk/src/qcow/mod.rs b/disk/src/qcow/mod.rs index ba98e05da7..b3e0047437 100644 --- a/disk/src/qcow/mod.rs +++ b/disk/src/qcow/mod.rs @@ -1822,7 +1822,7 @@ mod tests { fn basic_file(header: &[u8]) -> File { let mut disk_file = tempfile().expect("failed to create tempfile"); - disk_file.write_all(&header).unwrap(); + disk_file.write_all(header).unwrap(); disk_file.set_len(0x8000_0000).unwrap(); disk_file.seek(SeekFrom::Start(0)).unwrap(); disk_file @@ -1920,7 +1920,7 @@ mod tests { #[test] fn test_header_excessive_file_size_rejected() { let mut header = valid_header(); - &mut header[24..32].copy_from_slice(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1e]); + header[24..32].copy_from_slice(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1e]); with_basic_file(&header, |disk_file: File| { QcowFile::from(disk_file, MAX_NESTING_DEPTH).expect_err("Failed to create file."); }); @@ -1970,7 +1970,7 @@ mod tests { #[test] fn test_header_huge_num_refcounts() { let mut header = valid_header(); - &mut header[56..60].copy_from_slice(&[0x02, 0x00, 0xe8, 0xff]); + header[56..60].copy_from_slice(&[0x02, 0x00, 0xe8, 0xff]); with_basic_file(&header, |disk_file: File| { QcowFile::from(disk_file, MAX_NESTING_DEPTH) .expect_err("Created disk with excessive refcount clusters"); @@ -1980,7 +1980,7 @@ mod tests { #[test] fn test_header_huge_refcount_offset() { let mut header = valid_header(); - &mut header[48..56].copy_from_slice(&[0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x02, 0x00]); + header[48..56].copy_from_slice(&[0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x02, 0x00]); with_basic_file(&header, |disk_file: File| { QcowFile::from(disk_file, MAX_NESTING_DEPTH) .expect_err("Created disk with excessive refcount offset"); @@ -1991,11 +1991,11 @@ mod tests { fn write_read_start() { with_basic_file(&valid_header(), |disk_file: File| { let mut q = QcowFile::from(disk_file, MAX_NESTING_DEPTH).unwrap(); - q.write(b"test first bytes") + q.write_all(b"test first bytes") .expect("Failed to write test string."); let mut buf = [0u8; 4]; q.seek(SeekFrom::Start(0)).expect("Failed to seek."); - q.read(&mut buf).expect("Failed to read."); + q.read_exact(&mut buf).expect("Failed to read."); assert_eq!(&buf, b"test"); }); } @@ -2005,14 +2005,14 @@ mod tests { let disk_file = basic_file(&valid_header()); let mut backing = QcowFile::from(disk_file, MAX_NESTING_DEPTH).unwrap(); backing - .write(b"test first bytes") + .write_all(b"test first bytes") .expect("Failed to write test string."); let mut buf = [0u8; 4]; let wrapping_disk_file = basic_file(&valid_header()); let mut wrapping = QcowFile::from(wrapping_disk_file, MAX_NESTING_DEPTH).unwrap(); wrapping.set_backing_file(Some(Box::new(backing))); wrapping.seek(SeekFrom::Start(0)).expect("Failed to seek."); - wrapping.read(&mut buf).expect("Failed to read."); + wrapping.read_exact(&mut buf).expect("Failed to read."); assert_eq!(&buf, b"test"); } @@ -2021,18 +2021,18 @@ mod tests { let disk_file = basic_file(&valid_header()); let mut backing = QcowFile::from(disk_file, MAX_NESTING_DEPTH).unwrap(); backing - .write(b"test first bytes") + .write_all(b"test first bytes") .expect("Failed to write test string."); let wrapping_disk_file = basic_file(&valid_header()); let mut wrapping = QcowFile::from(wrapping_disk_file, MAX_NESTING_DEPTH).unwrap(); wrapping.set_backing_file(Some(Box::new(backing))); wrapping.seek(SeekFrom::Start(0)).expect("Failed to seek."); wrapping - .write(b"TEST") + .write_all(b"TEST") .expect("Failed to write second test string."); let mut buf = [0u8; 10]; wrapping.seek(SeekFrom::Start(0)).expect("Failed to seek."); - wrapping.read(&mut buf).expect("Failed to read."); + wrapping.read_exact(&mut buf).expect("Failed to read."); assert_eq!(&buf, b"TEST first"); } @@ -2042,10 +2042,10 @@ mod tests { let mut q = QcowFile::from(disk_file, MAX_NESTING_DEPTH).unwrap(); let b = [0x55u8; 0x1000]; q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); - q.write(&b).expect("Failed to write test string."); + q.write_all(&b).expect("Failed to write test string."); let mut buf = [0u8; 4]; q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); - q.read(&mut buf).expect("Failed to read."); + q.read_exact(&mut buf).expect("Failed to read."); assert_eq!(buf[0], 0x55); }); } @@ -2057,14 +2057,14 @@ mod tests { // Write some test data. let b = [0x55u8; 0x1000]; q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); - q.write(&b).expect("Failed to write test string."); + q.write_all(&b).expect("Failed to write test string."); // Overwrite the test data with zeroes. q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); q.write_zeroes_all(0x200).expect("Failed to write zeroes."); // Verify that the correct part of the data was zeroed out. let mut buf = [0u8; 0x1000]; q.seek(SeekFrom::Start(0xfff2000)).expect("Failed to seek."); - q.read(&mut buf).expect("Failed to read."); + q.read_exact(&mut buf).expect("Failed to read."); assert_eq!(buf[0], 0); assert_eq!(buf[0x1FF], 0); assert_eq!(buf[0x200], 0x55); @@ -2082,7 +2082,7 @@ mod tests { // Write some test data. let b = [0x55u8; CHUNK_SIZE]; q.seek(SeekFrom::Start(0)).expect("Failed to seek."); - q.write(&b).expect("Failed to write test string."); + q.write_all(&b).expect("Failed to write test string."); // Overwrite the full cluster with zeroes. q.seek(SeekFrom::Start(0)).expect("Failed to seek."); q.write_zeroes_all(CHUNK_SIZE) @@ -2090,7 +2090,7 @@ mod tests { // Verify that the data was zeroed out. let mut buf = [0u8; CHUNK_SIZE]; q.seek(SeekFrom::Start(0)).expect("Failed to seek."); - q.read(&mut buf).expect("Failed to read."); + q.read_exact(&mut buf).expect("Failed to read."); assert_eq!(buf[0], 0); assert_eq!(buf[CHUNK_SIZE - 1], 0); }); @@ -2105,7 +2105,7 @@ mod tests { backing .seek(SeekFrom::Start(0xfff2000)) .expect("Failed to seek."); - backing.write(&b).expect("Failed to write test string."); + backing.write_all(&b).expect("Failed to write test string."); let wrapping_disk_file = basic_file(&valid_header()); let mut wrapping = QcowFile::from(wrapping_disk_file, MAX_NESTING_DEPTH).unwrap(); wrapping.set_backing_file(Some(Box::new(backing))); @@ -2122,7 +2122,7 @@ mod tests { wrapping .seek(SeekFrom::Start(0xfff2000)) .expect("Failed to seek."); - wrapping.read(&mut buf).expect("Failed to read."); + wrapping.read_exact(&mut buf).expect("Failed to read."); assert_eq!(buf[0], 0); assert_eq!(buf[0x1FF], 0); assert_eq!(buf[0x200], 0x55); @@ -2143,7 +2143,7 @@ mod tests { let mut q = QcowFile::from(disk_file, MAX_NESTING_DEPTH).unwrap(); let mut b = [5u8; 16]; q.seek(SeekFrom::Start(1000)).expect("Failed to seek."); - q.read(&mut b).expect("Failed to read."); + q.read_exact(&mut b).expect("Failed to read."); assert_eq!(0, b[0]); assert_eq!(0, b[15]); }); @@ -2516,7 +2516,7 @@ mod tests { for xfer in &xfers { q.seek(SeekFrom::Start(xfer.addr)).expect("Failed to seek."); if xfer.write { - q.write(&b).expect("Failed to write."); + q.write_all(&b).expect("Failed to write."); } else { let read_count: usize = q.read(&mut b).expect("Failed to read."); assert_eq!(read_count, BUF_SIZE); @@ -2720,7 +2720,7 @@ mod tests { .unwrap(); let _level1_qcow_file = QcowFile::new_from_backing( level1_qcow_file, - &backing_file_path.to_str().unwrap(), + backing_file_path.to_str().unwrap(), 1000, /* allow deep nesting */ ) .unwrap(); @@ -2728,7 +2728,7 @@ mod tests { let level2_qcow_file = tempfile().unwrap(); let _level2_qcow_file = QcowFile::new_from_backing( level2_qcow_file, - &level1_qcow_file_path.to_str().unwrap(), + level1_qcow_file_path.to_str().unwrap(), 1000, /* allow deep nesting */ ) .expect("failed to create level2 qcow file"); diff --git a/disk/src/qcow/vec_cache.rs b/disk/src/qcow/vec_cache.rs index 18cfc69789..b5f6db2d38 100644 --- a/disk/src/qcow/vec_cache.rs +++ b/disk/src/qcow/vec_cache.rs @@ -178,7 +178,7 @@ mod tests { // Check that three of the four items inserted are still there and that the most recently // inserted is one of them. - let num_items = (0..=3).filter(|k| cache.contains_key(&k)).count(); + let num_items = (0..=3).filter(|k| cache.contains_key(k)).count(); assert_eq!(num_items, 3); assert!(cache.contains_key(&3)); } diff --git a/tools/clippy b/tools/clippy index ac8d431600..8c07e2b646 100755 --- a/tools/clippy +++ b/tools/clippy @@ -15,7 +15,6 @@ cd "$(dirname $0)/.." # TODO(b/192373803): Clean up clippy error is the following crates EXCLUDE=( devices # 92 errors - disk # 36 errors kvm # 641 errors kvm_sys # 613 errors libvda # 79 errors