From ee08ff4b3397e8f6267093a0a0153013020011c1 Mon Sep 17 00:00:00 2001 From: vasilito Date: Sat, 18 Jul 2026 17:54:02 +0900 Subject: [PATCH] refactor: manual clippy fixes (manual_strip, borrowed_box, redundant_struct) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reduced warnings from 96 to 86. Categories fixed (10 warnings): - manual_strip (5): starts_with + manual slice → strip_prefix/strip_suffix - borrowed_box (2): &Box → &impl Fn (fs.rs signatures + caller) - redundant_struct_expression (2): remove ..Default::default() when all fields set - field_reassign_with_default (1): Default + field assign → struct literal Verification: cargo check ✅, cargo test --lib ✅ 38/38, cargo clippy 86 remaining. --- src/cook/cook_build.rs | 4 ++-- src/cook/fetch.rs | 1 - src/cook/fs.rs | 25 +++++++++++-------------- src/cook/package.rs | 1 - src/recipe.rs | 4 +--- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/cook/cook_build.rs b/src/cook/cook_build.rs index 7f3ee3d1f7..047214d848 100644 --- a/src/cook/cook_build.rs +++ b/src/cook/cook_build.rs @@ -1001,14 +1001,14 @@ pub fn build( } move_dir_all_fn( &stage_dir_tmp, - &Box::new(|path: PathBuf| { + &|path: PathBuf| { for (glob, dst) in &globs { if glob.is_match(&path) { return Some(dst.as_path()); } } None - }), + }, ) .map_err(|e| format!("Unable to move {e:?}"))?; diff --git a/src/cook/fetch.rs b/src/cook/fetch.rs index cabf9726c0..ec12714bf7 100644 --- a/src/cook/fetch.rs +++ b/src/cook/fetch.rs @@ -1205,7 +1205,6 @@ pub fn fetch_remote( commit_identifier: pkg_toml.commit_identifier.clone(), source_identifier: pkg_toml.source_identifier.clone(), time_identifier: pkg_toml.time_identifier.clone(), - ..Default::default() }, )?; diff --git a/src/cook/fs.rs b/src/cook/fs.rs index 06ccfa0a6a..792616e47b 100644 --- a/src/cook/fs.rs +++ b/src/cook/fs.rs @@ -62,7 +62,7 @@ pub fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> io::Result< pub fn move_dir_all_fn<'a>( src: impl AsRef, - mv: &'a Box Option<&'a Path>>, + mv: &'a impl Fn(PathBuf) -> Option<&'a Path>, ) -> io::Result<()> { move_dir_all_inner_fn(&src, &src, mv) } @@ -70,7 +70,7 @@ pub fn move_dir_all_fn<'a>( fn move_dir_all_inner_fn<'a>( src: impl AsRef, srcrel: impl AsRef, - mv: &'a Box Option<&'a Path>>, + mv: &'a impl Fn(PathBuf) -> Option<&'a Path>, ) -> io::Result<()> { let mut files = Vec::new(); for entry in fs::read_dir(&src)? { @@ -349,8 +349,8 @@ pub fn read_to_string(path: &Path) -> Result { pub fn get_git_head_rev(dir: &PathBuf) -> Result<(String, bool)> { let git_head = dir.join(".git/HEAD"); let head_str = read_to_string(&git_head)?; - if head_str.starts_with("ref: ") { - let entry = head_str["ref: ".len()..].trim_end(); + if let Some(stripped) = head_str.strip_prefix("ref: ") { + let entry = stripped.trim_end(); let git_ref = dir.join(".git").join(entry); let ref_str = if git_ref.is_file() { read_to_string(&git_ref)? @@ -455,11 +455,11 @@ pub fn get_git_remote_tracking(dir: &PathBuf) -> Result<(String, String, String, if line.starts_with('[') { break; } - if line.starts_with("remote = ") { - remote_name = Some(line["remote = ".len()..].trim().to_string()); + if let Some(stripped) = line.strip_prefix("remote = ") { + remote_name = Some(stripped.trim().to_string()); } - if line.starts_with("merge = ") { - remote_branch = Some(get_git_branch_name(line["merge = ".len()..].trim())?); + if let Some(stripped) = line.strip_prefix("merge = ") { + remote_branch = Some(get_git_branch_name(stripped.trim())?); } } } @@ -488,8 +488,8 @@ pub fn get_git_remote_tracking(dir: &PathBuf) -> Result<(String, String, String, if line.starts_with('[') { break; } - if line.starts_with("url = ") { - let mut url = line["url = ".len()..].trim(); + if let Some(stripped) = line.strip_prefix("url = ") { + let mut url = stripped.trim(); url = chop_dot_git(url); remote_url = Some(url.to_string()); } @@ -510,10 +510,7 @@ pub fn get_git_remote_tracking(dir: &PathBuf) -> Result<(String, String, String, } pub(crate) fn chop_dot_git(url: &str) -> &str { - if url.ends_with(".git") { - return &url[..url.len() - ".git".len()]; - } - url + url.strip_suffix(".git").unwrap_or(url) } fn get_git_branch_name(local_branch_path: &str) -> Result { diff --git a/src/cook/package.rs b/src/cook/package.rs index 74851deff1..57b3658cc2 100644 --- a/src/cook/package.rs +++ b/src/cook/package.rs @@ -220,7 +220,6 @@ pub fn package_toml( commit_identifier: ident_source.commit_identifier, source_identifier: ident_source.source_identifier, time_identifier: ident_source.time_identifier, - ..Default::default() }; serialize_and_write(&toml_path, &package)?; diff --git a/src/recipe.rs b/src/recipe.rs index 53081a24f9..d4e366c435 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -156,9 +156,7 @@ pub struct Recipe { impl BuildRecipe { pub fn new(kind: BuildKind) -> Self { - let mut build = Self::default(); - build.kind = kind; - build + Self { kind, ..Default::default() } } pub fn set_as_remote(&mut self) {