From 21443490911e60f4fd8f9e155562ecab57d3a467 Mon Sep 17 00:00:00 2001 From: InventiveCoder Date: Mon, 18 Mar 2024 20:32:03 +0800 Subject: [PATCH] refactor: Optimize code based on cargo clippy suggestions Signed-off-by: InventiveCoder --- src/lib.rs | 59 +++++++++++++++++++++++++---------------------------- src/tool.rs | 6 ++---- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b539abd0c..a449b12be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -463,9 +463,9 @@ impl Build { /// .compile("foo"); /// ``` pub fn includes

(&mut self, dirs: P) -> &mut Build - where - P: IntoIterator, - P::Item: AsRef, + where + P: IntoIterator, + P::Item: AsRef, { for dir in dirs { self.include(dir); @@ -758,9 +758,9 @@ impl Build { /// Add files which will be compiled pub fn files

(&mut self, p: P) -> &mut Build - where - P: IntoIterator, - P::Item: AsRef, + where + P: IntoIterator, + P::Item: AsRef, { for file in p.into_iter() { self.file(file); @@ -769,7 +769,7 @@ impl Build { } /// Get the files which will be compiled - pub fn get_files(&self) -> impl Iterator { + pub fn get_files(&self) -> impl Iterator { self.files.iter().map(AsRef::as_ref) } @@ -1202,9 +1202,9 @@ impl Build { #[doc(hidden)] pub fn __set_env(&mut self, a: A, b: B) -> &mut Build - where - A: AsRef, - B: AsRef, + where + A: AsRef, + B: AsRef, { self.env.push((a.as_ref().into(), b.as_ref().into())); self @@ -1550,9 +1550,9 @@ impl Build { } fn cell_update(cell: &Cell, f: F) - where - T: Default, - F: FnOnce(T) -> T, + where + T: Default, + F: FnOnce(T) -> T, { let old = cell.take(); let new = f(old); @@ -1905,7 +1905,7 @@ impl Build { ToolFamily::Clang { .. } => { if !cmd.has_internal_target_arg && !(target.contains("android") - && android_clang_compiler_uses_target_arg_internally(&cmd.path)) + && android_clang_compiler_uses_target_arg_internally(&cmd.path)) { let (arch, rest) = target.split_once('-').ok_or_else(|| { Error::new( @@ -1941,7 +1941,7 @@ impl Build { "--target={}-apple-ios{}-simulator", arch, deployment_target ) - .into(), + .into(), ); } } else if target.contains("watchos-sim") { @@ -1960,7 +1960,7 @@ impl Build { "--target={}-apple-watchos{}-simulator", arch, deployment_target ) - .into(), + .into(), ); } } else if target.contains("tvos-sim") || target.contains("x86_64-apple-tvos") { @@ -1979,7 +1979,7 @@ impl Build { "--target={}-apple-tvos{}-simulator", arch, deployment_target ) - .into(), + .into(), ); } } else if target.contains("aarch64-apple-tvos") { @@ -2006,7 +2006,7 @@ impl Build { target_info::RISCV_ARCH_MAPPING[index].1, rest ) - .into(), + .into(), ); } else if target.contains("uefi") { if target.contains("x86_64") { @@ -2060,14 +2060,11 @@ impl Build { } else { cmd.push_cc_arg(format!("--target={}", target).into()); } - } else { - if target.contains("i586") { - cmd.push_cc_arg("-arch:IA32".into()); - } else if target.contains("arm64ec") { - cmd.push_cc_arg("-arm64EC".into()); - } + } else if target.contains("i586") { + cmd.push_cc_arg("-arch:IA32".into()); + } else if target.contains("arm64ec") { + cmd.push_cc_arg("-arm64EC".into()); } - // There is a check in corecrt.h that will generate a // compilation error if // _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE is @@ -2607,7 +2604,7 @@ impl Build { "-m{}simulator-version-min={}", sdk_details.sim_prefix, min_version ) - .into(), + .into(), ); } AppleArchSpec::Catalyst(_) => {} @@ -3387,7 +3384,7 @@ impl Build { "x86_64-unknown-netbsd" => Some("x86_64--netbsd"), _ => None, } - .map(|x| x.to_owned())) + .map(|x| x.to_owned())) } /// Some platforms have multiple, compatible, canonical prefixes. Look through @@ -3414,12 +3411,11 @@ impl Build { }) }) .map(|prefix| *prefix) - .or_else(|| // If no toolchain was found, provide the first toolchain that was passed in. // This toolchain has been shown not to exist, however it will appear in the // error that is shown to the user which should make it easier to search for // where it should be obtained. - prefixes.first().map(|prefix| *prefix)) + .or_else(|| prefixes.first().map(|prefix| *prefix)) } fn get_target(&self) -> Result, Error> { @@ -3623,7 +3619,7 @@ impl Build { "xcrun", &self.cargo_output, ) - .ok()?; + .ok()?; let version = std::str::from_utf8(&version).ok()?.trim().to_owned(); @@ -3753,6 +3749,7 @@ enum AppleOs { WatchOs, TvOs, } + impl std::fmt::Debug for AppleOs { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { @@ -3949,7 +3946,7 @@ fn search_programs(cc: &mut Command, prog: &str, cargo_output: &CargoOutput) -> // this doesn't concern the compilation so we always want to show warnings. cargo_output, ) - .ok()?; + .ok()?; // clang driver appears to be forcing UTF-8 output even on Windows, // hence from_utf8 is assumed to be usable in all cases. let search_dirs = std::str::from_utf8(&search_dirs).ok()?; diff --git a/src/tool.rs b/src/tool.rs index d8623cccc..4ef3de2c4 100644 --- a/src/tool.rs +++ b/src/tool.rs @@ -233,10 +233,8 @@ impl Tool { if chars.next() != Some('/') { return false; } - } else if self.is_like_gnu() || self.is_like_clang() { - if chars.next() != Some('-') { - return false; - } + } else if (self.is_like_gnu() || self.is_like_clang()) && chars.next() != Some('-') { + return false; } // Check for existing optimization flags (-O, /O)