From fb0bb469ee5bc5095bb33f9fe07b2b1c1edbfcf8 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Thu, 5 Dec 2019 16:35:39 +0100 Subject: [PATCH] fix clippy lints --- aoc2015/src/day04.rs | 8 +++---- aoc2019/src/day04.rs | 2 +- aoc2019/src/day05.rs | 54 +++++++++++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/aoc2015/src/day04.rs b/aoc2015/src/day04.rs index 77d8c52..ce7fae3 100644 --- a/aoc2015/src/day04.rs +++ b/aoc2015/src/day04.rs @@ -20,11 +20,11 @@ fn part1(input: &str) -> Result { for i in 0.. { content.truncate(input.len()); - content.extend(i.to_string().chars()); + content.push_str(&i.to_string()); hasher.input(&content); let res = hasher.result_reset(); - if &res[..2] == &[0, 0] && res[2] <= 0x0f { + if res[..2] == [0, 0] && res[2] <= 0x0f { return Ok(i); } } @@ -40,11 +40,11 @@ fn part2(input: &str) -> Result { for i in 0.. { content.truncate(input.len()); - content.extend(i.to_string().chars()); + content.push_str(&i.to_string()); hasher.input(&content); let res = hasher.result_reset(); - if &res[..3] == &[0, 0, 0] { + if res[..3] == [0, 0, 0] { return Ok(i); } } diff --git a/aoc2019/src/day04.rs b/aoc2019/src/day04.rs index 7a847ee..85f49b9 100644 --- a/aoc2019/src/day04.rs +++ b/aoc2019/src/day04.rs @@ -96,7 +96,7 @@ impl<'a> Iterator for GroupIter<'a> { type Item = &'a [usize]; fn next(&mut self) -> Option { - if self.digits.len() == 0 { + if self.digits.is_empty() { return None; } diff --git a/aoc2019/src/day05.rs b/aoc2019/src/day05.rs index b91f5e3..651bd4d 100644 --- a/aoc2019/src/day05.rs +++ b/aoc2019/src/day05.rs @@ -34,8 +34,8 @@ enum Parameter { } impl Parameter { - fn new(mode: i64, val: Option<&i64>) -> Result { - let val = *val.ok_or_else(|| err!("parameter value out of bounds"))?; + fn new(mode: i64, val: Option) -> Result { + let val = val.ok_or_else(|| err!("parameter value out of bounds"))?; let mode = mode % 10; match mode { @@ -54,7 +54,7 @@ impl Parameter { fn get(&self, memory: &[i64]) -> Option { match self { - Parameter::Position(address) => memory.get(*address).map(|v| *v), + Parameter::Position(address) => memory.get(*address).copied(), Parameter::Immediate(value) => Some(*value), } } @@ -118,9 +118,11 @@ impl Intcode { let opcode = instruction % 100; match opcode { 1 => { - let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; - let op2 = Parameter::new(instruction / 1000, self.memory.get(self.ip + 2))?; - let dst = Parameter::new(instruction / 10000, self.memory.get(self.ip + 3))?; + let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; + let op2 = + Parameter::new(instruction / 1000, self.memory.get(self.ip + 2).copied())?; + let dst = + Parameter::new(instruction / 10000, self.memory.get(self.ip + 3).copied())?; if let Parameter::Immediate(_) = dst { Err(err!("add: destination parameter can't be immediate")) @@ -129,9 +131,11 @@ impl Intcode { } } 2 => { - let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; - let op2 = Parameter::new(instruction / 1000, self.memory.get(self.ip + 2))?; - let dst = Parameter::new(instruction / 10000, self.memory.get(self.ip + 3))?; + let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; + let op2 = + Parameter::new(instruction / 1000, self.memory.get(self.ip + 2).copied())?; + let dst = + Parameter::new(instruction / 10000, self.memory.get(self.ip + 3).copied())?; if let Parameter::Immediate(_) = dst { Err(err!("multiply: destination parameter can't be immediate")) @@ -140,7 +144,7 @@ impl Intcode { } } 3 => { - let dst = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; + let dst = Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; if let Parameter::Immediate(_) = dst { Err(err!("input: destination parameter can't be immediate")) @@ -149,26 +153,32 @@ impl Intcode { } } 4 => { - let op = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; + let op = Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; Ok(Opcode::Output(op)) } 5 => { - let test = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; - let dst = Parameter::new(instruction / 1000, self.memory.get(self.ip + 2))?; + let test = + Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; + let dst = + Parameter::new(instruction / 1000, self.memory.get(self.ip + 2).copied())?; Ok(Opcode::JumpTrue(test, dst)) } 6 => { - let test = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; - let dst = Parameter::new(instruction / 1000, self.memory.get(self.ip + 2))?; + let test = + Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; + let dst = + Parameter::new(instruction / 1000, self.memory.get(self.ip + 2).copied())?; Ok(Opcode::JumpFalse(test, dst)) } 7 => { - let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; - let op2 = Parameter::new(instruction / 1000, self.memory.get(self.ip + 2))?; - let dst = Parameter::new(instruction / 10000, self.memory.get(self.ip + 3))?; + let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; + let op2 = + Parameter::new(instruction / 1000, self.memory.get(self.ip + 2).copied())?; + let dst = + Parameter::new(instruction / 10000, self.memory.get(self.ip + 3).copied())?; if let Parameter::Immediate(_) = dst { Err(err!("less than: destination parameter can't be immediate")) @@ -177,9 +187,11 @@ impl Intcode { } } 8 => { - let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1))?; - let op2 = Parameter::new(instruction / 1000, self.memory.get(self.ip + 2))?; - let dst = Parameter::new(instruction / 10000, self.memory.get(self.ip + 3))?; + let op1 = Parameter::new(instruction / 100, self.memory.get(self.ip + 1).copied())?; + let op2 = + Parameter::new(instruction / 1000, self.memory.get(self.ip + 2).copied())?; + let dst = + Parameter::new(instruction / 10000, self.memory.get(self.ip + 3).copied())?; if let Parameter::Immediate(_) = dst { Err(err!("equals: destination parameter can't be immediate"))