Refactor using Clippy nursery and unwrap_used

This commit is contained in:
2024-05-29 21:30:19 +03:00
parent b471aaf109
commit 27cc4cb1ad
6 changed files with 70 additions and 55 deletions

View File

@@ -13,6 +13,6 @@ rand = { version = "0.8.5", features = ["small_rng"] }
[lints.clippy]
enum_glob_use = "deny"
#pedantic = "deny"
#nursery = "deny"
#unwrap_used = "deny"
# pedantic = "deny"
nursery = "deny"
unwrap_used = "deny"

View File

@@ -36,7 +36,7 @@ static mut KING_ATTACKS: [Bitboard; 64] = [0; 64];
static mut BISHOP_ATTACKS: [[Bitboard; 512]; 64] = [[0; 512]; 64];
static mut ROOK_ATTACKS: [[Bitboard; 4096]; 64] = [[0; 4096]; 64];
fn pawn_attacks(bitboard: Bitboard, color: Color) -> Bitboard {
const fn pawn_attacks(bitboard: Bitboard, color: Color) -> Bitboard {
let mut attacks = 0_u64;
match color {
@@ -53,7 +53,7 @@ fn pawn_attacks(bitboard: Bitboard, color: Color) -> Bitboard {
attacks
}
fn knight_attacks(bitboard: Bitboard) -> Bitboard {
const fn knight_attacks(bitboard: Bitboard) -> Bitboard {
let mut attacks = 0_u64;
attacks |= (bitboard & NOT_AB_FILE) << (6);
@@ -68,7 +68,7 @@ fn knight_attacks(bitboard: Bitboard) -> Bitboard {
attacks
}
fn king_attacks(bitboard: Bitboard) -> Bitboard {
const fn king_attacks(bitboard: Bitboard) -> Bitboard {
let mut attacks = 0_u64;
attacks |= (bitboard & NOT_H_FILE) << (1);
@@ -85,12 +85,16 @@ fn king_attacks(bitboard: Bitboard) -> Bitboard {
pub fn mask_bishop_attacks(bitboard: Bitboard) -> Bitboard {
let mut attacks = 0_u64;
let target_rank = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
let target_file = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
let target_rank = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() / 8
};
let target_file = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() % 8
};
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
for (rank, file) in (rank..=6).zip(file..=6) {
@@ -117,12 +121,16 @@ pub fn mask_bishop_attacks(bitboard: Bitboard) -> Bitboard {
pub fn mask_rook_attacks(bitboard: Bitboard) -> Bitboard {
let mut attacks = 0_u64;
let target_rank = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
let target_file = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
let target_rank = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() / 8
};
let target_file = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() % 8
};
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
for rank in rank..=6 {
@@ -145,12 +153,16 @@ pub fn mask_rook_attacks(bitboard: Bitboard) -> Bitboard {
pub fn bishop_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitboard {
let mut attacks = 0_u64;
let target_rank = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
let target_file = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
let target_rank = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() / 8
};
let target_file = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() % 8
};
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
for (rank, file) in (rank..=7).zip(file..=7) {
@@ -193,12 +205,16 @@ pub fn bishop_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitbo
pub fn rook_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitboard {
let mut attacks = 0_u64;
let target_rank = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
let target_file = (bitboard == 0)
.then(|| 0)
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
let target_rank = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() / 8
};
let target_file = if bitboard == 0 {
0
} else {
bitboard.trailing_zeros() % 8
};
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
for rank in rank..=7 {

View File

@@ -1,15 +1,15 @@
use std::fmt;
use u64 as Bitboard;
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct Board {
white_pieces: [Piece; 6],
black_pieces: [Piece; 6],
}
impl Board {
pub fn new() -> Board {
Board {
pub const fn new() -> Self {
Self {
white_pieces: [
Piece::new(0xff00, Kind::Pawn, Color::White),
Piece::new(0x81, Kind::Rook, Color::White),
@@ -29,8 +29,8 @@ impl Board {
}
}
pub fn empty_board() -> Board {
Board {
pub const fn empty_board() -> Self {
Self {
white_pieces: [
Piece::new(0x0, Kind::Pawn, Color::White),
Piece::new(0x0, Kind::Rook, Color::White),
@@ -85,7 +85,7 @@ impl fmt::Display for Board {
}
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct Piece {
pub bitboard: Bitboard,
pub kind: Kind,
@@ -93,8 +93,8 @@ pub struct Piece {
}
impl Piece {
pub fn new(bitboard: Bitboard, kind: Kind, color: Color) -> Piece {
Piece {
pub const fn new(bitboard: Bitboard, kind: Kind, color: Color) -> Self {
Self {
bitboard,
kind,
color,
@@ -116,7 +116,7 @@ impl fmt::Display for Piece {
}
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Kind {
Pawn,
Rook,
@@ -126,7 +126,7 @@ pub enum Kind {
King,
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Color {
White,
Black,

View File

@@ -123,9 +123,9 @@ fn en_passant_target_square(square: &str) -> Result<u8, FenError> {
match sqr.next() {
Some(file) if files.contains_key(&file) => match sqr.next() {
Some(rank) if ranks.contains_key(&rank) => {
Ok(ranks.get(&rank).unwrap() * 8 + files.get(&file).unwrap())
}
Some(rank) if ranks.contains_key(&rank) => Ok(ranks.get(&rank).expect("Invalid rank")
* 8
+ files.get(&file).expect("Invalid file")),
Some(_) | None => Err(FenError::from(
"Not a valid rank (3 or 6) for an en passant target square",
)),

View File

@@ -4,25 +4,25 @@ use crate::{
};
use String as FenError;
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct Game {
pub board: Board,
pub state: State,
}
impl Game {
pub fn new() -> Game {
Game {
pub const fn new() -> Self {
Self {
board: Board::new(),
state: State::new(),
}
}
pub fn new_from_fen(fen: &str) -> Result<Game, FenError> {
pub fn new_from_fen(fen: &str) -> Result<Self, FenError> {
from_fen(fen)
}
pub fn run(&self) {
pub const fn run(&self) {
Board::new();
}
}
@@ -33,7 +33,7 @@ impl Default for Game {
}
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct State {
side_to_move: Color,
castling_ability: u8,
@@ -43,8 +43,8 @@ pub struct State {
}
impl State {
pub fn new() -> State {
State {
pub const fn new() -> Self {
Self {
side_to_move: Color::White,
castling_ability: 0b1111,
en_passant_target_square: 0,
@@ -53,14 +53,14 @@ impl State {
}
}
pub fn load_state(
pub const fn load_state(
side_to_move: Color,
castling_ability: u8,
en_passant_target_square: u8,
halfmove_clock: u8,
fullmove_counter: u8,
) -> State {
State {
) -> Self {
Self {
side_to_move,
castling_ability,
en_passant_target_square,

View File

@@ -3,7 +3,6 @@ pub mod board;
pub mod fen;
pub mod game;
pub mod magic;
pub mod r#move;
use game::Game;