Skip to content

Commit dbb91b6

Browse files
committed
cargo clippy pedantic
1 parent a12b133 commit dbb91b6

File tree

11 files changed

+54
-64
lines changed

11 files changed

+54
-64
lines changed

ascii/src/lib.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,22 @@ impl<'a> Tokens<'a> {
205205
let mut whole_string = String::new();
206206
let mut color = &DynColors::Ansi(AnsiColors::Default);
207207

208-
self.truncate(start, end).for_each(|token| {
209-
match token {
210-
Token::Char(chr) => {
211-
width = width.saturating_sub(1);
212-
colored_segment.push(chr);
213-
}
214-
Token::Color(col) => {
215-
add_styled_segment(&mut whole_string, &colored_segment, *color, bold);
216-
colored_segment = String::new();
217-
color = colors
218-
.get(col as usize)
219-
.unwrap_or(&DynColors::Ansi(AnsiColors::Default));
220-
}
221-
Token::Space => {
222-
width = width.saturating_sub(1);
223-
colored_segment.push(' ')
224-
}
225-
};
208+
self.truncate(start, end).for_each(|token| match token {
209+
Token::Char(chr) => {
210+
width = width.saturating_sub(1);
211+
colored_segment.push(chr);
212+
}
213+
Token::Color(col) => {
214+
add_styled_segment(&mut whole_string, &colored_segment, *color, bold);
215+
colored_segment = String::new();
216+
color = colors
217+
.get(col as usize)
218+
.unwrap_or(&DynColors::Ansi(AnsiColors::Default));
219+
}
220+
Token::Space => {
221+
width = width.saturating_sub(1);
222+
colored_segment.push(' ');
223+
}
226224
});
227225

228226
add_styled_segment(&mut whole_string, &colored_segment, *color, bold);
@@ -267,15 +265,15 @@ fn token<R>(s: &str, predicate: impl FnOnce(char) -> Option<R>) -> ParseResult<R
267265

268266
/// Parses a color indicator of the format `{n}` where `n` is a digit.
269267
fn color_token(s: &str) -> ParseResult<Token> {
270-
let (s, _) = token(s, succeed_when(|c| c == '{'))?;
268+
let (s, ()) = token(s, succeed_when(|c| c == '{'))?;
271269
let (s, color_index) = token(s, |c| c.to_digit(10))?;
272-
let (s, _) = token(s, succeed_when(|c| c == '}'))?;
270+
let (s, ()) = token(s, succeed_when(|c| c == '}'))?;
273271
Some((s, Token::Color(color_index)))
274272
}
275273

276274
/// Parses a space.
277275
fn space_token(s: &str) -> ParseResult<Token> {
278-
token(s, succeed_when(|c| c == ' ')).map(|(s, _)| (s, Token::Space))
276+
token(s, succeed_when(|c| c == ' ')).map(|(s, ())| (s, Token::Space))
279277
}
280278

281279
/// Parses any arbitrary character. This cannot fail.

build.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use lazy_static::lazy_static;
21
use regex::Regex;
32
use std::collections::HashMap;
43
use std::env;
54
use std::error::Error;
65
use std::fs::{self, File};
76
use std::path::Path;
7+
use std::sync::LazyLock;
88
use tera::{Context, Tera};
99

1010
fn main() -> Result<(), Box<dyn Error>> {
@@ -37,12 +37,9 @@ fn strip_color_tokens_filter(
3737
value: &tera::Value,
3838
_args: &HashMap<String, tera::Value>,
3939
) -> tera::Result<tera::Value> {
40-
lazy_static! {
41-
static ref COLOR_INDEX_REGEX: Regex = Regex::new(r"\{\d+\}").unwrap();
42-
}
43-
let s = match value {
44-
tera::Value::String(s) => s,
45-
_ => return Err(tera::Error::msg("expected string")),
40+
static COLOR_INDEX_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{\d+\}").unwrap());
41+
let tera::Value::String(s) = value else {
42+
return Err(tera::Error::msg("expected string"));
4643
};
4744
Ok(tera::Value::String(
4845
COLOR_INDEX_REGEX.replace_all(s, "").to_string(),
@@ -53,20 +50,17 @@ fn hex_to_rgb_filter(
5350
value: &tera::Value,
5451
_args: &HashMap<String, tera::Value>,
5552
) -> tera::Result<tera::Value> {
56-
let hex_string = match value {
57-
tera::Value::String(s) => s,
58-
_ => return Err(tera::Error::msg("expected string")),
53+
let tera::Value::String(hex_string) = value else {
54+
return Err(tera::Error::msg("expected string"));
5955
};
60-
let hex_string = match hex_string.strip_prefix('#') {
61-
Some(s) => s,
62-
None => return Err(tera::Error::msg("expected hex string starting with `#`")),
56+
let Some(hex_string) = hex_string.strip_prefix('#') else {
57+
return Err(tera::Error::msg("expected hex string starting with `#`"));
6358
};
6459
if hex_string.len() != 6 {
6560
return Err(tera::Error::msg("expected a 6 digit hex string"));
6661
}
67-
let channel_bytes = match u32::from_str_radix(hex_string, 16) {
68-
Ok(n) => n,
69-
Err(_) => return Err(tera::Error::msg("expected a valid hex string")),
62+
let Ok(channel_bytes) = u32::from_str_radix(hex_string, 16) else {
63+
return Err(tera::Error::msg("expected a valid hex string"));
7064
};
7165
let r = (channel_bytes >> 16) & 0xFF;
7266
let g = (channel_bytes >> 8) & 0xFF;

manifest/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ fn parse_cargo_manifest(path: &Path) -> Result<Manifest> {
4545
let m = cargo_toml::Manifest::from_path(path)
4646
.with_context(|| format!("Failed to parse Cargo.toml at '{}'", path.display()))?;
4747
let package = m.package.context("Not a package (only a workspace)")?;
48-
let description = package.description().map(|v| v.into());
48+
let description = package.description().map(Into::into);
4949

5050
Ok(Manifest {
5151
manifest_type: ManifestType::Cargo,
5252
number_of_dependencies: m.dependencies.len(),
5353
name: package.name.clone(),
5454
description,
5555
version: package.version().into(),
56-
license: package.license().map(|x| x.into()),
56+
license: package.license().map(Into::into),
5757
})
5858
}
5959

src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl Default for InfoCliOptions {
274274
impl Default for TextForamttingCliOptions {
275275
fn default() -> Self {
276276
TextForamttingCliOptions {
277-
text_colors: Default::default(),
277+
text_colors: Vec::default(),
278278
iso_time: Default::default(),
279279
number_separator: NumberSeparator::Plain,
280280
no_bold: Default::default(),
@@ -296,7 +296,7 @@ impl Default for ImageCliOptions {
296296
fn default() -> Self {
297297
ImageCliOptions {
298298
image: Option::default(),
299-
image_protocol: Default::default(),
299+
image_protocol: Option::default(),
300300
color_resolution: 16,
301301
}
302302
}
@@ -353,7 +353,7 @@ pub enum NumberSeparator {
353353
}
354354

355355
impl NumberSeparator {
356-
fn separator(&self) -> &'static str {
356+
fn separator(self) -> &'static str {
357357
match self {
358358
Self::Plain => "",
359359
Self::Comma => ",",

src/info/authors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl InfoField for AuthorsInfo {
162162
fn title(&self) -> String {
163163
let mut title: String = "Author".into();
164164
if self.authors.len() > 1 {
165-
title.push('s')
165+
title.push('s');
166166
}
167167
title
168168
}

src/info/churn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ fn compute_file_churns(
8787
Ok(number_of_commits_by_file_path_sorted
8888
.into_iter()
8989
.filter_map(|(file_path, nbr_of_commits)| {
90-
if !glob_set.is_match(file_path.to_string()) {
90+
if glob_set.is_match(file_path.to_string()) {
91+
None
92+
} else {
9193
Some(FileChurn::new(
9294
file_path.to_string(),
9395
*nbr_of_commits,
9496
number_separator,
9597
))
96-
} else {
97-
None
9898
}
9999
})
100100
.take(number_of_file_churns_to_display)

src/info/contributors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl InfoField for ContributorsInfo {
3232
if self.total_number_of_authors > self.number_of_authors_to_display {
3333
format_number(&self.total_number_of_authors, self.number_separator)
3434
} else {
35-
"".to_string()
35+
String::new()
3636
}
3737
}
3838

src/info/git/metrics.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::sig::Sig;
2-
use anyhow::Result;
32
use gix::bstr::BString;
43
use gix::date::Time;
54
use std::collections::HashMap;
@@ -21,7 +20,7 @@ impl GitMetrics {
2120
churn_pool_size: usize,
2221
time_of_first_commit: Option<Time>,
2322
time_of_most_recent_commit: Option<Time>,
24-
) -> Result<Self> {
23+
) -> Self {
2524
let total_number_of_commits = number_of_commits_by_signature.values().sum();
2625
let total_number_of_authors = number_of_commits_by_signature.len();
2726

@@ -30,14 +29,14 @@ impl GitMetrics {
3029
.and_then(|a| time_of_most_recent_commit.map(|b| (a, b)))
3130
.unwrap_or_default();
3231

33-
Ok(Self {
32+
Self {
3433
number_of_commits_by_signature,
3534
number_of_commits_by_file_path,
3635
total_number_of_authors,
3736
total_number_of_commits,
3837
churn_pool_size,
39-
time_of_first_commit,
4038
time_of_most_recent_commit,
41-
})
39+
time_of_first_commit,
40+
}
4241
}
4342
}

src/info/git/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn traverse_commit_graph(
3333
let total_number_of_commits = Arc::new(AtomicUsize::default());
3434

3535
let num_threads = std::thread::available_parallelism()
36-
.map(|p| p.get())
36+
.map(std::num::NonZero::get)
3737
.unwrap_or(1);
3838
let commit_graph = repo.commit_graph().ok();
3939
let can_use_author_threads = num_threads > 1 && commit_graph.is_some();
@@ -54,7 +54,7 @@ pub fn traverse_commit_graph(
5454
&is_traversal_complete,
5555
&total_number_of_commits,
5656
min_churn_pool_size,
57-
)?;
57+
);
5858

5959
let author_threads = can_use_author_threads
6060
.then(|| get_author_channel(repo, num_threads, no_bots.clone(), &mailmap));
@@ -117,7 +117,7 @@ pub fn traverse_commit_graph(
117117
churn_pool_size,
118118
time_of_first_commit,
119119
time_of_most_recent_commit,
120-
)?;
120+
);
121121

122122
Ok(git_metrics)
123123
}
@@ -177,7 +177,7 @@ fn get_churn_channel(
177177
is_traversal_complete: &Arc<AtomicBool>,
178178
total_number_of_commits: &Arc<AtomicUsize>,
179179
max_churn_pool_size: Option<usize>,
180-
) -> Result<(JoinHandle<Result<ChurnPair>>, Sender<ObjectId>)> {
180+
) -> (JoinHandle<Result<ChurnPair>>, Sender<ObjectId>) {
181181
let (tx, rx) = channel::<gix::hash::ObjectId>();
182182
let thread = std::thread::spawn({
183183
let repo = repo.clone();
@@ -209,7 +209,7 @@ fn get_churn_channel(
209209
}
210210
});
211211

212-
Ok((thread, tx))
212+
(thread, tx)
213213
}
214214

215215
fn should_break(
@@ -261,7 +261,7 @@ fn compute_diff_with_parent(
261261
let new_tree = commit.tree()?;
262262
let changes =
263263
repo.diff_tree_to_tree(&old_tree, &new_tree, Options::default().with_rewrites(None))?;
264-
for change in changes.iter() {
264+
for change in &changes {
265265
let is_file_change = match change {
266266
Change::Addition { entry_mode, .. } | Change::Modification { entry_mode, .. } => {
267267
entry_mode.is_blob()

src/info/head.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ impl HeadRefs {
2121

2222
impl std::fmt::Display for HeadRefs {
2323
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24-
if !self.refs.is_empty() {
24+
if self.refs.is_empty() {
25+
write!(f, "{}", self.short_commit_id)
26+
} else {
2527
let refs_str = self
2628
.refs
2729
.iter()
28-
.map(|ref_name| ref_name.as_str())
30+
.map(String::as_str)
2931
.collect::<Vec<&str>>()
3032
.join(", ");
3133
write!(f, "{} ({})", self.short_commit_id, refs_str)
32-
} else {
33-
write!(f, "{}", self.short_commit_id)
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)