fix some pedantic clippy lints that make sense

- clippy::redundant-closure-for-method-calls
- clippy::explicit-iter-loop
This commit is contained in:
Antoine Martin 2020-12-17 01:23:10 +01:00
parent 449cdc0157
commit e25bc47f8f
20 changed files with 59 additions and 66 deletions

View file

@ -20,7 +20,7 @@ fn fuel_needed(module_weight: u64) -> u64 {
fn part1(input: &str) -> Result<u64> {
input
.lines()
.map(|line| line.parse::<u64>())
.map(str::parse)
.map(|w| w.map(fuel_needed).map_err(anyhow::Error::new))
.sum()
}
@ -40,7 +40,7 @@ fn cumulated_fuel_needed(module_weight: u64) -> u64 {
fn part2(input: &str) -> Result<u64> {
input
.lines()
.map(|line| line.parse::<u64>())
.map(str::parse)
.map(|w| w.map(cumulated_fuel_needed).map_err(anyhow::Error::new))
.sum()
}

View file

@ -33,9 +33,8 @@ fn part1(first_wire: &Wire, second_wire: &Wire) -> Result<u64> {
})
.filter_map(|(f, s)| match f.intersection(s) {
Some(Point { x: 0, y: 0 }) | None => None,
Some(p) => Some(p),
Some(p) => Some(manhattan_distance(&p, &Point { x: 0, y: 0 })),
})
.map(|inter| manhattan_distance(&inter, &Point { x: 0, y: 0 }))
.min()
.context("wire was empty")
}
@ -45,10 +44,10 @@ fn part2(first_wire: &Wire, second_wire: &Wire) -> Result<u64> {
let mut first_length = 0;
for seg1 in first_wire.0.iter() {
for seg1 in &first_wire.0 {
let mut second_length = 0;
for seg2 in second_wire.0.iter() {
for seg2 in &second_wire.0 {
if let Some(inter) = seg1.intersection(&seg2) {
if inter.x == 0 && inter.y == 0 {
continue;

View file

@ -37,7 +37,7 @@ fn count_orbits(
fn part1(input: &str) -> Result<u64> {
let orbits = input
.lines()
.map(|line| line.trim_end())
.map(str::trim_end)
.map(|line| {
let paren = line
.find(')')
@ -56,7 +56,7 @@ fn part1(input: &str) -> Result<u64> {
fn part2(input: &str) -> Result<usize> {
let orbits = input
.lines()
.map(|line| line.trim_end())
.map(str::trim_end)
.map(|line| {
let paren = line
.find(')')

View file

@ -48,7 +48,7 @@ fn part1(input: &str) -> Result<i64> {
let mut res = 0;
for combination in combinations {
let mut output = 0;
for phase in combination.iter() {
for phase in &combination {
let mut intcode = Intcode::with_memory(memory.clone());
intcode.add_input(*phase);
@ -95,7 +95,7 @@ fn part2(input: &str) -> Result<i64> {
intcode.run_and_wait()?;
for out in intcode.output.iter() {
for out in &intcode.output {
next.add_input(*out);
}
intcode.output.clear();
@ -116,7 +116,7 @@ fn part2(input: &str) -> Result<i64> {
None => bail!("last amplifier halted without output"),
};
} else {
for out in last.output.iter() {
for out in &last.output {
first.add_input(*out);
}
last.output.clear();

View file

@ -96,7 +96,7 @@ impl FromStr for Image {
let mut result = vec![vec![2; IMG_WIDTH]; IMG_HEIGHT];
// overlap layers
for layer in layers.iter() {
for layer in &layers {
for (src_row, dst_row) in layer.pixels.iter().zip(result.iter_mut()) {
for (src_pixel, dst_pixel) in src_row.iter().zip(dst_row.iter_mut()) {
if let 2 = *dst_pixel {

View file

@ -48,10 +48,10 @@ fn part1(input: &str) -> Result<usize> {
}
let mut best = None;
for a in asteroids.iter() {
for a in &asteroids {
let mut set = HashSet::new();
for b in asteroids.iter() {
for b in &asteroids {
if a == b {
continue;
}

View file

@ -40,7 +40,7 @@ fn generate_pairs(n: usize) -> Vec<(usize, usize)> {
}
fn parse_planets(input: &str) -> Result<Vec<Planet>> {
input.lines().map(|l| l.parse()).collect()
input.lines().map(str::parse).collect()
}
fn part1(mut planets: Vec<Planet>, steps: usize) -> Result<u64> {
@ -91,12 +91,12 @@ fn part1(mut planets: Vec<Planet>, steps: usize) -> Result<u64> {
}
// update position
for planet in planets.iter_mut() {
for planet in &mut planets {
planet.update_pos();
}
}
Ok(planets.iter().map(|p| p.total_energy()).sum())
Ok(planets.iter().map(Planet::total_energy).sum())
}
fn gcd(a: usize, b: usize) -> usize {
@ -165,7 +165,7 @@ fn part2(mut planets: Vec<Planet>) -> Result<usize> {
}
// update position
for planet in planets.iter_mut() {
for planet in &mut planets {
planet.update_pos();
}

View file

@ -79,7 +79,7 @@ fn get_ore_cost(
let needed = quantity - in_stock;
let num_reactions = (needed + recipe.produced - 1) / recipe.produced;
for elem in recipe.elems.iter() {
for elem in &recipe.elems {
total += get_ore_cost(
elem.name.clone(),
elem.amount * num_reactions,

View file

@ -203,10 +203,10 @@ impl Intcode {
bail!("dst must be a valid address: {}", dst);
}
if val != 0 {
self.ip = dst as usize;
} else {
if val == 0 {
self.ip += 3;
} else {
self.ip = dst as usize;
}
}
Opcode::JumpFalse(test, dst) => {