Skip to content

Commit 310da51

Browse files
committed
fix all clippy warnings
1 parent c7af681 commit 310da51

4 files changed

Lines changed: 27 additions & 37 deletions

File tree

src/game.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use halfbrown::HashMap;
22
use smallvec::{smallvec, SmallVec};
33
use std::collections::BinaryHeap;
44
use std::fmt;
5+
use std::fmt::Write;
56

67
use crate::{score::Score, score_cache::ScoreCache};
78

8-
const NON_WINNING_SCORES_COUNT: usize = (3 as usize).pow(5) - 1;
9+
const NON_WINNING_SCORES_COUNT: usize = 3_usize.pow(5) - 1;
910
const SMALLVEC_MAX: usize = 4;
1011

1112
type WordIndex = u16;
@@ -50,18 +51,18 @@ struct Guess<'a> {
5051

5152
impl<'a> Ord for Guess<'a> {
5253
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
53-
return other.avg_score.partial_cmp(&self.avg_score).unwrap();
54+
other.avg_score.partial_cmp(&self.avg_score).unwrap()
5455
}
5556
}
5657
impl<'a> PartialOrd for Guess<'a> {
5758
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
58-
return Some(self.cmp(other));
59+
Some(self.cmp(other))
5960
}
6061
}
6162
impl<'a> Eq for Guess<'a> {}
6263
impl<'a> PartialEq for Guess<'a> {
6364
fn eq(&self, other: &Self) -> bool {
64-
return self.guess == other.guess;
65+
self.guess == other.guess
6566
}
6667
}
6768

@@ -121,37 +122,33 @@ impl<'a> Game<'a> {
121122
}
122123
}
123124

124-
pub fn print_current_best_score(&self, words: &'a Vec<String>) -> String {
125+
pub fn print_current_best_score(&self, words: &'a [String]) -> String {
125126
if let Game::Large(g) = self {
126127
if let OptimizationStatus::InProgress(ref heap) = g.optimization {
127128
let mut result = String::new();
128129
for c in heap.iter().take(3) {
129-
result.push_str(&format!("{}-{} ", words[c.guess as usize], c.avg_score));
130+
write!(result, "{}-{} ", words[c.guess as usize], c.avg_score).unwrap();
130131
}
131132
return result;
132133
}
133134
}
134135
String::from("optimization is over")
135136
}
136137

137-
pub fn print_tree(&self, words: &'a Vec<String>) -> String {
138+
pub fn print_tree(&self, words: &'a [String]) -> String {
138139
match self {
139140
Game::Single(w) => format!("{} !", words[*w as usize]),
140141
Game::Double(w1, w2) => format!("{} or {} !", words[*w1 as usize], words[*w2 as usize]),
141142
Game::Large(g) => {
142143
if let OptimizationStatus::Done(ref guess) = g.optimization {
143144
let word = &words[guess.guess as usize];
144-
if guess.subgames.len() == 0 {
145-
word.clone() + "!"
146-
} else {
147-
let mut lines: Vec<String> = vec![];
148-
for (score, sub) in guess.subgames.iter() {
149-
for l in sub.print_tree(words).lines() {
150-
lines.push(format!("{} {} {}", word, score, l));
151-
}
145+
let mut result = String::new();
146+
for (score, sub) in guess.subgames.iter() {
147+
for l in sub.print_tree(words).lines() {
148+
writeln!(result, "{} {} {}", word, score, l).unwrap();
152149
}
153-
lines.join("\n")
154150
}
151+
result
155152
} else {
156153
panic!("optimization not done")
157154
}
@@ -164,11 +161,7 @@ impl<'a> Game<'a> {
164161
Game::Single(_) => true,
165162
Game::Double(_, _) => true,
166163
Game::Large(g) => {
167-
if let OptimizationStatus::Done(_) = g.optimization {
168-
true
169-
} else {
170-
false
171-
}
164+
matches!(g.optimization, OptimizationStatus::Done(_))
172165
}
173166
}
174167
}

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use word::Word;
1414

1515
use crate::score_cache::ScoreCache;
1616

17-
fn write_all_words(words: &Vec<Word>) {
17+
fn write_all_words(words: &[Word]) {
1818
let filename = "all_words.csv";
1919
let mut file = File::create(filename).unwrap();
2020
for (i, w) in words.iter().enumerate() {
21-
file.write(format!("{}\t \"{}\"\n", i, w).as_bytes())
21+
file.write_all(format!("{}\t \"{}\"\n", i, w).as_bytes())
2222
.unwrap();
2323
}
2424
println!("{} written !", filename);
@@ -30,7 +30,7 @@ fn write_all_scores() {
3030
for code in 0..3_usize.pow(5) {
3131
let score = Score { code: code as u8 };
3232
scores_file
33-
.write(format!("{}\t{}\n", code, score).as_bytes())
33+
.write_all(format!("{}\t{}\n", code, score).as_bytes())
3434
.unwrap();
3535
}
3636
println!("{} written !", filename);
@@ -66,7 +66,7 @@ fn main() {
6666
let score_cache = ScoreCache::from_words(&words);
6767
println!("all scores computed");
6868

69-
let words_str = words.iter().map(|w| w.to_string()).collect();
69+
let words_str: Vec<String> = words.iter().map(|w| w.to_string()).collect();
7070
let mut game = Game::new(&score_cache, (0..n as u16).collect());
7171
while !game.is_optimization_done() {
7272
game.refine_score();

src/score.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
11
use crate::Color;
22
use std::fmt;
33

4-
#[derive(Debug, Hash, Copy, Clone)]
4+
#[derive(Debug, Hash, Copy, Clone, Eq, PartialEq)]
55
pub struct Score {
66
pub code: u8,
77
}
88
impl fmt::Display for Score {
99
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10-
let colors = self.to_colors();
10+
let colors = self.as_colors();
1111
write!(
1212
f,
1313
"{}{}{}{}{}",
1414
colors[0], colors[1], colors[2], colors[3], colors[4]
1515
)
1616
}
1717
}
18-
impl Eq for Score {}
19-
impl PartialEq for Score {
20-
fn eq(&self, other: &Self) -> bool {
21-
return self.code == other.code;
22-
}
23-
}
2418
impl Score {
25-
pub fn to_colors(&self) -> [Color; 5] {
19+
pub fn as_colors(&self) -> [Color; 5] {
2620
let mut nn = self.code;
2721
let mut result = [Color::Black; 5];
22+
#[allow(clippy::needless_range_loop)]
2823
for i in 0..5 {
2924
result[i] = match nn % 3 {
3025
0 => Color::Black,
3126
1 => Color::Yellow,
3227
2 => Color::Green,
3328
_ => panic!("imposible modulo"),
3429
};
35-
nn = nn / 3;
30+
nn /= 3;
3631
}
3732
result
3833
}
3934
pub fn from_colors(colors: [Color; 5]) -> Self {
4035
let mut result: u8 = 0;
36+
#[allow(clippy::needless_range_loop)]
4137
for i in 0..5 {
4238
let score = match colors[i] {
4339
Color::Black => 0,
@@ -63,6 +59,6 @@ mod tests {
6359
Color::Yellow,
6460
Color::Black,
6561
];
66-
assert_eq!(colors, Score::from_colors(colors).to_colors());
62+
assert_eq!(colors, Score::from_colors(colors).as_colors());
6763
}
6864
}

src/word.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ impl Word {
1919
}
2020
}
2121
// assign yellows
22+
#[allow(clippy::needless_range_loop)]
2223
for i in 0..5 {
2324
if result[i] == Color::Green {
2425
continue;
@@ -39,7 +40,7 @@ impl Word {
3940
}
4041
pub fn load_words() -> Vec<Word> {
4142
let words_iter = include_str!("solutions.txt").lines();
42-
return words_iter.map(Word::from_str).collect();
43+
words_iter.map(Word::from_str).collect()
4344
}
4445
}
4546

0 commit comments

Comments
 (0)