From f23624c456e3a9e6922970126ced3ed7ca7c4147 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 17 Dec 2018 12:26:31 +0100 Subject: [PATCH] Fix all clippy warnings. --- 2018/src/common.rs | 4 ++-- 2018/src/day01.rs | 3 ++- 2018/src/day02.rs | 6 +++--- 2018/src/day03.rs | 2 +- 2018/src/day04.rs | 8 ++++---- 2018/src/day05.rs | 4 ++-- 2018/src/day06.rs | 11 ++++++----- 2018/src/day07.rs | 2 +- 2018/src/day08.rs | 4 ++-- 2018/src/day09.rs | 4 ++-- 2018/src/day11.rs | 6 ++++++ 2018/src/day12.rs | 8 ++++---- 2018/src/day13.rs | 8 ++++---- 2018/src/day14.rs | 2 +- 2018/src/day16.rs | 18 ++++++++++++------ 15 files changed, 52 insertions(+), 38 deletions(-) diff --git a/2018/src/common.rs b/2018/src/common.rs index e771c36..38be760 100644 --- a/2018/src/common.rs +++ b/2018/src/common.rs @@ -15,7 +15,7 @@ use std::fmt::Debug; /// assumed to be filled with "true" before being handed to this /// method. pub fn prime_sieve(dest: &mut [bool]) { - if dest.len() >= 1 { + if !dest.is_empty() { dest[0] = false; } @@ -25,7 +25,7 @@ pub fn prime_sieve(dest: &mut [bool]) { let limit = (dest.len() as f64).sqrt() as usize; - for i in 1..(limit + 1) { + for i in 1..=limit { if !dest[i] { continue; } diff --git a/2018/src/day01.rs b/2018/src/day01.rs index 20fc351..2950191 100644 --- a/2018/src/day01.rs +++ b/2018/src/day01.rs @@ -4,11 +4,12 @@ use std::io::prelude::*; use common; +#[derive(Default)] pub struct Day01 {} impl Day01 { pub fn new() -> Day01 { - Day01 {} + Default::default() } } diff --git a/2018/src/day02.rs b/2018/src/day02.rs index 1cea6a5..8dcce37 100644 --- a/2018/src/day02.rs +++ b/2018/src/day02.rs @@ -88,9 +88,9 @@ mod tests { fn test_count_letters() { let result = count_chars("abcaba"); - assert_eq!(3, *result.get(&'a').unwrap()); - assert_eq!(2, *result.get(&'b').unwrap()); - assert_eq!(1, *result.get(&'c').unwrap()) + assert_eq!(3, result[&'a']); + assert_eq!(2, result[&'b']); + assert_eq!(1, result[&'c']); } #[test] diff --git a/2018/src/day03.rs b/2018/src/day03.rs index 35f68b8..7b36a00 100644 --- a/2018/src/day03.rs +++ b/2018/src/day03.rs @@ -86,7 +86,7 @@ impl common::Solution for Day03 { let claims = self.get_claims(); let uncontested = self.claims.iter() - .position(|x| x.range().all(|x| *claims.get(&x).unwrap() == 1)) + .position(|x| x.range().all(|x| claims[&x] == 1)) .unwrap(); format!("{}", uncontested + 1) diff --git a/2018/src/day04.rs b/2018/src/day04.rs index 7316070..8aa568b 100644 --- a/2018/src/day04.rs +++ b/2018/src/day04.rs @@ -72,7 +72,7 @@ impl Day04 { sleep_start = None; } EventType::SLEEP => { - sleep_start = Some(event.time.clone()); + sleep_start = Some(event.time); } EventType::WAKE => { let mut minutes = sleeps.entry(guard.unwrap()).or_insert([0u32; 60]); @@ -86,7 +86,7 @@ impl Day04 { sleeps } - fn format_results(sleepers: HashMap, scores: HashMap) -> String { + fn format_results(sleepers: &HashMap, scores: &HashMap) -> String { let (best_sleeper, _) = scores.iter().max_by(|&(_, a), &(_, b)| a.cmp(b)).unwrap(); let best_minute = sleepers[best_sleeper].iter().enumerate() @@ -102,7 +102,7 @@ impl common::Solution for Day04 { let sleepers = self.get_sleeps(); let scores: HashMap = sleepers.iter().map(|(k, v)| (*k, v.iter().sum())).collect(); - Day04::format_results(sleepers, scores) + Day04::format_results(&sleepers, &scores) } fn part2(&mut self, input: &mut io::Read) -> String { @@ -110,7 +110,7 @@ impl common::Solution for Day04 { let sleepers = self.get_sleeps(); let scores: HashMap = sleepers.iter().map(|(k, v)| (*k, *v.iter().max().unwrap())).collect(); - Day04::format_results(sleepers, scores) + Day04::format_results(&sleepers, &scores) } } diff --git a/2018/src/day05.rs b/2018/src/day05.rs index 9f14516..1a4bb8c 100644 --- a/2018/src/day05.rs +++ b/2018/src/day05.rs @@ -42,8 +42,8 @@ impl common::Solution for Day05 { input.read_to_end(&mut data).expect("Can't read input!"); common::trim_back(&mut data); - let min_len = (b'a'..=b'z').map(|option| data.iter().filter(|&x| !x.eq_ignore_ascii_case(&option)).map(|&x| x).collect()) - .map(|x| Day05::reduce(x)) + let min_len = (b'a'..=b'z').map(|option| data.iter().filter(|&x| !x.eq_ignore_ascii_case(&option)).cloned().collect()) + .map(Day05::reduce) .min().unwrap(); format!("{}", min_len) diff --git a/2018/src/day06.rs b/2018/src/day06.rs index b3d5e7a..d288822 100644 --- a/2018/src/day06.rs +++ b/2018/src/day06.rs @@ -83,7 +83,7 @@ impl Day06 { self.range() .map(|x| self.points.iter().map(|y| manhattan_distance(x, *y)).sum::()) - .filter(|x| x < &limit) + .filter(|&x| x < limit) .count() } } @@ -100,10 +100,11 @@ impl Solution for Day06 { self.read_points(input); let grid = self.compute_claim_grid(); let mut infinite: HashSet = HashSet::new(); - infinite.extend(grid[0].iter().filter_map(claim_filter)); - infinite.extend(grid[self.ymax].iter().filter_map(claim_filter)); - for y in 0..=self.ymax { - infinite.extend([grid[y][0], grid[y][self.xmax]].iter().filter_map(claim_filter)); + infinite.extend(grid.first().unwrap().iter().filter_map(claim_filter)); + infinite.extend(grid.last().unwrap().iter().filter_map(claim_filter)); + for row in grid.iter().take(self.ymax) { + infinite.extend([row.first().unwrap(), row.last().unwrap()].iter() + .cloned().filter_map(claim_filter)); } let counts = grid.iter().flat_map(|x| x.iter()) diff --git a/2018/src/day07.rs b/2018/src/day07.rs index 6d46779..313c6f5 100644 --- a/2018/src/day07.rs +++ b/2018/src/day07.rs @@ -50,7 +50,7 @@ impl Day07 { let a = groups[1].chars().next().unwrap(); let b = groups[2].chars().next().unwrap(); - self.forward.entry(a).or_insert(Vec::new()).push(b); + self.forward.entry(a).or_insert_with(Vec::new).push(b); *self.dep_count.entry(b).or_insert(0) += 1; } } diff --git a/2018/src/day08.rs b/2018/src/day08.rs index 9df06e9..7e70f52 100644 --- a/2018/src/day08.rs +++ b/2018/src/day08.rs @@ -61,7 +61,7 @@ impl Solution for Day08 { fn part1(&mut self, input: &mut Read) -> String { let data: String = read_single_input(input); - let data: Vec = data.trim().split(" ").map(|x| x.parse().unwrap()).collect(); + let data: Vec = data.trim().split(' ').map(|x| x.parse().unwrap()).collect(); let (result, _) = total1(&data); format!("{}", result) @@ -70,7 +70,7 @@ impl Solution for Day08 { fn part2(&mut self, input: &mut Read) -> String { let data: String = read_single_input(input); - let data: Vec = data.trim().split(" ").map(|x| x.parse().unwrap()).collect(); + let data: Vec = data.trim().split(' ').map(|x| x.parse().unwrap()).collect(); let (result, _) = total2(&data); format!("{}", result) diff --git a/2018/src/day09.rs b/2018/src/day09.rs index fa7d649..976af92 100644 --- a/2018/src/day09.rs +++ b/2018/src/day09.rs @@ -43,9 +43,9 @@ impl Day09 { fn read_input(input: &mut Read) -> (usize, usize) { let mut data = String::new(); input.read_to_string(&mut data).unwrap(); - let mut parts = data.split(" "); + let mut parts = data.split(' '); let elves = parts.next().unwrap().parse().unwrap(); - let marbles = parts.skip(5).next().unwrap().parse().unwrap(); + let marbles = parts.nth(5).unwrap().parse().unwrap(); (elves, marbles) } diff --git a/2018/src/day11.rs b/2018/src/day11.rs index 29dc86d..e71976d 100644 --- a/2018/src/day11.rs +++ b/2018/src/day11.rs @@ -86,6 +86,12 @@ impl Day11 { } } +impl Default for Day11 { + fn default() -> Self { + Self::new() + } +} + impl Solution for Day11 { fn part1(&mut self, input: &mut Read) -> String { let serial = read_single_input(input); diff --git a/2018/src/day12.rs b/2018/src/day12.rs index 53f25c5..c30d0de 100644 --- a/2018/src/day12.rs +++ b/2018/src/day12.rs @@ -20,7 +20,7 @@ fn char_bool(c: char) -> bool { } } -fn print_state(state: &State) -> String { +fn print_state(state: &[(i64, bool)]) -> String { state.iter().map(|(_, x)| if *x { '#' } else { '.' }).collect() } @@ -60,7 +60,7 @@ impl Day12 { state } - fn simulate(&self, state: &State) -> State { + fn simulate(&self, state: &[(i64, bool)]) -> State { let mut new_state = Vec::with_capacity(state.len() + 8); let mut index = 0; for &(idx, b) in state { @@ -97,7 +97,7 @@ impl Day12 { state } - fn sum(&self, state: &State) -> i64 { + fn sum(&self, state: &[(i64, bool)]) -> i64 { state.iter() .filter(|&&(_, x)| x) .map(|&(i, _)| i) @@ -118,7 +118,7 @@ impl Solution for Day12 { let mut state = self.read_input(input); let mut seen = HashMap::new(); let mut time = 1i64; - const TARGET_TIME: i64 = 50000000000; + const TARGET_TIME: i64 = 50_000_000_000; loop { state = self.simulate(&state); diff --git a/2018/src/day13.rs b/2018/src/day13.rs index a235d97..c05704f 100644 --- a/2018/src/day13.rs +++ b/2018/src/day13.rs @@ -25,7 +25,7 @@ impl Direction { _ => panic!("Invalid direction {}", c), } } - pub fn clockwise(&self) -> Self { + pub fn clockwise(self) -> Self { match self { Direction::North => Direction::East, Direction::East => Direction::South, @@ -34,11 +34,11 @@ impl Direction { } } - pub fn counter_clockwise(&self) -> Self { - return self.clockwise().clockwise().clockwise(); + pub fn counter_clockwise(self) -> Self { + self.clockwise().clockwise().clockwise() } - pub fn run(&self, pos: Coordinate) -> Coordinate { + pub fn run(self, pos: Coordinate) -> Coordinate { let (x, y) = pos; match self { Direction::North => (x, y - 1), diff --git a/2018/src/day14.rs b/2018/src/day14.rs index 5a310bd..02af927 100644 --- a/2018/src/day14.rs +++ b/2018/src/day14.rs @@ -46,7 +46,7 @@ fn skill_after(n: usize) -> u64 { let mut skill = 0; for d in state.skip(n).take(10) { skill *= 10; - skill += d as u64; + skill += u64::from(d); } skill diff --git a/2018/src/day16.rs b/2018/src/day16.rs index ee5f5f5..def40ca 100644 --- a/2018/src/day16.rs +++ b/2018/src/day16.rs @@ -28,7 +28,7 @@ enum OpCode { } impl OpCode { - fn valid(&self, op: &[i32; 4], before: &[i32; 4], after: &[i32; 4]) -> bool { + fn valid(self, op: &[i32; 4], before: &[i32; 4], after: &[i32; 4]) -> bool { let mut cpu: CPU = Default::default(); cpu.registers.copy_from_slice(before); @@ -38,7 +38,7 @@ impl OpCode { } } - return false; + false } } @@ -72,7 +72,7 @@ struct CPU { } impl CPU { - pub fn execute(&mut self, op: &OpCode, var: &[i32]) -> Result { + pub fn execute(&mut self, op: OpCode, var: &[i32]) -> Result { use self::OpCode::*; let res = match op { ADDR => self.reg(var[0])? + self.reg(var[1])?, @@ -121,7 +121,7 @@ impl Day16 { fn read(&mut self, reader: &mut BufRead, target: &mut [i32; 4]) -> bool { self.buf.clear(); - if let Err(_) = reader.read_line(&mut self.buf) { + if reader.read_line(&mut self.buf).is_err() { return false; } @@ -154,7 +154,7 @@ impl Day16 { if mappings[op[0] as usize].is_empty() { mappings[op[0] as usize].extend(OP_LIST.iter() .filter(|x| x.valid(&op, &before, &after)) - .map(|x| *x)); + .cloned()); } else { for option in OP_LIST.iter() .filter(|x| !x.valid(&op, &before, &after)) { @@ -197,6 +197,12 @@ impl Day16 { } } +impl Default for Day16 { + fn default() -> Self { + Self::new() + } +} + impl Solution for Day16 { fn part1(&mut self, input: &mut Read) -> String { let mut reader = BufReader::new(input); @@ -235,7 +241,7 @@ impl Solution for Day16 { let mut cpu: CPU = Default::default(); while self.read(&mut reader, &mut op) { - cpu.execute(&mapping[op[0] as usize], &op[1..4]).unwrap(); + cpu.execute(mapping[op[0] as usize], &op[1..4]).unwrap(); } format!("{}", cpu.registers[0])