[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

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

View file

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