[bitboard,core,position] Address a bunch of clippy warnings

This commit is contained in:
Eryn Wells 2024-03-14 17:00:46 -07:00
parent d0abbd8f93
commit 27a36565f3
8 changed files with 63 additions and 32 deletions

View file

@ -89,7 +89,7 @@ mod tests {
#[test] #[test]
fn leading_complex() { fn leading_complex() {
let mut scanner = LeadingBitScanner::new(0b11000101); let mut scanner = LeadingBitScanner::new(0b_1100_0101);
assert_eq!(scanner.next(), Some(7)); assert_eq!(scanner.next(), Some(7));
assert_eq!(scanner.next(), Some(6)); assert_eq!(scanner.next(), Some(6));
assert_eq!(scanner.next(), Some(2)); assert_eq!(scanner.next(), Some(2));
@ -112,7 +112,7 @@ mod tests {
#[test] #[test]
fn trailing_complex() { fn trailing_complex() {
let mut scanner = TrailingBitScanner::new(0b11000101); let mut scanner = TrailingBitScanner::new(0b_1100_0101);
assert_eq!(scanner.next(), Some(0)); assert_eq!(scanner.next(), Some(0));
assert_eq!(scanner.next(), Some(2)); assert_eq!(scanner.next(), Some(2));
assert_eq!(scanner.next(), Some(6)); assert_eq!(scanner.next(), Some(6));

View file

@ -146,18 +146,23 @@ impl BitBoard {
} }
impl BitBoard { impl BitBoard {
/// Return an Iterator over the occupied squares, starting from the leading /// Returns an Iterator over the occupied squares.
/// (most-significant bit) end of the field. ///
/// The Iterator yields squares starting from the leading (most-significant bit) end of the
/// board to the trailing (least-significant bit) end.
#[must_use]
pub fn occupied_squares(&self) -> impl Iterator<Item = Square> { pub fn occupied_squares(&self) -> impl Iterator<Item = Square> {
LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) }) LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) })
} }
/// Return an Iterator over the occupied squares, starting from the trailing /// Return an Iterator over the occupied squares, starting from the trailing
/// (least-significant bit) end of the field. /// (least-significant bit) end of the field.
#[must_use]
pub fn occupied_squares_trailing(&self) -> impl Iterator<Item = Square> { pub fn occupied_squares_trailing(&self) -> impl Iterator<Item = Square> {
TrailingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) }) TrailingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) })
} }
#[must_use]
pub fn first_occupied_square(&self) -> Option<Square> { pub fn first_occupied_square(&self) -> Option<Square> {
let leading_zeros = self.0.leading_zeros() as u8; let leading_zeros = self.0.leading_zeros() as u8;
if leading_zeros < Square::NUM as u8 { if leading_zeros < Square::NUM as u8 {
@ -167,6 +172,7 @@ impl BitBoard {
} }
} }
#[must_use]
pub fn first_occupied_square_trailing(&self) -> Option<Square> { pub fn first_occupied_square_trailing(&self) -> Option<Square> {
let trailing_zeros = self.0.trailing_zeros() as u8; let trailing_zeros = self.0.trailing_zeros() as u8;
if trailing_zeros < Square::NUM as u8 { if trailing_zeros < Square::NUM as u8 {

View file

@ -12,17 +12,21 @@ use crate::BitBoard;
use chessfriend_core::{Color, Direction, Square}; use chessfriend_core::{Color, Direction, Square};
use std::sync::OnceLock; use std::sync::OnceLock;
#[allow(clippy::identity_op)]
#[allow(clippy::erasing_op)]
pub(crate) const RANKS: [BitBoard; 8] = [ pub(crate) const RANKS: [BitBoard; 8] = [
BitBoard(0xFF << 0 * 8), BitBoard(0xFF << (0 * 8)),
BitBoard(0xFF << 1 * 8), BitBoard(0xFF << (1 * 8)),
BitBoard(0xFF << 2 * 8), BitBoard(0xFF << (2 * 8)),
BitBoard(0xFF << 3 * 8), BitBoard(0xFF << (3 * 8)),
BitBoard(0xFF << 4 * 8), BitBoard(0xFF << (4 * 8)),
BitBoard(0xFF << 5 * 8), BitBoard(0xFF << (5 * 8)),
BitBoard(0xFF << 6 * 8), BitBoard(0xFF << (6 * 8)),
BitBoard(0xFF << 7 * 8), BitBoard(0xFF << (7 * 8)),
]; ];
#[allow(clippy::identity_op)]
#[allow(clippy::erasing_op)]
pub(crate) const FILES: [BitBoard; 8] = [ pub(crate) const FILES: [BitBoard; 8] = [
BitBoard(0x0101010101010101 << 0), BitBoard(0x0101010101010101 << 0),
BitBoard(0x0101010101010101 << 1), BitBoard(0x0101010101010101 << 1),

View file

@ -3,8 +3,9 @@
use crate::{errors::TryFromCharError, try_from_string}; use crate::{errors::TryFromCharError, try_from_string};
use std::fmt; use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum Color { pub enum Color {
#[default]
White = 0, White = 0,
Black = 1, Black = 1,
} }
@ -16,7 +17,9 @@ impl Color {
Color::ALL.iter() Color::ALL.iter()
} }
pub fn other(&self) -> Color { /// The other color
#[must_use]
pub const fn other(&self) -> Color {
match self { match self {
Color::White => Color::Black, Color::White => Color::Black,
Color::Black => Color::White, Color::Black => Color::White,
@ -24,12 +27,6 @@ impl Color {
} }
} }
impl Default for Color {
fn default() -> Self {
Color::White
}
}
impl fmt::Display for Color { impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(

View file

@ -34,7 +34,7 @@ impl Shape {
PROMOTABLE_SHAPES.iter() PROMOTABLE_SHAPES.iter()
} }
const fn to_ascii(&self) -> char { const fn to_ascii(self) -> char {
match self { match self {
Shape::Pawn => 'P', Shape::Pawn => 'P',
Shape::Knight => 'N', Shape::Knight => 'N',
@ -79,7 +79,7 @@ impl Into<char> for Shape {
impl fmt::Display for Shape { impl fmt::Display for Shape {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let self_char: char = self.into(); let self_char: char = self.into();
write!(f, "{}", self_char) write!(f, "{self_char}")
} }
} }
@ -91,6 +91,7 @@ pub struct Piece {
macro_rules! piece_constructor { macro_rules! piece_constructor {
($func_name:ident, $type:tt) => { ($func_name:ident, $type:tt) => {
#[must_use]
pub fn $func_name(color: Color) -> Piece { pub fn $func_name(color: Color) -> Piece {
Piece { Piece {
color, color,
@ -102,6 +103,7 @@ macro_rules! piece_constructor {
macro_rules! is_shape { macro_rules! is_shape {
($func_name:ident, $shape:ident) => { ($func_name:ident, $shape:ident) => {
#[must_use]
pub fn $func_name(&self) -> bool { pub fn $func_name(&self) -> bool {
self.shape == Shape::$shape self.shape == Shape::$shape
} }
@ -109,6 +111,7 @@ macro_rules! is_shape {
} }
impl Piece { impl Piece {
#[must_use]
pub fn new(color: Color, shape: Shape) -> Piece { pub fn new(color: Color, shape: Shape) -> Piece {
Piece { color, shape } Piece { color, shape }
} }
@ -120,10 +123,12 @@ impl Piece {
piece_constructor!(queen, Queen); piece_constructor!(queen, Queen);
piece_constructor!(king, King); piece_constructor!(king, King);
#[must_use]
pub fn color(&self) -> Color { pub fn color(&self) -> Color {
self.color self.color
} }
#[must_use]
pub fn shape(&self) -> Shape { pub fn shape(&self) -> Shape {
self.shape self.shape
} }
@ -135,7 +140,8 @@ impl Piece {
is_shape!(is_queen, Queen); is_shape!(is_queen, Queen);
is_shape!(is_king, King); is_shape!(is_king, King);
pub fn to_ascii(&self) -> char { #[must_use]
pub fn to_ascii(self) -> char {
let ch = self.shape.to_ascii(); let ch = self.shape.to_ascii();
match self.color { match self.color {
Color::White => ch, Color::White => ch,
@ -143,7 +149,8 @@ impl Piece {
} }
} }
fn to_unicode(&self) -> char { #[must_use]
fn to_unicode(self) -> char {
match (self.color, self.shape) { match (self.color, self.shape) {
(Color::Black, Shape::Pawn) => '♟', (Color::Black, Shape::Pawn) => '♟',
(Color::Black, Shape::Knight) => '♞', (Color::Black, Shape::Knight) => '♞',
@ -175,6 +182,7 @@ pub struct PlacedPiece {
macro_rules! is_shape { macro_rules! is_shape {
($func_name:ident, $shape:ident) => { ($func_name:ident, $shape:ident) => {
#[must_use]
pub fn $func_name(&self) -> bool { pub fn $func_name(&self) -> bool {
self.piece().shape == Shape::$shape self.piece().shape == Shape::$shape
} }
@ -182,26 +190,35 @@ macro_rules! is_shape {
} }
impl PlacedPiece { impl PlacedPiece {
#[must_use]
pub const fn new(piece: Piece, square: Square) -> PlacedPiece { pub const fn new(piece: Piece, square: Square) -> PlacedPiece {
PlacedPiece { piece, square } PlacedPiece { piece, square }
} }
/// The [Piece] itself
#[inline] #[inline]
#[must_use]
pub fn piece(&self) -> &Piece { pub fn piece(&self) -> &Piece {
&self.piece &self.piece
} }
/// The square the piece is on
#[inline] #[inline]
#[must_use]
pub fn square(&self) -> Square { pub fn square(&self) -> Square {
self.square self.square
} }
/// The piece's [Color]
#[inline] #[inline]
#[must_use]
pub fn color(&self) -> Color { pub fn color(&self) -> Color {
self.piece.color self.piece.color
} }
/// The piece's [Shape]
#[inline] #[inline]
#[must_use]
pub fn shape(&self) -> Shape { pub fn shape(&self) -> Shape {
self.piece.shape self.piece.shape
} }
@ -213,6 +230,7 @@ impl PlacedPiece {
is_shape!(is_queen, Queen); is_shape!(is_queen, Queen);
is_shape!(is_king, King); is_shape!(is_king, King);
#[must_use]
pub fn is_kingside_rook(&self) -> bool { pub fn is_kingside_rook(&self) -> bool {
self.is_rook() self.is_rook()
&& match self.color() { && match self.color() {
@ -221,6 +239,7 @@ impl PlacedPiece {
} }
} }
#[must_use]
pub fn is_queenside_rook(&self) -> bool { pub fn is_queenside_rook(&self) -> bool {
self.is_rook() self.is_rook()
&& match self.color() { && match self.color() {

View file

@ -1,10 +1,9 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::sight::SliderRayToSquareExt;
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Shape; use chessfriend_core::Shape;
use crate::sight::SliderRayToSquareExt;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct CheckingPieces { pub struct CheckingPieces {
bitboards: [BitBoard; 5], bitboards: [BitBoard; 5],
@ -23,12 +22,12 @@ impl CheckingPieces {
} }
} }
/// The number of checking pieces.
pub fn count(&self) -> u32 { pub fn count(&self) -> u32 {
self.bitboards.iter().map(BitBoard::population_count).sum() self.bitboards.iter().map(BitBoard::population_count).sum()
} }
/// A BitBoard representing the set of pieces that must be captured to /// A BitBoard representing the set of pieces that must be captured to resolve check.
/// resolve check.
pub fn capture_mask(&self) -> BitBoard { pub fn capture_mask(&self) -> BitBoard {
if self.count() == 0 { if self.count() == 0 {
BitBoard::FULL BitBoard::FULL
@ -39,8 +38,8 @@ impl CheckingPieces {
} }
} }
/// A BitBoard representing the set of squares to which a player can move a /// A BitBoard representing the set of squares to which a player can move a piece to block a
/// piece to block a checking piece. /// checking piece.
pub fn push_mask(&self, king: &BitBoard) -> BitBoard { pub fn push_mask(&self, king: &BitBoard) -> BitBoard {
let target = king.first_occupied_square().unwrap(); let target = king.first_occupied_square().unwrap();

View file

@ -78,7 +78,7 @@ impl PieceBitBoards {
} }
pub(super) fn place_piece(&mut self, piece: &PlacedPiece) -> Result<(), PlacePieceError> { pub(super) fn place_piece(&mut self, piece: &PlacedPiece) -> Result<(), PlacePieceError> {
self.place_piece_with_strategy(piece, Default::default()) self.place_piece_with_strategy(piece, PlacePieceStrategy::default())
} }
pub(super) fn place_piece_with_strategy( pub(super) fn place_piece_with_strategy(

View file

@ -138,22 +138,27 @@ impl Position {
}) })
} }
/// Return a BitBoard representing the set of squares containing a piece. /// A [BitBoard] representing the set of squares containing a piece.
#[inline] #[inline]
#[must_use]
pub(crate) fn occupied_squares(&self) -> &BitBoard { pub(crate) fn occupied_squares(&self) -> &BitBoard {
&self.pieces.all_pieces() &self.pieces.all_pieces()
} }
#[inline] #[inline]
#[must_use]
pub(crate) fn friendly_pieces(&self) -> &BitBoard { pub(crate) fn friendly_pieces(&self) -> &BitBoard {
self.pieces.all_pieces_of_color(self.color_to_move) self.pieces.all_pieces_of_color(self.color_to_move)
} }
#[inline] #[inline]
#[must_use]
pub(crate) fn opposing_pieces(&self) -> &BitBoard { pub(crate) fn opposing_pieces(&self) -> &BitBoard {
self.pieces.all_pieces_of_color(self.color_to_move.other()) self.pieces.all_pieces_of_color(self.color_to_move.other())
} }
#[inline]
#[must_use]
pub(crate) fn all_pieces(&self) -> (&BitBoard, &BitBoard) { pub(crate) fn all_pieces(&self) -> (&BitBoard, &BitBoard) {
(self.friendly_pieces(), self.opposing_pieces()) (self.friendly_pieces(), self.opposing_pieces())
} }
@ -161,6 +166,7 @@ impl Position {
/// Return a BitBoard representing the set of squares containing a piece. /// Return a BitBoard representing the set of squares containing a piece.
/// This set is the inverse of `occupied_squares`. /// This set is the inverse of `occupied_squares`.
#[inline] #[inline]
#[must_use]
pub(crate) fn empty_squares(&self) -> BitBoard { pub(crate) fn empty_squares(&self) -> BitBoard {
!self.occupied_squares() !self.occupied_squares()
} }