Skip to content

Commit

Permalink
fix some clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Emerentius committed Dec 2, 2023
1 parent e60cb01 commit 7901820
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/board/grid_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct GridState([CellState; 81]);
impl std::fmt::Display for GridState {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
let mut column_widths = [0; 9];
#[allow(clippy::needless_range_loop)]
for col in 0..9 {
let max_width = (0..9)
.map(|row| match self.0[row * 9 + col] {
Expand Down
12 changes: 9 additions & 3 deletions src/board/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ define_types!(
Chute: 6,
);

impl Cell {
pub(crate) fn from_coords(row: u8, col: u8) -> Self {
Cell::new(row * 9 + col)
}
}

/// A [`Row`] or [`Col`]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub enum LineType {
Expand Down Expand Up @@ -604,13 +610,13 @@ pub trait CellAt: Sized {

impl CellAt for Row {
fn cell_at(self, pos: Position<Row>) -> Cell {
Cell::new(self.0 * 9 + pos.0)
Cell::from_coords(self.0, pos.0)
}
}

impl CellAt for Col {
fn cell_at(self, pos: Position<Col>) -> Cell {
Cell::new(pos.0 * 9 + self.0)
Cell::from_coords(pos.0, self.0)
}
}

Expand All @@ -623,7 +629,7 @@ impl CellAt for Block {
let col_in_stack = pos.0 % 3;
let row = band * 3 + row_in_band;
let col = stack * 3 + col_in_stack;
Cell::new(row * 9 + col)
Cell::from_coords(row, col)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/board/sudoku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ impl Sudoku {
let mut nums_contained: u16 = 0;
// same with less than 17 clues
let mut n_clues = 0;
self.iter().filter_map(|id| id).for_each(|num| {
self.iter().flatten().for_each(|num| {
nums_contained |= 1 << num;
n_clues += 1;
});
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
clippy::unreadable_literal,
clippy::wrong_self_convention,
clippy::inconsistent_digit_grouping,
clippy::unusual_byte_groupings,
clippy::too_many_arguments
)]
//! Utilities for classical 9x9 sudokus.
Expand Down
2 changes: 2 additions & 0 deletions src/strategy/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct StrategySolver {
// AvoidableRectangles
// We can't assume that this struct is created only from clues nor that the information about them
// will always be present for the caller
#[allow(unused)]
pub(crate) clues: Option<Sudoku>,
// current state of the sudoku
// for when it's faster to recompute from the end state
Expand Down Expand Up @@ -234,6 +235,7 @@ impl StrategySolver {

/// Try to solve the sudoku using the given `strategies`. Returns a `Result` of the sudoku and a struct containing the series of deductions.
/// If a solution was found, `Ok(..)` is returned, otherwise `Err(..)`.
#[allow(clippy::result_large_err)] // nonsense, Ok and Err are the same size.
pub fn solve(mut self, strategies: &[Strategy]) -> Result<(Sudoku, Deductions), (Sudoku, Deductions)> {
self.try_solve(strategies);
self.update_grid();
Expand Down
7 changes: 5 additions & 2 deletions src/strategy/strategies/xy_wing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ mod test {
} = deductions.get(0).unwrap()
{
assert_eq!(hinge.get(), 1);
assert_eq!(pincers, Cell::new(1 * 9 + 2).as_set() | Cell::new(8 * 9 + 1));
assert_eq!(
pincers,
Cell::from_coords(1, 2).as_set() | Cell::from_coords(8, 1)
);

let conflict = Candidate {
cell: Cell::new(7 * 9 + 2),
cell: Cell::from_coords(7, 2),
digit: Digit::new(4),
};
assert_eq!(conflicts, &[conflict]);
Expand Down
6 changes: 3 additions & 3 deletions src/strategy/strategies/xyz_wing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ mod test {
assert_eq!(
deductions.get(0).unwrap(),
crate::strategy::Deduction::Wing {
hinge: Cell::new(5 * 9 + 8),
hinge: Cell::from_coords(5, 8),
hinge_digits: Digit::new(1).as_set() | Digit::new(2) | Digit::new(4),
pincers: Cell::new(3 * 9 + 8).as_set() | Cell::new(5 * 9 + 0),
pincers: Cell::from_coords(3, 8).as_set() | Cell::from_coords(5, 0),
conflicts: &[Candidate {
cell: Cell::new(5 * 9 + 6),
cell: Cell::from_coords(5, 6),
digit: Digit::new(1),
}][..],
}
Expand Down

0 comments on commit 7901820

Please sign in to comment.