From a676280ec85d36fb02a947c0aea600950545c295 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Fri, 31 Mar 2023 14:32:01 -0500 Subject: [PATCH] clippy: Fix all clippy lints --- src/bin/chirp-minifb.rs | 5 +++-- src/cpu.rs | 12 ++++++------ src/cpu/disassembler.rs | 4 ++-- src/cpu/tests.rs | 20 ++++++++++---------- src/io.rs | 6 +++--- src/lib.rs | 2 +- tests/integration.rs | 5 ++++- 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/bin/chirp-minifb.rs b/src/bin/chirp-minifb.rs index b1212fb..2218740 100644 --- a/src/bin/chirp-minifb.rs +++ b/src/bin/chirp-minifb.rs @@ -190,12 +190,13 @@ impl Iterator for State { fn main() -> Result<()> { let options = Arguments::parse_args_default_or_exit(); let state = State::new(options)?; - Ok(for result in state { + for result in state { if let Err(e) = result { eprintln!("{}", e.bold().red()); break; } - }) + } + Ok(()) } /// Parses a hexadecimal string into a u16 diff --git a/src/cpu.rs b/src/cpu.rs index 2362f8c..7265103 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -394,7 +394,7 @@ impl CPU { /// Unset a breakpoint // TODO: Unit test this pub fn unset_break(&mut self, point: Adr) -> &mut Self { - fn linear_find(needle: Adr, haystack: &Vec) -> Option { + fn linear_find(needle: Adr, haystack: &[Adr]) -> Option { for (i, v) in haystack.iter().enumerate() { if *v == needle { return Some(i); @@ -402,7 +402,7 @@ impl CPU { } None } - if let Some(idx) = linear_find(point, &self.breakpoints) { + if let Some(idx) = linear_find(point, self.breakpoints.as_slice()) { assert_eq!(point, self.breakpoints.swap_remove(idx)); } self @@ -419,7 +419,7 @@ impl CPU { ///# } /// ``` pub fn breakpoints(&self) -> &[Adr] { - &self.breakpoints.as_slice() + self.breakpoints.as_slice() } /// Unpauses the emulator for a single tick, @@ -498,7 +498,7 @@ impl CPU { // Use a monotonic counter when testing if let Some(speed) = self.flags.monotonic { if self.flags.vbi_wait { - self.flags.vbi_wait = !(self.cycle % speed == 0); + self.flags.vbi_wait = self.cycle % speed != 0; } let speed = 1.0 / speed as f64; self.delay -= speed; @@ -1003,7 +1003,7 @@ impl CPU { let addr = (y + byte) * 8 + (x & 0x3f) / 8 + self.screen; // Read a byte of sprite data into a u16, and shift it x % 8 bits let sprite: u8 = bus.read(self.i + byte); - let sprite = (sprite as u16) << 8 - (x & 7) & if x % 64 > 56 { 0xff00 } else { 0xffff }; + let sprite = (sprite as u16) << (8 - (x & 7)) & if x % 64 > 56 { 0xff00 } else { 0xffff }; // Read a u16 from the bus containing the two bytes which might need to be updated let mut screen: u16 = bus.read(addr); // Save the bits-toggled-off flag if necessary @@ -1145,7 +1145,7 @@ impl CPU { fn load_dma(&mut self, x: Reg, bus: &mut Bus) { let i = self.i as usize; for (reg, value) in bus - .get(i + 0..=i + x) + .get(i..=i + x) .unwrap_or_default() .iter() .enumerate() diff --git a/src/cpu/disassembler.rs b/src/cpu/disassembler.rs index 39b325f..8bc585c 100644 --- a/src/cpu/disassembler.rs +++ b/src/cpu/disassembler.rs @@ -1,5 +1,5 @@ //! A disassembler for Chip-8 opcodes - +#![allow(clippy::bad_bit_mask)] use super::Disassembler; use imperative_rs::InstructionSet; use owo_colors::{OwoColorize, Style}; @@ -175,7 +175,7 @@ impl Default for Dis { impl Disassembler for Dis { fn once(&self, insn: u16) -> String { - if let Some((_, insn)) = Insn::decode(&insn.to_be_bytes()).ok() { + if let Ok((_, insn)) = Insn::decode(&insn.to_be_bytes()) { format!("{}", insn.style(self.normal)) } else { format!("{}", format_args!("inval {insn:04x}").style(self.invalid)) diff --git a/src/cpu/tests.rs b/src/cpu/tests.rs index 419f7fc..0c1bafa 100644 --- a/src/cpu/tests.rs +++ b/src/cpu/tests.rs @@ -109,7 +109,7 @@ mod sys { // Place the address on the stack bus.write(cpu.sp.wrapping_add(2), test_addr); - cpu.ret(&mut bus); + cpu.ret(&bus); // Verify the current address is the address from the stack assert_eq!(test_addr, cpu.pc); @@ -715,20 +715,20 @@ mod io { for x in 0..0xf { cpu.v[x] = 0xff; cpu.wait_for_key(x); - assert_eq!(true, cpu.flags.keypause); + assert!(cpu.flags.keypause); assert_eq!(0xff, cpu.v[x]); // There are three parts to a button press // When the button is pressed cpu.press(key); - assert_eq!(true, cpu.flags.keypause); + assert!(cpu.flags.keypause); assert_eq!(0xff, cpu.v[x]); // When the button is held cpu.wait_for_key(x); - assert_eq!(true, cpu.flags.keypause); + assert!(cpu.flags.keypause); assert_eq!(0xff, cpu.v[x]); // And when the button is released! cpu.release(key); - assert_eq!(false, cpu.flags.keypause); + assert!(!cpu.flags.keypause); assert_eq!(Some(key), cpu.flags.lastkey); cpu.wait_for_key(x); assert_eq!(key as u8, cpu.v[x]); @@ -880,7 +880,7 @@ mod io { const DATA: &[u8] = b"ABCDEFGHIJKLMNOP"; // Load some test data into memory let addr = 0x456; - cpu.v.as_mut_slice().write(DATA).unwrap(); + cpu.v.as_mut_slice().write_all(DATA).unwrap(); for len in 0..16 { // Perform DMA store cpu.i = addr as u16; @@ -890,7 +890,7 @@ mod io { assert_eq!(bus[0..=len], DATA[0..=len]); assert_eq!(bus[len + 1..], [0; 16][len + 1..]); // clear - bus.write(&[0; 16]).unwrap(); + bus.write_all(&[0; 16]).unwrap(); } } @@ -904,7 +904,7 @@ mod io { let addr = 0x456; bus.get_mut(addr..addr + DATA.len()) .unwrap() - .write(DATA) + .write_all(DATA) .unwrap(); for len in 0..16 { // Perform DMA load @@ -958,7 +958,7 @@ mod behavior { std::thread::sleep(Duration::from_secs_f64(1.0 / 60.0)); } // Display wait is disabled after a 1 frame pause - assert_eq!(false, cpu.flags.vbi_wait); + assert!(!cpu.flags.vbi_wait); } } mod breakpoint { @@ -968,7 +968,7 @@ mod behavior { let (mut cpu, mut bus) = setup_environment(); cpu.set_break(0x202); cpu.multistep(&mut bus, 10).unwrap(); - assert_eq!(true, cpu.flags.pause); + assert!(cpu.flags.pause); assert_eq!(0x202, cpu.pc); } } diff --git a/src/io.rs b/src/io.rs index dd36191..7d2339f 100644 --- a/src/io.rs +++ b/src/io.rs @@ -110,7 +110,7 @@ impl FrameBuffer { if let Some(screen) = bus.get_region(Region::Screen) { for (idx, byte) in screen.iter().enumerate() { for bit in 0..8 { - self.buffer[8 * idx + bit] = if byte & (1 << 7 - bit) as u8 != 0 { + self.buffer[8 * idx + bit] = if byte & (1 << (7 - bit)) as u8 != 0 { self.format.fg } else { self.format.bg @@ -156,7 +156,7 @@ impl UI { } self.time = Instant::now(); // update framebuffer - self.fb.render(&mut self.window, &mut ch8.bus); + self.fb.render(&mut self.window, &ch8.bus); } Some(()) } @@ -189,7 +189,7 @@ impl UI { .print_screen() .expect("The 'screen' memory region should exist"), F3 => { - debug_dump_screen(&ch8, &self.rom).expect("Unable to write debug screen dump"); + debug_dump_screen(ch8, &self.rom).expect("Unable to write debug screen dump"); } F4 | Slash => { eprintln!("Debug {}.", { diff --git a/src/lib.rs b/src/lib.rs index 667e5c8..29f69f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ // (c) 2023 John A. Breaux // This code is licensed under MIT license (see LICENSE.txt for details) #![cfg_attr(feature = "unstable", feature(no_coverage))] -#![deny(missing_docs)] +#![deny(missing_docs, clippy::all)] //! This crate implements a Chip-8 interpreter as if it were a real CPU architecture, //! to the best of my current knowledge. As it's the first emulator project I've //! embarked on, and I'm fairly new to Rust, it's going to be rough around the edges. diff --git a/tests/integration.rs b/tests/integration.rs index c1cb003..6cee0ee 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -22,6 +22,7 @@ mod bus { assert_eq!(r1, r2); } #[test] + #[allow(clippy::clone_on_copy)] fn clone() { let r1 = Screen; let r2 = r1.clone(); @@ -194,8 +195,9 @@ mod dis { use imperative_rs::InstructionSet; #[test] + #[allow(clippy::clone_on_copy)] fn clone() { - let opcode = Insn::decode(&[0xeF, 0xa1]).unwrap().1; // random valid opcode + let opcode = Insn::decode(&[0xef, 0xa1]).unwrap().1; // random valid opcode let clone = opcode.clone(); assert_eq!(opcode, clone); } @@ -226,6 +228,7 @@ mod ui_builder { println!("{ui_builder:?}"); } #[test] + #[allow(clippy::redundant_clone)] fn clone_debug() { let ui_builder_clone = UIBuilder::default().clone(); println!("{ui_builder_clone:?}");