Skip to content

Commit 6cbfe1d

Browse files
committed
clippy pedantic
1 parent 66207e7 commit 6cbfe1d

File tree

63 files changed

+382
-315
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+382
-315
lines changed

src/analytic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn polygon_area<T: Copy + num::Integer + std::iter::Sum>(polygon: &[(T, T)])
99
}
1010

1111
// Pick's formula - Number of interior vertices of a polygon given its area and boundary points
12+
#[must_use]
1213
pub fn polygon_inner_vertices(area: usize, num_boundary_points: usize) -> usize {
1314
area + 1 - num_boundary_points / 2
1415
}

src/bin/2015_01/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use aoc::common;
22

33
fn part1(input: &str) -> i32 {
4-
input.chars().filter(|&c| c == '(').count() as i32
5-
- input.chars().filter(|&c| c == ')').count() as i32
4+
i32::try_from(input.chars().filter(|&c| c == '(').count()).unwrap()
5+
- i32::try_from(input.chars().filter(|&c| c == ')').count()).unwrap()
66
}
77

88
fn part2(input: &str) -> usize {

src/bin/2015_02/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn part1(input: &str) -> i32 {
88
.map(|s| s.parse::<i32>().unwrap())
99
.collect::<Vec<_>>();
1010
let mut areas = [dims[0] * dims[1], dims[1] * dims[2], dims[2] * dims[0]];
11-
areas.sort();
11+
areas.sort_unstable();
1212
let wrapping_paper_area = areas[0] + 2 * (areas[0] + areas[1] + areas[2]);
1313
total_wrapping_paper_area += wrapping_paper_area;
1414
}
@@ -22,7 +22,7 @@ fn part2(input: &str) -> i32 {
2222
.split('x')
2323
.map(|s| s.parse::<i32>().unwrap())
2424
.collect::<Vec<_>>();
25-
dims.sort();
25+
dims.sort_unstable();
2626
let ribbon_length = 2 * (dims[0] + dims[1]) + dims[0] * dims[1] * dims[2];
2727
total_ribbon_length += ribbon_length;
2828
}

src/bin/2020_20/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ enum Order {
1313
Reverse,
1414
}
1515

16-
fn border(g: &Grid<char>, dir: &CardinalDirection) -> Vec<char> {
17-
match *dir {
16+
fn border(g: &Grid<char>, dir: CardinalDirection) -> Vec<char> {
17+
match dir {
1818
CardinalDirection::North => g.values[0].clone(),
1919
CardinalDirection::South => g.values[g.rows - 1].clone(),
2020
CardinalDirection::West => (0..g.rows).map(|r| g.values[r][0]).collect_vec(),
@@ -26,8 +26,8 @@ fn matching_borders(
2626
g1: (&Grid<char>, &CardinalDirection),
2727
g2: (&Grid<char>, &CardinalDirection),
2828
) -> Option<Order> {
29-
let val1 = border(g1.0, g1.1);
30-
let val2 = border(g2.0, g2.1);
29+
let val1 = border(g1.0, *g1.1);
30+
let val2 = border(g2.0, *g2.1);
3131

3232
if val1 == val2 {
3333
return Some(Order::Straight);
@@ -43,7 +43,7 @@ fn matching_borders(
4343
fn part1(input: &str) -> usize {
4444
let grid_strs = io::line_batches(input);
4545
let n = grid_strs.len();
46-
println!("Cells {}", n);
46+
println!("Cells {n}");
4747
let mut grid_with_ids: Vec<(Grid<char>, usize)> = Vec::new();
4848

4949
for g in grid_strs {
@@ -86,9 +86,9 @@ fn part1(input: &str) -> usize {
8686
.filter(|(_, v)| v.len() == 2)
8787
.map(|(k, _)| *k)
8888
.product::<usize>();
89-
for (key, val) in matches.iter() {
89+
for (key, val) in &matches {
9090
if val.len() == 2 {
91-
println!("{:?} => {:?}", key, val);
91+
println!("{key:?} => {val:?}");
9292
}
9393
}
9494

src/bin/2022_04/main.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ fn part1(input_lines: &str) -> usize {
55

66
for line in input_lines.split('\n') {
77
let ranges: Vec<_> = line.split(',').collect();
8-
let idx1: Vec<_> = ranges[0]
9-
.split('-')
10-
.flat_map(|s| s.parse::<usize>())
11-
.collect();
12-
let idx2: Vec<_> = ranges[1]
13-
.split('-')
14-
.flat_map(|s| s.parse::<usize>())
15-
.collect();
8+
let idx1: Vec<_> = ranges[0].split('-').flat_map(str::parse::<usize>).collect();
9+
let idx2: Vec<_> = ranges[1].split('-').flat_map(str::parse::<usize>).collect();
1610

1711
if (idx2[0] >= idx1[0] && idx2[1] <= idx1[1]) || (idx1[0] >= idx2[0] && idx1[1] <= idx2[1])
1812
{
@@ -28,14 +22,8 @@ fn part2(input_lines: &str) -> usize {
2822

2923
for line in input_lines.split('\n') {
3024
let ranges: Vec<_> = line.split(',').collect();
31-
let idx1: Vec<_> = ranges[0]
32-
.split('-')
33-
.flat_map(|s| s.parse::<usize>())
34-
.collect();
35-
let idx2: Vec<_> = ranges[1]
36-
.split('-')
37-
.flat_map(|s| s.parse::<usize>())
38-
.collect();
25+
let idx1: Vec<_> = ranges[0].split('-').flat_map(str::parse::<usize>).collect();
26+
let idx2: Vec<_> = ranges[1].split('-').flat_map(str::parse::<usize>).collect();
3927

4028
let idx_l = idx1[0].max(idx2[0]);
4129
let idx_r = idx1[1].min(idx2[1]);

src/bin/2022_05/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn part1(stacks: &[Vec<char>], instructions: &Vec<Vec<usize>>) -> String {
1010
}
1111
}
1212

13-
stacks.iter().flat_map(|v| v.last()).collect::<String>()
13+
stacks.iter().filter_map(|v| v.last()).collect::<String>()
1414
}
1515

1616
fn part2(stacks: &[Vec<char>], instructions: &Vec<Vec<usize>>) -> String {
@@ -24,7 +24,7 @@ fn part2(stacks: &[Vec<char>], instructions: &Vec<Vec<usize>>) -> String {
2424
stacks[ins[1] - 1].truncate(l - ins[0]);
2525
}
2626

27-
stacks.iter().flat_map(|v| v.last()).collect::<String>()
27+
stacks.iter().filter_map(|v| v.last()).collect::<String>()
2828
}
2929

3030
fn process_and_solve<const PART1: bool>(input: &str) -> String {
@@ -61,7 +61,7 @@ fn process_and_solve<const PART1: bool>(input: &str) -> String {
6161
} else {
6262
instructions.push(
6363
line.split(' ')
64-
.flat_map(|s| s.parse::<usize>())
64+
.flat_map(str::parse::<usize>)
6565
.collect::<Vec<usize>>(),
6666
);
6767
}

src/bin/2022_07/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ use aoc::common;
22
use std::collections::HashMap;
33

44
fn process_and_solve<const PART1: bool>(input_lines: &str) -> usize {
5+
const DIR_SEP: &str = "/";
56
let mut curr_dir_path: Vec<String> = Vec::new();
67
let mut curr_total_filesizes: usize = 0;
78
let mut dir_size_map: HashMap<String, usize> = HashMap::new();
89
let mut reading = false;
910

10-
const DIR_SEP: &str = "/";
11-
1211
for line in input_lines.split('\n') {
1312
let tokens = line
1413
.split(' ')
15-
.map(|s| s.to_string())
14+
.map(std::string::ToString::to_string)
1615
.collect::<Vec<String>>();
1716

1817
if reading && tokens[0] != "$" && tokens[0] != "dir" {
@@ -64,7 +63,7 @@ fn process_and_solve<const PART1: bool>(input_lines: &str) -> usize {
6463
fn part1(dir_size_map: &HashMap<String, usize>) -> usize {
6564
let mut sum = 0;
6665
for v in dir_size_map.values() {
67-
if *v <= 100000 {
66+
if *v <= 100_000 {
6867
sum += *v;
6968
}
7069
}
@@ -73,8 +72,8 @@ fn part1(dir_size_map: &HashMap<String, usize>) -> usize {
7372
}
7473

7574
fn part2(dir_size_map: &HashMap<String, usize>) -> usize {
76-
const TOTAL_DISK_SPACE: usize = 70000000;
77-
const UNUSED_SPACE_REQUIRED: usize = 30000000;
75+
const TOTAL_DISK_SPACE: usize = 70_000_000;
76+
const UNUSED_SPACE_REQUIRED: usize = 30_000_000;
7877

7978
let unused_space = TOTAL_DISK_SPACE - dir_size_map.get("/").unwrap();
8079
let minimimum_space_to_free = UNUSED_SPACE_REQUIRED - unused_space;

src/bin/2022_08/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use aoc::{common, grid::Grid};
22
use itertools::iproduct;
33

4+
#[allow(clippy::cast_possible_wrap)]
45
fn part1(grid: &Grid<u32>) -> usize {
56
// Check visibility for each internal tree
67
let mut visible = Grid::<bool>::new(grid.rows, grid.cols, false);
@@ -84,7 +85,7 @@ fn part2(grid: &Grid<u32>) -> i32 {
8485
fn get_grid_and_solve<const PART1: bool>(input: &str) -> i32 {
8586
let grid = Grid::from_str(input, |c| c.to_digit(10).unwrap());
8687
if PART1 {
87-
return part1(&grid) as i32;
88+
return i32::try_from(part1(&grid)).unwrap();
8889
}
8990
part2(&grid)
9091
}

src/bin/2022_09/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use aoc::common;
22
use itertools::Itertools;
33

4+
#[allow(clippy::similar_names)]
45
fn follow_knot(head_new: (i32, i32), tail_old: (i32, i32)) -> (i32, i32) {
56
let mut dx = head_new.0 - tail_old.0;
67
let mut dy = head_new.1 - tail_old.1;

src/bin/2022_10/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,27 @@ fn register_history(input_lines: &str) -> Vec<i32> {
1717
register
1818
}
1919

20+
#[allow(clippy::cast_possible_truncation)]
21+
#[allow(clippy::cast_possible_wrap)]
2022
fn part1(input_lines: &str) -> i32 {
23+
const CYCLES: [usize; 6] = [20, 60, 100, 140, 180, 220];
2124
let mut ans = 0;
2225
let register = register_history(input_lines);
2326

24-
const CYCLES: [usize; 6] = [20, 60, 100, 140, 180, 220];
25-
2627
for cycle in CYCLES {
2728
ans += cycle as i32 * register[cycle - 1];
2829
}
2930

3031
ans
3132
}
3233

34+
#[allow(clippy::cast_possible_truncation)]
35+
#[allow(clippy::cast_possible_wrap)]
3336
fn part2(input_lines: &str) -> common::GridDisplay {
34-
let mut ans = common::GridDisplay { rows: Vec::new() };
35-
let register = register_history(input_lines);
36-
3737
const W: usize = 40;
3838
const H: usize = 6;
39+
let mut ans = common::GridDisplay { rows: Vec::new() };
40+
let register = register_history(input_lines);
3941

4042
for row in 0..H {
4143
let mut crt_row = String::new();

0 commit comments

Comments
 (0)