diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-11-14 09:51:12 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-11-14 09:51:12 -0500 |
| commit | 7b78c39cb604961564b1d4e0a491eafacc85e8cb (patch) | |
| tree | 8618f2a4fc76a389bcabdeb94ff46ab6d74b1d77 | |
| parent | dungeon: refactor manual drop (diff) | |
| download | DungeonCrawl-7b78c39cb604961564b1d4e0a491eafacc85e8cb.tar.gz DungeonCrawl-7b78c39cb604961564b1d4e0a491eafacc85e8cb.tar.bz2 DungeonCrawl-7b78c39cb604961564b1d4e0a491eafacc85e8cb.zip | |
Enable more clippy lints
| -rw-r--r-- | Cargo.toml | 18 | ||||
| -rw-r--r-- | dungeon/src/bsp.rs | 28 | ||||
| -rw-r--r-- | dungeon/src/entity.rs | 9 | ||||
| -rw-r--r-- | dungeon/src/lib.rs | 6 | ||||
| -rw-r--r-- | dungeon/src/map.rs | 2 | ||||
| -rw-r--r-- | dungeon/src/pos.rs | 2 | ||||
| -rw-r--r-- | graphics/src/lib.rs | 32 | ||||
| -rw-r--r-- | graphics/src/render.rs | 20 | ||||
| -rw-r--r-- | graphics/src/timer.rs | 2 |
9 files changed, 67 insertions, 52 deletions
@@ -53,29 +53,47 @@ default-features = false branches_sharing_code = "warn" collection_is_never_read = "warn" derive_partial_eq_without_eq = "warn" +missing_const_for_fn = "warn" +needless_collect = "warn" return_self_not_must_use = "warn" use_self = "warn" # pedantic cast_possible_wrap = "warn" cast_possible_truncation = "warn" +comparison_chain = "warn" +elidable_lifetime_names = "warn" +explicit_into_iter_loop = "warn" +explicit_iter_loop = "warn" inconsistent_struct_constructor = "warn" +large_types_passed_by_value = "warn" manual_assert = "warn" +manual_is_variant_and = "warn" +manual_let_else = "warn" map_unwrap_or = "warn" +match_bool = "warn" +needless_pass_by_ref_mut = "warn" needless_pass_by_value = "warn" redundant_closure_for_method_calls = "warn" redundant_else = "warn" semicolon_if_nothing_returned = "warn" single_match_else = "warn" +trivially_copy_pass_by_ref = "warn" uninlined_format_args = "warn" unused_self = "warn" +unnecessary_debug_formatting = "warn" +unnecessary_semicolon = "warn" unnested_or_patterns = "warn" used_underscore_binding = "warn" +used_underscore_items = "warn" +verbose_bit_mask = "warn" # restriction allow_attributes = "warn" +cfg_not_test = "deny" expect_used = "deny" shadow_reuse = "warn" +todo = "deny" unwrap_used = "deny" [profile.release] diff --git a/dungeon/src/bsp.rs b/dungeon/src/bsp.rs index db9ff99..971f5fe 100644 --- a/dungeon/src/bsp.rs +++ b/dungeon/src/bsp.rs @@ -4,7 +4,7 @@ use core::panic; use rand::prelude::IndexedRandom; use rand::{Rng, rngs::SmallRng}; -use std::cmp; // for min/max +use std::cmp::{self, Ordering}; // for min/max use crate::Floor; use crate::map::{MAP_SIZE, TILE_COUNT, Tile}; @@ -28,19 +28,19 @@ struct Rect { } // since this is all "internal", you likely have to use unit tests and not doctests impl Rect { - fn new(x: u16, y: u16, w: u16, h: u16) -> Self { + const fn new(x: u16, y: u16, w: u16, h: u16) -> Self { Self { x, y, w, h } } /// Returns the center point (cx, cy) of the rectangle as a Pos. - fn center(&self) -> Pos { + fn center(self) -> Pos { let cx = self.x + self.w / 2; let cy = self.y + self.h / 2; Pos::new(cx, cy).unwrap_or(const_pos!(1, 1)) } /// Returns a random point in this rectangle. - fn random_point(&self, rng: &mut SmallRng) -> Pos { + fn random_point(self, rng: &mut SmallRng) -> Pos { let rx = rng.random_range(self.x..(self.x + self.w)); let ry = rng.random_range(self.y..(self.y + self.h)); Pos::new(rx, ry).unwrap_or(self.center()) @@ -58,7 +58,7 @@ struct Node { } impl Node { - fn new(rect: Rect) -> Self { + const fn new(rect: Rect) -> Self { Self { rect, left: None, @@ -85,12 +85,10 @@ impl Node { } // Choose orientation: prefer the longer side - let split_h = if self.rect.w > self.rect.h { - false - } else if self.rect.h > self.rect.w { - true - } else { - rng.random_bool(0.5) + let split_h = match self.rect.w.cmp(&self.rect.h) { + Ordering::Greater => false, + Ordering::Less => true, + Ordering::Equal => rng.random(), }; // Choose split coordinate with margin so each side can contain a room @@ -251,7 +249,7 @@ impl Node { } /// Carve a room rectangle into the map (tile array) by setting tiles inside to Room. -fn carve_room(tiles: &mut [Tile; TILE_COUNT], room: &Rect) { +fn carve_room(tiles: &mut [Tile; TILE_COUNT], room: Rect) { for y in room.y..(room.y + room.h) { for x in room.x..(room.x + room.w) { let idx = x + y * MAP_SIZE; @@ -327,7 +325,7 @@ pub fn generate(rng: &mut SmallRng) -> Floor { let mut splitted_any = false; // Try splitting each leaf in order - for node_ptr in leaves.iter_mut() { + for node_ptr in &mut leaves { // Attempt to split if possible if node_ptr.split(rng) { splitted_any = true; @@ -346,8 +344,8 @@ pub fn generate(rng: &mut SmallRng) -> Floor { // Carve all rooms into the tile array let mut leaves = vec![]; root.collect_leaves(&mut leaves); - for leaf in leaves.iter() { - if let Some(room) = &leaf.room { + for leaf in &leaves { + if let Some(room) = leaf.room { carve_room(&mut tiles_box, room); } } diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs index e3b8fbd..818328c 100644 --- a/dungeon/src/entity.rs +++ b/dungeon/src/entity.rs @@ -46,7 +46,7 @@ pub enum EntityKind { impl EntityKind { /// Returns the move speed value for this type of entity in tiles/s - pub fn move_speed(&self) -> f32 { + pub const fn move_speed(&self) -> f32 { match &self { Self::Player => 5., Self::Zombie(_) => 4., @@ -190,6 +190,7 @@ impl Entity { /// let pos = Pos::new(0, 0).unwrap(); /// let player = Entity::zombie(pos); /// ``` + #[must_use] pub const fn zombie(pos: Pos) -> Self { let dir = Direction::East; let kind = EntityKind::Zombie(EnemyMoveState::new_idle()); @@ -252,9 +253,7 @@ impl Entity { EntityKind::Zombie(EnemyMoveState::new_roam(self.pos, floor, rng)); } EnemyMoveState::Roam(mut moves) => { - let p = if let Some(p) = moves.last() { - p - } else { + let Some(p) = moves.last() else { self.kind = EntityKind::Zombie(EnemyMoveState::new_idle()); return; }; @@ -340,7 +339,7 @@ impl Player { /// let pos = Pos::new(1, 2).unwrap(); /// let player = Player::new(pos); /// ``` - pub fn new(pos: Pos) -> Self { + pub const fn new(pos: Pos) -> Self { let entity = Entity::player(pos); let inventory = vec![]; Self { diff --git a/dungeon/src/lib.rs b/dungeon/src/lib.rs index 0021e81..b96cb83 100644 --- a/dungeon/src/lib.rs +++ b/dungeon/src/lib.rs @@ -74,7 +74,7 @@ impl Dungeon { /// Returns the current position of the camera (viewer) #[must_use] - pub fn camera(&self) -> FPos { + pub const fn camera(&self) -> FPos { self.player.entity.fpos } @@ -86,7 +86,7 @@ impl Dungeon { /// Returns the random number gen for the `Floor` #[must_use] - pub fn rng(&mut self) -> &mut SmallRng { + pub const fn rng(&mut self) -> &mut SmallRng { &mut self.rng } @@ -136,7 +136,7 @@ impl Dungeon { } fn act_non_players(&mut self, delta_time: f32) { - for enemy in self.enemies.iter_mut() { + for enemy in &mut self.enemies { enemy.handle_movement( self.player.entity.pos, &self.floor, diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index a28248b..4928a29 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -83,7 +83,7 @@ pub struct Floor { } impl Floor { /// Construct a floor from its components - pub fn new(tiles: Box<[Tile; TILE_COUNT]>, player_start: Pos) -> Self { + pub const fn new(tiles: Box<[Tile; TILE_COUNT]>, player_start: Pos) -> Self { Self { tiles, player_start, diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 59daaa2..16f1fe5 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -541,7 +541,7 @@ impl FPos { D::East => x.add_assign(amt), D::West if x >= amt => x.sub_assign(amt), _ => return None, - }; + } Some(Self(x, y)) } diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 2633e8d..b42845c 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -89,36 +89,42 @@ pub struct WindowBuilder<'a> { } impl<'a> WindowBuilder<'a> { /// Default window builder - pub fn new() -> Self { - Self::default() + pub const fn new() -> Self { + Self { + title: "Dungeon Crawl", + width: render::RENDER_WIDTH, + height: render::RENDER_HEIGHT, + vsync: false, + verbose: false, + } } /// Set the window title - pub fn title(&mut self, title: &'a str) -> &mut Self { + pub const fn title(&mut self, title: &'a str) -> &mut Self { self.title = title; self } /// Set the default window width - pub fn width(&mut self, width: u16) -> &mut Self { + pub const fn width(&mut self, width: u16) -> &mut Self { self.width = width; self } /// Set the default window height - pub fn height(&mut self, height: u16) -> &mut Self { + pub const fn height(&mut self, height: u16) -> &mut Self { self.height = height; self } /// Toggle vsync support - pub fn vsync(&mut self, vsync: bool) -> &mut Self { + pub const fn vsync(&mut self, vsync: bool) -> &mut Self { self.vsync = vsync; self } /// Toggle verbose logging - pub fn verbose(&mut self, verbose: bool) -> &mut Self { + pub const fn verbose(&mut self, verbose: bool) -> &mut Self { self.verbose = verbose; self } @@ -164,13 +170,7 @@ impl<'a> WindowBuilder<'a> { } impl Default for WindowBuilder<'_> { fn default() -> Self { - Self { - title: "Dungeon Crawl", - width: render::RENDER_WIDTH, - height: render::RENDER_HEIGHT, - vsync: false, - verbose: false, - } + Self::new() } } @@ -198,7 +198,7 @@ impl Window { } /// Toggles the debug UI - pub fn toggle_debug(&mut self) { + pub const fn toggle_debug(&mut self) { self.renderer.toggle_debug(); } @@ -228,7 +228,7 @@ impl Window { } /// Get audio data for the window - pub fn audio(&self) -> &Audio { + pub const fn audio(&self) -> &Audio { &self.audio } } diff --git a/graphics/src/render.rs b/graphics/src/render.rs index d450f16..bfd9e37 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -148,7 +148,7 @@ impl Textures { }) } - fn item_texture(&self, _item: &Item) -> &Texture2D { + const fn item_texture(&self, _item: Item) -> &Texture2D { // TODO: make item textures &self.error } @@ -201,7 +201,7 @@ impl Renderer { }) } - pub fn toggle_debug(&mut self) { + pub const fn toggle_debug(&mut self) { self.debug = !self.debug; } @@ -254,11 +254,11 @@ impl Renderer { // Restore the fb self.framebuffer = Some(fb); - }; + } } /// Draws the game dungeon - fn draw_dungeon<R>(&mut self, r: &mut R, dungeon: &Dungeon) + fn draw_dungeon<R>(&self, r: &mut R, dungeon: &Dungeon) where R: RaylibDraw + RaylibMode2DExt, { @@ -280,7 +280,7 @@ impl Renderer { { // Textures are up to date return; - }; + } self.tiles_hash = Some(current_hash); self.update_fg_tilemap(r, t, floor); @@ -302,7 +302,7 @@ impl Renderer { // fg layer only draws a top walls if floor.get(pos) != Tile::Wall { continue; - }; + } // draw base wall top texture rt.draw_atlas( @@ -476,7 +476,7 @@ impl Renderer { } /// Draws player HP, inventory, and floor number - fn draw_ui<R>(&mut self, r: &mut R, dungeon: &Dungeon) + fn draw_ui<R>(&self, r: &mut R, dungeon: &Dungeon) where R: RaylibDraw, { @@ -586,7 +586,7 @@ impl Renderer { ); if let Some(item) = player.inventory.get(idx as usize) { - let tex = self.textures.item_texture(item); + let tex = self.textures.item_texture(*item); const ITEM_PADDDING: u16 = UI_PADDING * 3; let dest_rec = rect! { slot_x + ITEM_PADDDING/2, @@ -607,7 +607,7 @@ impl Renderer { } /// Draw player health & equpped weapon damage - fn draw_stats<R>(&mut self, r: &mut R, player: &Player) + fn draw_stats<R>(&self, r: &mut R, player: &Player) where R: RaylibDraw, { @@ -645,7 +645,7 @@ impl Renderer { } /// Draws debug information ontop of other UI elements - fn draw_debug_ui<R>(&mut self, r: &mut R, dungeon: &Dungeon) + fn draw_debug_ui<R>(&self, r: &mut R, dungeon: &Dungeon) where R: RaylibDraw, { diff --git a/graphics/src/timer.rs b/graphics/src/timer.rs index 8310776..a8317c7 100644 --- a/graphics/src/timer.rs +++ b/graphics/src/timer.rs @@ -52,7 +52,7 @@ impl Timer { self.count += 1; } - pub fn get_frame(&self) -> u32 { + pub const fn get_frame(&self) -> u32 { self.count } |