From 337211e7dbc82e0a61f2200ffdf5fc6692e527d8 Mon Sep 17 00:00:00 2001 From: "Tristan F." Date: Thu, 26 Sep 2024 15:00:05 -0700 Subject: [PATCH] style: clippy --- crates/game-solver/src/lib.rs | 22 +++++++++------------- crates/games/src/chomp/mod.rs | 2 +- crates/games/src/domineering/mod.rs | 2 +- crates/games/src/nim/mod.rs | 4 ++-- crates/games/src/order_and_chaos/mod.rs | 5 ++--- crates/games/src/reversi/mod.rs | 2 +- crates/games/src/sprouts/mod.rs | 16 ++++++---------- crates/games/src/tic_tac_toe/mod.rs | 10 +++++----- crates/games/src/util/cli/human.rs | 5 ++--- crates/nimnim/src/lib.rs | 4 +++- 10 files changed, 32 insertions(+), 40 deletions(-) diff --git a/crates/game-solver/src/lib.rs b/crates/game-solver/src/lib.rs index 180644b..0d56444 100644 --- a/crates/game-solver/src/lib.rs +++ b/crates/game-solver/src/lib.rs @@ -83,12 +83,10 @@ fn negamax + Eq + Hash>( } else { stats.terminal_ends.losing.fetch_add(1, Ordering::Relaxed); } + } else if stats.original_player == winning_player { + stats.terminal_ends.winning.fetch_add(1, Ordering::Relaxed); } else { - if stats.original_player == winning_player { - stats.terminal_ends.winning.fetch_add(1, Ordering::Relaxed); - } else { - stats.terminal_ends.losing.fetch_add(1, Ordering::Relaxed); - } + stats.terminal_ends.losing.fetch_add(1, Ordering::Relaxed); } } @@ -131,12 +129,10 @@ fn negamax + Eq + Hash>( } else { stats.terminal_ends.losing.fetch_add(1, Ordering::Relaxed); } + } else if stats.original_player == winning_player { + stats.terminal_ends.winning.fetch_add(1, Ordering::Relaxed); } else { - if stats.original_player == winning_player { - stats.terminal_ends.winning.fetch_add(1, Ordering::Relaxed); - } else { - stats.terminal_ends.losing.fetch_add(1, Ordering::Relaxed); - } + stats.terminal_ends.losing.fetch_add(1, Ordering::Relaxed); } } @@ -197,7 +193,7 @@ fn negamax + Eq + Hash>( -beta, -alpha, stats, - &cancellation_token, + cancellation_token, )? } else { let score = -negamax( @@ -206,7 +202,7 @@ fn negamax + Eq + Hash>( -alpha - 1, -alpha, stats, - &cancellation_token, + cancellation_token, )?; if score > alpha { -negamax( @@ -215,7 +211,7 @@ fn negamax + Eq + Hash>( -beta, -alpha, stats, - &cancellation_token, + cancellation_token, )? } else { score diff --git a/crates/games/src/chomp/mod.rs b/crates/games/src/chomp/mod.rs index 860c5c6..1be3392 100644 --- a/crates/games/src/chomp/mod.rs +++ b/crates/games/src/chomp/mod.rs @@ -146,7 +146,7 @@ impl Display for Chomp { impl Debug for Chomp { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - ::fmt(&self, f) + ::fmt(self, f) } } diff --git a/crates/games/src/domineering/mod.rs b/crates/games/src/domineering/mod.rs index 4850dfe..66e91cd 100644 --- a/crates/games/src/domineering/mod.rs +++ b/crates/games/src/domineering/mod.rs @@ -217,7 +217,7 @@ impl Display for Domineering Debug for Domineering { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - ::fmt(&self, f) + ::fmt(self, f) } } diff --git a/crates/games/src/nim/mod.rs b/crates/games/src/nim/mod.rs index 7ffdda2..edb11f6 100644 --- a/crates/games/src/nim/mod.rs +++ b/crates/games/src/nim/mod.rs @@ -128,7 +128,7 @@ impl Display for Nim { impl Debug for Nim { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ::fmt(&self, f) + ::fmt(self, f) } } @@ -197,7 +197,7 @@ mod tests { #[test] fn max_moves_is_heap_sum() { assert_eq!(Nim::new(vec![3, 5, 7]).max_moves(), Some(3 + 5 + 7)); - assert_eq!(Nim::new(vec![0, 2, 2]).max_moves(), Some(0 + 2 + 2)); + assert_eq!(Nim::new(vec![0, 2, 2]).max_moves(), Some(2 + 2)); } #[test] diff --git a/crates/games/src/order_and_chaos/mod.rs b/crates/games/src/order_and_chaos/mod.rs index dff784a..3061439 100644 --- a/crates/games/src/order_and_chaos/mod.rs +++ b/crates/games/src/order_and_chaos/mod.rs @@ -291,7 +291,7 @@ impl< > Debug for OrderAndChaos { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - ::fmt(&self, f) + ::fmt(self, f) } } @@ -347,14 +347,13 @@ mod tests { fn from_string(string: &str) -> OrderAndChaos<6, 6, 5, 6> { let board_internal = string .chars() - .map(|ch| match ch { + .filter_map(|ch| match ch { 'X' => Some(Some(CellType::X)), 'O' => Some(Some(CellType::O)), '.' => Some(None), '\n' => None, _ => panic!("There shouldn't be other characters in the string!"), }) - .filter_map(|x| x) .collect::>(); let element_count = board_internal.iter().filter(|x| x.is_some()).count(); diff --git a/crates/games/src/reversi/mod.rs b/crates/games/src/reversi/mod.rs index 6f00f73..3761e83 100644 --- a/crates/games/src/reversi/mod.rs +++ b/crates/games/src/reversi/mod.rs @@ -236,7 +236,7 @@ impl fmt::Display for Reversi { impl Debug for Reversi { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - ::fmt(&self, f) + ::fmt(self, f) } } diff --git a/crates/games/src/sprouts/mod.rs b/crates/games/src/sprouts/mod.rs index cc22d82..562a845 100644 --- a/crates/games/src/sprouts/mod.rs +++ b/crates/games/src/sprouts/mod.rs @@ -63,10 +63,6 @@ impl PartialEq for Sprouts { && self.0.edge_references().collect::>() == other.0.edge_references().collect::>() } - - fn ne(&self, other: &Self) -> bool { - !self.eq(other) - } } impl Eq for Sprouts {} @@ -119,7 +115,7 @@ impl Game for Sprouts { fn make_move(&mut self, m: &Self::Move) -> Result<(), Self::MoveError> { // There already exists an edge here! if self.0.has_edge(m.from, m.to) { - return Err(SproutsMoveError::SproutsConnected(m.clone())); + return Err(SproutsMoveError::SproutsConnected(*m)); } // move index is out of bounds @@ -127,14 +123,14 @@ impl Game for Sprouts { if !self.0.node_identifiers().contains(&m.from) { return Err(SproutsMoveError::MoveOutOfBounds( m.from.index().try_into().unwrap(), - m.clone(), + *m, )); } if !self.0.node_identifiers().contains(&m.to) { return Err(SproutsMoveError::MoveOutOfBounds( m.to.index().try_into().unwrap(), - m.clone(), + *m, )); } } @@ -144,14 +140,14 @@ impl Game for Sprouts { if self.0.edges(m.from).count() >= MAX_SPROUTS { return Err(SproutsMoveError::DeadSprout( m.from.index().try_into().unwrap(), - m.clone(), + *m, )); } if self.0.edges(m.to).count() >= MAX_SPROUTS { return Err(SproutsMoveError::DeadSprout( m.to.index().try_into().unwrap(), - m.clone(), + *m, )); } } @@ -244,7 +240,7 @@ impl Debug for Sprouts { impl Display for Sprouts { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ::fmt(&self, f) + ::fmt(self, f) } } diff --git a/crates/games/src/tic_tac_toe/mod.rs b/crates/games/src/tic_tac_toe/mod.rs index 5c534a5..262cdf6 100644 --- a/crates/games/src/tic_tac_toe/mod.rs +++ b/crates/games/src/tic_tac_toe/mod.rs @@ -141,7 +141,7 @@ impl TicTacToe { fn winning_line(&self, point: &Dim, offset: &[i32]) -> Option { let square = self.board.get(point).unwrap(); - if *square == None { + if square.is_none() { return None; } @@ -204,7 +204,7 @@ impl Game for TicTacToe { // check every move for (index, square) in self.board.indexed_iter() { - if square == &None { + if square.is_none() { continue; } @@ -220,7 +220,7 @@ impl Game for TicTacToe { } fn make_move(&mut self, m: &Self::Move) -> Result<(), Self::MoveError> { - if *self.board.get(m.0.clone()).unwrap() == None { + if self.board.get(m.0.clone()).unwrap().is_none() { let square = Square::from_player(self.player()); *self.board.get_mut(m.0.clone()).unwrap() = Some(square); @@ -235,7 +235,7 @@ impl Game for TicTacToe { self.board .indexed_iter() .filter_map(move |(index, square)| { - if square == &None { + if square.is_none() { Some(TicTacToeMove(index)) } else { None @@ -320,7 +320,7 @@ impl Display for TicTacToe { impl Debug for TicTacToe { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - ::fmt(&self, f) + ::fmt(self, f) } } diff --git a/crates/games/src/util/cli/human.rs b/crates/games/src/util/cli/human.rs index f3a2b24..0c0b7c4 100644 --- a/crates/games/src/util/cli/human.rs +++ b/crates/games/src/util/cli/human.rs @@ -69,9 +69,8 @@ impl App { } fn handle_key_event(&mut self, key_event: KeyEvent) { - match key_event.code { - KeyCode::Char('q') => self.exit(), - _ => {} + if let KeyCode::Char('q') = key_event.code { + self.exit() } } diff --git a/crates/nimnim/src/lib.rs b/crates/nimnim/src/lib.rs index 22100ae..ffc4023 100644 --- a/crates/nimnim/src/lib.rs +++ b/crates/nimnim/src/lib.rs @@ -7,7 +7,9 @@ impl Add for Nimber { type Output = Nimber; fn add(self, rhs: Self) -> Self::Output { - return Nimber(self.0 ^ rhs.0); + // bitxor is the way nimbers are added. + #[allow(clippy::suspicious_arithmetic_impl)] + Nimber(self.0 ^ rhs.0) } }