From 119f399623aedc8e974ab86aa8fc37f7b7bcc882 Mon Sep 17 00:00:00 2001 From: Jonathan Gilchrist Date: Thu, 31 Oct 2024 14:49:01 +0000 Subject: [PATCH] Fix some Clippy lints --- src/chess/fen/fen_writer.rs | 17 +++++++---------- src/chess/perft.rs | 5 ++--- src/chess/san/san_parser.rs | 5 ++--- src/engine/tablebases/mod.rs | 30 +++++++++++++++++++----------- src/engine/uci/mod.rs | 18 +++++++++++------- src/engine/uci/options.rs | 4 ++-- 6 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/chess/fen/fen_writer.rs b/src/chess/fen/fen_writer.rs index 119096b8..1ebe81a1 100644 --- a/src/chess/fen/fen_writer.rs +++ b/src/chess/fen/fen_writer.rs @@ -23,12 +23,10 @@ const fn format_piece(piece: Piece) -> char { } fn format_rank(rank: &[Option]) -> String { - let state = rank - .iter() - .fold((String::new(), 0), |acc, piece| match piece { - Some(p) => { - let (str_so_far, prev_empty_squares) = acc; - + let state = rank.iter().fold( + (String::new(), 0), + |(str_so_far, prev_empty_squares), piece| { + if let Some(p) = piece { let new_string = format!( "{}{}{}", str_so_far, @@ -41,12 +39,11 @@ fn format_rank(rank: &[Option]) -> String { ); (new_string, 0) - } - None => { - let (str_so_far, prev_empty_squares) = acc; + } else { (str_so_far, prev_empty_squares + 1) } - }); + }, + ); let (str_so_far, prev_empty_squares) = state; format!( diff --git a/src/chess/perft.rs b/src/chess/perft.rs index 785edd68..9ceaf58f 100644 --- a/src/chess/perft.rs +++ b/src/chess/perft.rs @@ -7,10 +7,9 @@ pub fn perft(depth: u8, game: &mut Game) -> usize { } game.moves() - .to_vec() - .into_iter() + .iter() .map(|m| { - game.make_move(m); + game.make_move(*m); let result = perft(depth - 1, game); game.undo_move(); result diff --git a/src/chess/san/san_parser.rs b/src/chess/san/san_parser.rs index 8e2c1c38..10946ce6 100644 --- a/src/chess/san/san_parser.rs +++ b/src/chess/san/san_parser.rs @@ -119,9 +119,8 @@ fn parse_piece(c: char) -> Option { fn parse_source_square(game: &Game, src: &str, dst: Square) -> Result { let piece_moves: Vec<(PieceKind, Move)> = game .moves() - .to_vec() - .into_iter() - .map(|mv| (game.board.piece_at(mv.src).unwrap().kind, mv)) + .iter() + .map(|mv| (game.board.piece_at(mv.src).unwrap().kind, *mv)) .collect(); // Pawn move diff --git a/src/engine/tablebases/mod.rs b/src/engine/tablebases/mod.rs index 6ba6e133..3961d9fb 100644 --- a/src/engine/tablebases/mod.rs +++ b/src/engine/tablebases/mod.rs @@ -23,42 +23,50 @@ impl Tablebase { } } - #[expect(unused)] + #[expect( + unused, + reason = "Won't be used until proper tablebase support is implemented" + )] pub fn is_enabled(&self) -> bool { self.is_enabled } - #[expect(unused)] + #[expect( + unused, + reason = "Won't be used until proper tablebase support is implemented" + )] pub fn n_men(&self) -> usize { self.shakmaty_tb.max_pieces() } - pub fn set_paths(&mut self, path: &str) -> Result<(), ()> { + pub fn set_paths(&mut self, path: &str) { let path = path.to_string(); let separator = if cfg!(windows) { ';' } else { ':' }; - let paths = path.split(separator).map(|p| Path::new(p)); + let paths = path.split(separator).map(Path::new); for p in paths { - self.shakmaty_tb.add_directory(p).expect(&format!( - "Invalid tablebase path: {}", - p.to_str().unwrap_or_default() - )); + self.shakmaty_tb.add_directory(p).unwrap_or_else(|_| { + panic!("Invalid tablebase path: {}", p.to_str().unwrap_or_default()) + }); } self.is_enabled = true; - Ok(()) } - #[expect(unused)] + #[expect( + unused, + reason = "Won't be used until proper tablebase support is implemented" + )] pub fn wdl(&self, game: &Game) -> Option { + use shakmaty_syzygy::Wdl::*; + if !self.is_enabled { return None; } let shakmaty_pos = Self::pos_to_shakmaty_pos(game); - use shakmaty_syzygy::Wdl::*; match self.shakmaty_tb.probe_wdl(&shakmaty_pos) { Ok(m) => m.unambiguous().map(|wdl| match wdl { Loss => Wdl::Loss, diff --git a/src/engine/uci/mod.rs b/src/engine/uci/mod.rs index cecc5fdb..3d90e340 100644 --- a/src/engine/uci/mod.rs +++ b/src/engine/uci/mod.rs @@ -53,7 +53,14 @@ impl UciReporter { depth: Some(progress.depth), seldepth: Some(progress.seldepth), score: Some(score), - pv: Some(progress.pv.clone().into_iter().map(|m| m.into()).collect()), + pv: Some( + progress + .pv + .clone() + .into_iter() + .map(std::convert::Into::into) + .collect(), + ), time: Some(progress.stats.time), nodes: Some(progress.stats.nodes), nps: Some(progress.stats.nodes_per_second), @@ -126,7 +133,7 @@ impl UciReporter { ); print!(" "); - for mv in progress.pv.clone().into_iter() { + for mv in progress.pv.clone() { let san_mv = san::format_move(&game, mv); print!( @@ -238,13 +245,10 @@ impl Uci { options::MoveOverheadOption::set(&mut self.options, value) } options::SyzygyPath::NAME => { - let syzygy_path = options::SyzygyPath::set(&mut self.options, value)?; + let syzygy_path = options::SyzygyPath::set(&mut self.options, value); if let Ok(mut state_handle) = self.persistent_state.try_lock() { - state_handle - .tablebase - .set_paths(&syzygy_path) - .map_err(|()| "Invalid SyzygyPath")?; + state_handle.tablebase.set_paths(&syzygy_path); } else { self.reporter .generic_report("error: Unable to change SyzygyPath during search"); diff --git a/src/engine/uci/options.rs b/src/engine/uci/options.rs index bec31374..6dbb2f1d 100644 --- a/src/engine/uci/options.rs +++ b/src/engine/uci/options.rs @@ -94,9 +94,9 @@ impl UciOption for SyzygyPath { } impl SyzygyPath { - pub fn set(options: &mut EngineOptions, value: &str) -> Result { + pub fn set(options: &mut EngineOptions, value: &str) -> String { let path = value.to_string(); options.syzygy_path = Some(path.clone()); - Ok(path) + path } }