Skip to content

Commit

Permalink
Fix some Clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
jgilchrist committed Oct 31, 2024
1 parent 5c63669 commit 119f399
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
17 changes: 7 additions & 10 deletions src/chess/fen/fen_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ const fn format_piece(piece: Piece) -> char {
}

fn format_rank(rank: &[Option<Piece>]) -> 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,
Expand All @@ -41,12 +39,11 @@ fn format_rank(rank: &[Option<Piece>]) -> 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!(
Expand Down
5 changes: 2 additions & 3 deletions src/chess/perft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/chess/san/san_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ fn parse_piece(c: char) -> Option<PieceKind> {
fn parse_source_square(game: &Game, src: &str, dst: Square) -> Result<Square, ParseError> {
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
Expand Down
30 changes: 19 additions & 11 deletions src/engine/tablebases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Wdl> {
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,
Expand Down
18 changes: 11 additions & 7 deletions src/engine/uci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/engine/uci/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ impl UciOption for SyzygyPath {
}

impl SyzygyPath {
pub fn set(options: &mut EngineOptions, value: &str) -> Result<String, String> {
pub fn set(options: &mut EngineOptions, value: &str) -> String {
let path = value.to_string();
options.syzygy_path = Some(path.clone());
Ok(path)
path
}
}

0 comments on commit 119f399

Please sign in to comment.