From 42f106dc91fc7be3b1ea38b6ad322876d37d7ed9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 13 Nov 2022 11:07:07 +0000
Subject: [PATCH] refactor: more clippy refactor

---
 src/cpu.rs   |  3 +--
 src/gb.rs    | 10 ++++++++--
 src/inst.rs  | 34 +++++++++++-----------------------
 src/mmu.rs   |  6 +++---
 src/pad.rs   | 19 ++++++++++++-------
 src/ppu.rs   | 15 ++++++++++-----
 src/rom.rs   |  6 ++++++
 src/timer.rs |  6 ++++++
 8 files changed, 57 insertions(+), 42 deletions(-)

diff --git a/src/cpu.rs b/src/cpu.rs
index f557f595..fe7f9c1b 100644
--- a/src/cpu.rs
+++ b/src/cpu.rs
@@ -381,7 +381,7 @@ impl Cpu {
     pub fn read_u16(&mut self) -> u16 {
         let byte1 = self.read_u8();
         let byte2 = self.read_u8();
-        
+
         byte1 as u16 | ((byte2 as u16) << 8)
     }
 
@@ -406,7 +406,6 @@ impl Cpu {
 
     #[inline(always)]
     pub fn pop_word(&mut self) -> u16 {
-        
         self.pop_byte() as u16 | ((self.pop_byte() as u16) << 8)
     }
 
diff --git a/src/gb.rs b/src/gb.rs
index 01f57fd0..73af0a25 100644
--- a/src/gb.rs
+++ b/src/gb.rs
@@ -13,7 +13,7 @@ use crate::{
 use wasm_bindgen::prelude::*;
 
 #[cfg(feature = "wasm")]
-use crate::ppu::{Pixel, Palette};
+use crate::ppu::{Palette, Pixel};
 
 #[cfg(feature = "wasm")]
 use std::{
@@ -206,7 +206,7 @@ impl GameBoy {
         self.cpu.timer()
     }
 
-    pub fn frame_buffer(&mut self) -> &Box<[u8; FRAME_BUFFER_SIZE]> {
+    pub fn frame_buffer(&mut self) -> &[u8; FRAME_BUFFER_SIZE] {
         &(self.ppu().frame_buffer)
     }
 
@@ -292,3 +292,9 @@ pub fn hook_impl(info: &PanicInfo) {
     let message = info.to_string();
     panic(message.as_str());
 }
+
+impl Default for GameBoy {
+    fn default() -> Self {
+        Self::new()
+    }
+}
diff --git a/src/inst.rs b/src/inst.rs
index 7d9891b2..c0a16aa2 100644
--- a/src/inst.rs
+++ b/src/inst.rs
@@ -1,6 +1,6 @@
 use crate::cpu::Cpu;
 
-pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &str); 256] = [
+pub const INSTRUCTIONS: [Instruction; 256] = [
     // 0x0 opcodes
     (nop, 4, "NOP"),
     (ld_bc_u16, 12, "LD BC, u16"),
@@ -275,7 +275,7 @@ pub const INSTRUCTIONS: [(fn(&mut Cpu), u8, &str); 256] = [
     (rst_38h, 16, "RST 38h"),
 ];
 
-pub const EXTENDED: [(fn(&mut Cpu), u8, &str); 256] = [
+pub const EXTENDED: [Instruction; 256] = [
     // 0x0 opcodes
     (rlc_b, 8, "RLC B"),
     (rlc_c, 8, "RLC C"),
@@ -550,6 +550,8 @@ pub const EXTENDED: [(fn(&mut Cpu), u8, &str); 256] = [
     (set_7_a, 8, "SET 7, A"),
 ];
 
+pub type Instruction = (fn(&mut Cpu), u8, &'static str);
+
 fn nop(_cpu: &mut Cpu) {}
 
 fn illegal(_cpu: &mut Cpu) {
@@ -1040,9 +1042,7 @@ fn ccf(cpu: &mut Cpu) {
     cpu.set_carry(!cpu.get_carry());
 }
 
-fn ld_b_b(cpu: &mut Cpu) {
-    cpu.b = cpu.b;
-}
+fn ld_b_b(_cpu: &mut Cpu) {}
 
 fn ld_b_c(cpu: &mut Cpu) {
     cpu.b = cpu.c;
@@ -1077,9 +1077,7 @@ fn ld_c_b(cpu: &mut Cpu) {
     cpu.c = cpu.b;
 }
 
-fn ld_c_c(cpu: &mut Cpu) {
-    cpu.c = cpu.c;
-}
+fn ld_c_c(_cpu: &mut Cpu) {}
 
 fn ld_c_d(cpu: &mut Cpu) {
     cpu.c = cpu.d;
@@ -1114,9 +1112,7 @@ fn ld_d_c(cpu: &mut Cpu) {
     cpu.d = cpu.c;
 }
 
-fn ld_d_d(cpu: &mut Cpu) {
-    cpu.d = cpu.d;
-}
+fn ld_d_d(_cpu: &mut Cpu) {}
 
 fn ld_d_e(cpu: &mut Cpu) {
     cpu.d = cpu.e;
@@ -1151,9 +1147,7 @@ fn ld_e_d(cpu: &mut Cpu) {
     cpu.e = cpu.d;
 }
 
-fn ld_e_e(cpu: &mut Cpu) {
-    cpu.e = cpu.e;
-}
+fn ld_e_e(_cpu: &mut Cpu) {}
 
 fn ld_e_h(cpu: &mut Cpu) {
     cpu.e = cpu.h;
@@ -1188,9 +1182,7 @@ fn ld_h_e(cpu: &mut Cpu) {
     cpu.h = cpu.e;
 }
 
-fn ld_h_h(cpu: &mut Cpu) {
-    cpu.h = cpu.h;
-}
+fn ld_h_h(_cpu: &mut Cpu) {}
 
 fn ld_h_l(cpu: &mut Cpu) {
     cpu.h = cpu.l;
@@ -1225,9 +1217,7 @@ fn ld_l_h(cpu: &mut Cpu) {
     cpu.l = cpu.h;
 }
 
-fn ld_l_l(cpu: &mut Cpu) {
-    cpu.l = cpu.l;
-}
+fn ld_l_l(_cpu: &mut Cpu) {}
 
 fn ld_l_mhl(cpu: &mut Cpu) {
     let byte = cpu.mmu.read(cpu.hl());
@@ -1299,9 +1289,7 @@ fn ld_a_mhl(cpu: &mut Cpu) {
     cpu.a = byte;
 }
 
-fn ld_a_a(cpu: &mut Cpu) {
-    cpu.a = cpu.a;
-}
+fn ld_a_a(_cpu: &mut Cpu) {}
 
 fn add_a_b(cpu: &mut Cpu) {
     cpu.a = add_set_flags(cpu, cpu.a, cpu.b);
diff --git a/src/mmu.rs b/src/mmu.rs
index c18e1f7c..2847a46f 100644
--- a/src/mmu.rs
+++ b/src/mmu.rs
@@ -230,9 +230,9 @@ impl Mmu {
         }
     }
 
-    pub fn write_many(&mut self, addr: u16, data: &Vec<u8>) {
-        for index in 0..data.len() {
-            self.write(addr + index as u16, data[index])
+    pub fn write_many(&mut self, addr: u16, data: &[u8]) {
+        for (index, byte) in data.iter().enumerate() {
+            self.write(addr + index as u16, *byte)
         }
     }
 
diff --git a/src/pad.rs b/src/pad.rs
index 84c833a1..0f2298ff 100644
--- a/src/pad.rs
+++ b/src/pad.rs
@@ -53,21 +53,20 @@ impl Pad {
     pub fn read(&mut self, addr: u16) -> u8 {
         match addr & 0x00ff {
             0x0000 => {
-                let mut value;
-                match self.selection {
+                let mut value = match self.selection {
                     PadSelection::Action => {
-                        value = if self.a { 0x00 } else { 0x01 }
+                        (if self.a { 0x00 } else { 0x01 }
                             | if self.b { 0x00 } else { 0x02 }
                             | if self.select { 0x00 } else { 0x04 }
-                            | if self.start { 0x00 } else { 0x08 }
+                            | if self.start { 0x00 } else { 0x08 })
                     }
                     PadSelection::Direction => {
-                        value = if self.right { 0x00 } else { 0x01 }
+                        (if self.right { 0x00 } else { 0x01 }
                             | if self.left { 0x00 } else { 0x02 }
                             | if self.up { 0x00 } else { 0x04 }
-                            | if self.down { 0x00 } else { 0x08 }
+                            | if self.down { 0x00 } else { 0x08 })
                     }
-                }
+                };
                 value |= if self.selection == PadSelection::Direction {
                     0x10
                 } else {
@@ -141,3 +140,9 @@ impl Pad {
         self.set_int_pad(false);
     }
 }
+
+impl Default for Pad {
+    fn default() -> Self {
+        Self::new()
+    }
+}
diff --git a/src/ppu.rs b/src/ppu.rs
index c871a452..bccbc8e3 100644
--- a/src/ppu.rs
+++ b/src/ppu.rs
@@ -882,12 +882,11 @@ impl Ppu {
                 tile = &self.tiles[obj.tile as usize];
             }
 
-            let tile_row: &[u8];
-            if obj.yflip {
-                tile_row = tile.get_row((7 - tile_offset) as usize);
+            let tile_row = if obj.yflip {
+                tile.get_row((7 - tile_offset) as usize)
             } else {
-                tile_row = tile.get_row((tile_offset) as usize);
-            }
+                tile.get_row((tile_offset) as usize)
+            };
 
             for x in 0..TILE_WIDTH {
                 let is_contained =
@@ -936,3 +935,9 @@ impl Ppu {
             || self.stat_hblank && self.mode == PpuMode::HBlank
     }
 }
+
+impl Default for Ppu {
+    fn default() -> Self {
+        Self::new()
+    }
+}
diff --git a/src/rom.rs b/src/rom.rs
index b01458eb..26a09286 100644
--- a/src/rom.rs
+++ b/src/rom.rs
@@ -412,6 +412,12 @@ impl Cartridge {
     }
 }
 
+impl Default for Cartridge {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
 impl Display for Cartridge {
     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
         write!(
diff --git a/src/timer.rs b/src/timer.rs
index dfe13721..e1fb4a1a 100644
--- a/src/timer.rs
+++ b/src/timer.rs
@@ -100,3 +100,9 @@ impl Timer {
         self.set_int_tima(false);
     }
 }
+
+impl Default for Timer {
+    fn default() -> Self {
+        Self::new()
+    }
+}
-- 
GitLab