From 31c6c4858f1edd3e62aa2de47ceb056d378154a5 Mon Sep 17 00:00:00 2001 From: Alexander Bass Date: Wed, 7 Jun 2023 16:18:36 -0400 Subject: [PATCH] Fix clippy warnings --- src/gui.rs | 14 ++++++------ src/gui/highlighter.rs | 2 +- src/gui/settings_menu.rs | 46 +++++++++++++++++----------------------- src/gui/seven_segment.rs | 6 +++--- src/gui/texture_store.rs | 4 ++-- src/gui/top_menu.rs | 8 +++---- src/logic.rs | 2 +- src/logic/game_board.rs | 20 ++++++++--------- src/logic/tile.rs | 2 +- src/logic/timer.rs | 6 +----- src/main.rs | 4 ++-- src/util.rs | 2 +- 12 files changed, 53 insertions(+), 63 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index e2e418e..b7bf1de 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -53,13 +53,13 @@ pub struct UIState { } impl UIState { pub fn new(width: usize, height: usize, tile_size: usize, top_offset: usize) -> Self { - return Self { + Self { width, height, tile_size, top_offset, ..Default::default() - }; + } } pub fn update_dimensions(&mut self, width: usize, height: usize) { self.width = width; @@ -91,12 +91,12 @@ impl UIState { let (x, y) = self.pixel_screen_scale(x, y); let x = x + self.letterbox.0; let y = y + self.letterbox.1; - return (x, y); + (x, y) } pub fn pixel_screen_scale(&self, x: usize, y: usize) -> (f32, f32) { let x = x as f32; let y = y as f32; - return (x * self.scale, y * self.scale); + (x * self.scale, y * self.scale) } } @@ -113,10 +113,10 @@ pub struct GameUI { impl GameUI { pub fn new(settings: UIState) -> Self { let set = settings; - return Self { + Self { state: set, ..Default::default() - }; + } } pub fn is_valid_position(&self, x: usize, y: usize) -> bool { if x < self.state.width && y < self.state.height { @@ -145,6 +145,6 @@ impl GameUI { if !self.is_valid_position(x, y) { return None; } - return Some((x, y)); + Some((x, y)) } } diff --git a/src/gui/highlighter.rs b/src/gui/highlighter.rs index 77d7c28..029c190 100644 --- a/src/gui/highlighter.rs +++ b/src/gui/highlighter.rs @@ -117,7 +117,7 @@ impl Highlighter { } } - self.move_highlight(&interface, event_handler); + self.move_highlight(interface, event_handler); } fn move_highlight(&mut self, interface: &UIState, event_handler: &mut Events) { diff --git a/src/gui/settings_menu.rs b/src/gui/settings_menu.rs index 97afb60..9e4e383 100644 --- a/src/gui/settings_menu.rs +++ b/src/gui/settings_menu.rs @@ -51,18 +51,18 @@ impl SettingsMenu { root_ui().window(hash!(), vec2(0., 0.), vec2(screen_width, screen_height), |ui| { draw_rectangle(0f32, 0f32, screen_width, screen_height, background_color); - ui.push_skin(&exit_button_skin); + ui.push_skin(exit_button_skin); if widgets::Button::new("").size(vec2(50.0, 50.0)).position(vec2(0f32, 0f32)).ui(ui) { event_handler.add(GUIEvent::CloseSettings) } ui.pop_skin(); - ui.push_skin(&skin); + ui.push_skin(skin); let half_screen_width = screen_width * 0.5; render_counter( &mut self.width, ui, - &textures, + textures, vec2(half_screen_width, 100f32), "Minefield Width", MIN_MINEFIELD_WIDTH, @@ -71,7 +71,7 @@ impl SettingsMenu { render_counter( &mut self.height, ui, - &textures, + textures, vec2(half_screen_width, 200f32), "Minefield Height", MIN_MINEFIELD_HEIGHT, @@ -80,7 +80,7 @@ impl SettingsMenu { render_counter( &mut self.mines, ui, - &textures, + textures, vec2(half_screen_width, 300f32), "Mines", 1, @@ -113,14 +113,12 @@ impl SettingsMenu { { event_handler.add(GUIEvent::SwitchLanguage(Language::Japanese)); } - } else { - if widgets::Button::new("Japanese") - .size(vec2(BUTTON_SIZE, BUTTON_SIZE)) - .position(vec2(language_button_x, BUTTON_MENU_Y)) - .ui(ui) - { - event_handler.add(GUIEvent::SwitchLanguage(Language::English)); - } + } else if widgets::Button::new("Japanese") + .size(vec2(BUTTON_SIZE, BUTTON_SIZE)) + .position(vec2(language_button_x, BUTTON_MENU_Y)) + .ui(ui) + { + event_handler.add(GUIEvent::SwitchLanguage(Language::English)); } if let ModifyMode::Question = self.board_modify_mode { if widgets::Button::new("ON") @@ -131,15 +129,13 @@ impl SettingsMenu { self.board_modify_mode = ModifyMode::Flag; event_handler.add(GUIEvent::SetQuestionMode(ModifyMode::Flag)); } - } else { - if widgets::Button::new("OFF") - .size(vec2(BUTTON_SIZE, BUTTON_SIZE)) - .position(vec2(question_button_x, BUTTON_MENU_Y)) - .ui(ui) - { - self.board_modify_mode = ModifyMode::Question; - event_handler.add(GUIEvent::SetQuestionMode(ModifyMode::Question)); - } + } else if widgets::Button::new("OFF") + .size(vec2(BUTTON_SIZE, BUTTON_SIZE)) + .position(vec2(question_button_x, BUTTON_MENU_Y)) + .ui(ui) + { + self.board_modify_mode = ModifyMode::Question; + event_handler.add(GUIEvent::SetQuestionMode(ModifyMode::Question)); } }); } @@ -172,11 +168,9 @@ fn render_counter(count: &mut usize, ui: &mut Ui, textures: &TextureStore, posit if widgets::Button::new("-") .size(vec2(COUNTER_BUTTON_HEIGHT, COUNTER_BUTTON_HEIGHT)) .position(position - vec2(COUNTER_BUTTON_HEIGHT + COUNTER_BUTTON_MARGIN, -BUTTON_OFFSET_HEIGHT)) - .ui(ui) + .ui(ui) && *count > min { - if *count > min { - *count -= 1; - } + *count -= 1; } if widgets::Button::new("++") .size(vec2(COUNTER_BUTTON_HEIGHT, COUNTER_BUTTON_HEIGHT)) diff --git a/src/gui/seven_segment.rs b/src/gui/seven_segment.rs index 2f34a09..61dbab0 100644 --- a/src/gui/seven_segment.rs +++ b/src/gui/seven_segment.rs @@ -7,9 +7,9 @@ pub const HEIGHT: usize = 23 * 2; use super::{texture_store::TextureStore, UIState}; -pub fn draw_seven_segment(ui_state: &UIState, ui: &mut Ui, textures: &TextureStore, val: &Vec, x: usize, y: usize) { +pub fn draw_seven_segment(ui_state: &UIState, ui: &mut Ui, textures: &TextureStore, val: &[usize], x: usize, y: usize) { for (n, digit) in val.iter().enumerate() { - let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH as usize, HEIGHT); + let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH, HEIGHT); let (pos_x, pos_y) = ui_state.pixel_screen_offset(n * WIDTH + x, y); widgets::Texture::new(textures.numbers[*digit]) @@ -19,7 +19,7 @@ pub fn draw_seven_segment(ui_state: &UIState, ui: &mut Ui, textures: &TextureSto } } -pub fn draw_seven_segment_unscaled(ui: &mut Ui, textures: &TextureStore, val: &Vec, x: usize, y: usize) { +pub fn draw_seven_segment_unscaled(ui: &mut Ui, textures: &TextureStore, val: &[usize], x: usize, y: usize) { for (n, digit) in val.iter().enumerate() { let (pos_x, pos_y) = ((n * WIDTH + x) as f32, y as f32); diff --git a/src/gui/texture_store.rs b/src/gui/texture_store.rs index 02effe7..c5b2f91 100644 --- a/src/gui/texture_store.rs +++ b/src/gui/texture_store.rs @@ -33,9 +33,9 @@ impl TextureStore { } } pub fn get_tiles(&self) -> &Vec { - return match self.lang { + match self.lang { Language::English => &self.english_tiles, Language::Japanese => &self.japanese_tiles, - }; + } } } diff --git a/src/gui/top_menu.rs b/src/gui/top_menu.rs index 4d0c56d..ca88784 100644 --- a/src/gui/top_menu.rs +++ b/src/gui/top_menu.rs @@ -54,7 +54,7 @@ impl GUITop { const HEIGHT: usize = 35; let pos_y = (ui_state.top_offset - HEIGHT) / 2; let pos_x = (13 * 2 * 2 - WIDTH) / 2; - let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH as usize, HEIGHT); + let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH, HEIGHT); let (pos_x, pos_y) = ui_state.pixel_screen_offset(pos_x, pos_y); if widgets::Button::new(textures.cog) .size(vec2(scaled_width, scaled_height)) @@ -69,9 +69,9 @@ impl GUITop { } } - self.timer.render(&ui_state, game_logic.get_time(), ui, &textures); - self.smile.render(&ui_state, ui, event_handler, &textures); - self.flag_counter.render(&ui_state, game_logic.board.remaining_flags(), ui, &textures); + self.timer.render(ui_state, game_logic.get_time(), ui, textures); + self.smile.render(ui_state, ui, event_handler, textures); + self.flag_counter.render(ui_state, game_logic.board.remaining_flags(), ui, textures); }); } } diff --git a/src/logic.rs b/src/logic.rs index 45659b2..8118f48 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -82,7 +82,7 @@ impl Minesweeper { self.board.modify(x, y, &mut self.events) } pub fn get_time(&self) -> Option { - return self.timer.elapsed(); + self.timer.elapsed() } pub fn highlight(&mut self, x: usize, y: usize) { if self.state == GameState::Playing || self.state == GameState::Empty { diff --git a/src/logic/game_board.rs b/src/logic/game_board.rs index df069df..71b9efd 100644 --- a/src/logic/game_board.rs +++ b/src/logic/game_board.rs @@ -109,11 +109,11 @@ impl GameBoard { self.flags -= 1; match self.modify_mode { ModifyMode::Flag => { - event_handler.add(GameEvent::FlagTile(x, y, tile.clone())); + event_handler.add(GameEvent::FlagTile(x, y, tile)); None } ModifyMode::Question => { - event_handler.add(GameEvent::QuestionTile(x, y, tile.clone())); + event_handler.add(GameEvent::QuestionTile(x, y, tile)); Some(TileModifier::Unsure) } @@ -123,7 +123,7 @@ impl GameBoard { } } else { self.flags += 1; - event_handler.add(GameEvent::FlagTile(x, y, tile.clone())); + event_handler.add(GameEvent::FlagTile(x, y, tile)); Some(TileModifier::Flagged) }; if let Some(tile) = self.get_tile_mut(x, y) { @@ -136,8 +136,8 @@ impl GameBoard { if let BoardState::Ungenerated = self.state { self.generate(x, y); } - let &tile = &self.tiles[x][y]; - if let Some(_) = tile.modifier { + let tile = self.tiles[x][y]; + if tile.modifier.is_some() { return None; } if tile.swept { @@ -145,10 +145,10 @@ impl GameBoard { } self.tiles[x][y].swept = true; self.revealed_tiles += 1; - event_handler.add(GameEvent::RevealTile(x, y, self.tiles[x][y].clone())); + event_handler.add(GameEvent::RevealTile(x, y, self.tiles[x][y])); if tile.state == TileState::Mine { - event_handler.add(GameEvent::Lose(x, y, tile.clone())); + event_handler.add(GameEvent::Lose(x, y, tile)); event_handler.add(GameEvent::GameEnd(self.clone())); return Some(GameState::GameOver); @@ -157,7 +157,7 @@ impl GameBoard { let mut scan_list = VecDeque::from([(x, y)]); let mut revealed: usize = 0; - while scan_list.len() > 0 { + while !scan_list.is_empty() { for &scan_location in ADJACENT_WITHOUT_CENTER.iter() { if let Some((x, y)) = scan_list.front() { if let Some(old_tile) = self.get_tile(*x, *y) { @@ -179,7 +179,7 @@ impl GameBoard { scan_list.push_back((x, y)); tile.swept = true; revealed += 1; - event_handler.add(GameEvent::RevealTile(x, y, tile.clone())); + event_handler.add(GameEvent::RevealTile(x, y, *tile)); } } } @@ -226,7 +226,7 @@ impl GameBoard { let y = macroquad::rand::gen_range(0, height); let mut tile = &mut self.tiles[x][y]; - if tile.state == TileState::Mine || tile.safe == true { + if tile.state == TileState::Mine || tile.safe { continue; } diff --git a/src/logic/tile.rs b/src/logic/tile.rs index 1f5a4ce..3481bd7 100644 --- a/src/logic/tile.rs +++ b/src/logic/tile.rs @@ -27,7 +27,7 @@ impl Tile { } } pub fn highlight(&mut self) { - if self.swept == false { + if !self.swept { self.highlighted = true; } } diff --git a/src/logic/timer.rs b/src/logic/timer.rs index 7f618ce..a8a58f9 100644 --- a/src/logic/timer.rs +++ b/src/logic/timer.rs @@ -26,11 +26,7 @@ impl Timer { if let TimerState::Frozen = self.state { return Some(self.old); } - if let Some(time) = self.start_time { - Some(get_time() - time) - } else { - None - } + self.start_time.map(|time| get_time() - time) } pub fn stop(&mut self) { self.old = self.elapsed().unwrap_or(0f64); diff --git a/src/main.rs b/src/main.rs index e15f38a..f1eb96a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ mod sprite_loader; mod util; fn main() { - let width = (30 * 32) as i32; - let height = (16 * 32) as i32 + 100; + let width = 30 * 32; + let height = 16 * 32 + 100; Window::from_config( Conf { sample_count: 2, diff --git a/src/util.rs b/src/util.rs index 52cb782..f172491 100644 --- a/src/util.rs +++ b/src/util.rs @@ -25,7 +25,7 @@ impl Events { } pub fn next(&mut self) -> Option { - if self.events.len() > 0 { + if !self.events.is_empty() { self.events.pop() } else { None