Skip to content

Commit

Permalink
chore: apply fixes to all the codebase with cargo clippy --fix
Browse files Browse the repository at this point in the history
Checking the source code with Clippy is a good practice that helps maintaining the code in good shape and ensures an homogeneous style. Most of the automatic fixes in this commit are small cosmetic changes, but some of them improve legibility or even performance.

If this change is accepted I plan to keep applying more fixes for hints that Clippy reports but can't fix automatically.
  • Loading branch information
plusvic committed Sep 4, 2024
1 parent 6951916 commit 35d421f
Show file tree
Hide file tree
Showing 106 changed files with 521 additions and 604 deletions.
35 changes: 16 additions & 19 deletions ci-gen/src/ghwf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ use crate::yaml::Yaml;

#[allow(dead_code)]
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Default)]
pub enum Env {
WindowsLatest,
#[default]
UbuntuLatest,
MacosLatest,
}

impl Default for Env {
fn default() -> Self {
Env::UbuntuLatest
}
}

impl fmt::Display for Env {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -60,7 +57,7 @@ impl Step {
name: name.to_owned(),
uses: Some(uses.to_owned()),
env: env
.into_iter()
.iter()
.map(|(k, v)| (String::from(*k), String::from(*v)))
.collect(),
with: Some(with),
Expand All @@ -85,16 +82,16 @@ impl Step {
}
}

impl Into<Yaml> for Step {
fn into(self) -> Yaml {
impl From<Step> for Yaml {
fn from(val: Step) -> Self {
let Step {
name,
uses,
with,
run,
shell,
env,
} = self;
} = val;
let mut entries = Vec::new();
entries.push(("name", Yaml::string(name)));
if let Some(uses) = uses {
Expand Down Expand Up @@ -134,23 +131,23 @@ impl Job {
}
}

impl Into<(String, Yaml)> for Job {
fn into(self) -> (String, Yaml) {
assert!(!self.id.is_empty());
impl From<Job> for (String, Yaml) {
fn from(val: Job) -> Self {
assert!(!val.id.is_empty());
let mut entries = vec![
("name", Yaml::string(self.name)),
("runs-on", Yaml::string(format!("{}", self.runs_on))),
("name", Yaml::string(val.name)),
("runs-on", Yaml::string(format!("{}", val.runs_on))),
];
if let Some(timeout_minutes) = self.timeout_minutes {
if let Some(timeout_minutes) = val.timeout_minutes {
entries.push((
"timeout-minutes",
Yaml::string(format!("{}", timeout_minutes)),
));
}
if !self.env.is_empty() {
entries.push(("env", Yaml::map(self.env)));
if !val.env.is_empty() {
entries.push(("env", Yaml::map(val.env)));
}
entries.push(("steps", Yaml::list(self.steps)));
(self.id, Yaml::map(entries))
entries.push(("steps", Yaml::list(val.steps)));
(val.id, Yaml::map(entries))
}
}
16 changes: 8 additions & 8 deletions ci-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ enum Features {
impl Features {
fn flag(&self) -> String {
match self {
Features::Default => format!(""),
Features::Default => String::new(),
Features::Specific(f) => format!("--features={}", f.join(",")),
Features::All => format!("--all-features"),
Features::All => "--all-features".to_string(),
}
}

Expand All @@ -72,25 +72,25 @@ impl Features {

fn id(&self) -> String {
match self {
Features::Default => format!("default-features"),
Features::All => format!("all-features"),
Features::Default => "default-features".to_string(),
Features::All => "all-features".to_string(),
Features::Specific(s) => s.join("-"),
}
}

fn name(&self) -> String {
match self {
Features::Default => format!("default features"),
Features::All => format!("all features"),
Features::Default => "default features".to_string(),
Features::All => "all features".to_string(),
Features::Specific(s) => s.join(","),
}
}
}

fn self_check_job() -> Job {
Job {
id: format!("self-check"),
name: format!("CI self-check"),
id: "self-check".to_string(),
name: "CI self-check".to_string(),
runs_on: LINUX.ghwf,
steps: vec![
checkout_sources(),
Expand Down
11 changes: 4 additions & 7 deletions ci-gen/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,19 @@ pub struct YamlWriter {
}

#[derive(Eq, PartialEq)]
#[derive(Default)]
enum MinusState {
#[default]
No,
Yes,
Already,
}

impl Default for MinusState {
fn default() -> Self {
MinusState::No
}
}

impl YamlWriter {
pub fn write_line(&mut self, line: &str) {
if line.is_empty() {
self.buffer.push_str("\n");
self.buffer.push('\n');
} else {
for _ in 0..self.indent {
self.buffer.push_str(" ");
Expand All @@ -95,7 +92,7 @@ impl YamlWriter {
}

self.buffer.push_str(line);
self.buffer.push_str("\n");
self.buffer.push('\n');
}
}

Expand Down
13 changes: 5 additions & 8 deletions protobuf-codegen/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ use crate::gen_and_write::gen_and_write;
use crate::Customize;

#[derive(Debug)]
#[derive(Default)]
enum WhichParser {
#[default]
Pure,
Protoc,
}

impl Default for WhichParser {
fn default() -> WhichParser {
WhichParser::Pure
}
}

#[derive(Debug, thiserror::Error)]
enum CodegenError {
Expand Down Expand Up @@ -221,9 +218,9 @@ impl Codegen {

if self.create_out_dir {
if out_dir.exists() {
fs::remove_dir_all(&out_dir)?;
fs::remove_dir_all(out_dir)?;
}
fs::create_dir(&out_dir)?;
fs::create_dir(out_dir)?;
}

let mut parser = Parser::new();
Expand Down Expand Up @@ -257,7 +254,7 @@ impl Codegen {
&parsed_and_typechecked.file_descriptors,
&parsed_and_typechecked.parser,
&parsed_and_typechecked.relative_paths,
&out_dir,
out_dir,
&self.customize,
&*self.customize_callback,
)
Expand Down
6 changes: 2 additions & 4 deletions protobuf-codegen/src/gen/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ pub(crate) fn gen_all(
};

for file_name in files_to_generate {
let file = files_map.get(file_name.as_path()).expect(&format!(
"file not found in file descriptors: {:?}, files: {:?}",
let file = files_map.get(file_name.as_path()).unwrap_or_else(|| panic!("file not found in file descriptors: {:?}, files: {:?}",
file_name,
files_map.keys()
));
files_map.keys()));
let gen_file_result = gen_file(file, &files_map, &root_scope, &customize, parser)?;
results.push(gen_file_result.compiler_plugin_result);
mods.push(gen_file_result.mod_name);
Expand Down
12 changes: 7 additions & 5 deletions protobuf-codegen/src/gen/code_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ impl<'a> CodeWriter<'a> {
}

pub(crate) fn with_no_error(f: impl FnOnce(&mut CodeWriter)) -> String {
Self::with_impl::<Infallible, _>(|w| Ok(f(w))).unwrap_or_else(|e| match e {})
Self::with_impl::<Infallible, _>(|w| {
f(w);
Ok(())
}).unwrap_or_else(|e| match e {})
}

pub(crate) fn with<F>(f: F) -> anyhow::Result<String>
Expand All @@ -47,7 +50,7 @@ impl<'a> CodeWriter<'a> {

pub(crate) fn write_line<S: AsRef<str>>(&mut self, line: S) {
if line.as_ref().is_empty() {
self.writer.push_str("\n");
self.writer.push('\n');
} else {
let s: String = [self.indent.as_ref(), line.as_ref(), "\n"].concat();
self.writer.push_str(&s);
Expand Down Expand Up @@ -96,7 +99,7 @@ impl<'a> CodeWriter<'a> {
}

pub(crate) fn unimplemented(&mut self) {
self.write_line(format!("unimplemented!();"));
self.write_line("unimplemented!();");
}

pub(crate) fn indented<F>(&mut self, cb: F)
Expand Down Expand Up @@ -333,8 +336,7 @@ impl<'a> CodeWriter<'a> {

let lines = doc
.iter()
.map(|doc| doc.lines())
.flatten()
.flat_map(|doc| doc.lines())
.collect::<Vec<_>>();

// Skip comments with code blocks to avoid rustdoc trying to compile them.
Expand Down
6 changes: 3 additions & 3 deletions protobuf-codegen/src/gen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'a> EnumGen<'a> {
w.comment("Note: you cannot use pattern matching for enums with allow_alias option");
}
w.derive(&derive);
let ref type_name = self.type_name;
let type_name = &self.type_name;
write_protoc_insertion_point_for_enum(
w,
&self.customize.for_elem,
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<'a> EnumGen<'a> {
}

fn write_impl_enum_full(&self, w: &mut CodeWriter) {
let ref type_name = self.type_name;
let type_name = &self.type_name;
w.impl_for_block(
&format!(
"{}::EnumFull",
Expand Down Expand Up @@ -351,7 +351,7 @@ impl<'a> EnumGen<'a> {
});
w.write_line("};");
}
w.write_line(&format!("Self::enum_descriptor().value_by_index(index)"));
w.write_line("Self::enum_descriptor().value_by_index(index)");
});
}

Expand Down
4 changes: 2 additions & 2 deletions protobuf-codegen/src/gen/field/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl AccessorFn {
s.push_str("::<_");
for p in &self.type_params {
s.push_str(", ");
s.push_str(&p);
s.push_str(p);
}
s.push_str(">");
s.push('>');
s
}
}
Expand Down
Loading

0 comments on commit 35d421f

Please sign in to comment.