Skip to content

Commit

Permalink
refactor: Optimize code based on cargo clippy suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: InventiveCoder <liucongcong@outlook.com>
  • Loading branch information
InventiveCoder committed Mar 18, 2024
1 parent f36d6a7 commit 2144349
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
59 changes: 28 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ impl Build {
/// .compile("foo");
/// ```
pub fn includes<P>(&mut self, dirs: P) -> &mut Build
where
P: IntoIterator,
P::Item: AsRef<Path>,
where
P: IntoIterator,
P::Item: AsRef<Path>,
{
for dir in dirs {
self.include(dir);
Expand Down Expand Up @@ -758,9 +758,9 @@ impl Build {

/// Add files which will be compiled
pub fn files<P>(&mut self, p: P) -> &mut Build
where
P: IntoIterator,
P::Item: AsRef<Path>,
where
P: IntoIterator,
P::Item: AsRef<Path>,
{
for file in p.into_iter() {
self.file(file);
Expand All @@ -769,7 +769,7 @@ impl Build {
}

/// Get the files which will be compiled
pub fn get_files(&self) -> impl Iterator<Item = &Path> {
pub fn get_files(&self) -> impl Iterator<Item=&Path> {
self.files.iter().map(AsRef::as_ref)
}

Expand Down Expand Up @@ -1202,9 +1202,9 @@ impl Build {

#[doc(hidden)]
pub fn __set_env<A, B>(&mut self, a: A, b: B) -> &mut Build
where
A: AsRef<OsStr>,
B: AsRef<OsStr>,
where
A: AsRef<OsStr>,
B: AsRef<OsStr>,
{
self.env.push((a.as_ref().into(), b.as_ref().into()));
self
Expand Down Expand Up @@ -1550,9 +1550,9 @@ impl Build {
}

fn cell_update<T, F>(cell: &Cell<T>, f: F)
where
T: Default,
F: FnOnce(T) -> T,
where
T: Default,
F: FnOnce(T) -> T,
{
let old = cell.take();
let new = f(old);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1941,7 +1941,7 @@ impl Build {
"--target={}-apple-ios{}-simulator",
arch, deployment_target
)
.into(),
.into(),
);
}
} else if target.contains("watchos-sim") {
Expand All @@ -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") {
Expand All @@ -1979,7 +1979,7 @@ impl Build {
"--target={}-apple-tvos{}-simulator",
arch, deployment_target
)
.into(),
.into(),
);
}
} else if target.contains("aarch64-apple-tvos") {
Expand All @@ -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") {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2607,7 +2604,7 @@ impl Build {
"-m{}simulator-version-min={}",
sdk_details.sim_prefix, min_version
)
.into(),
.into(),
);
}
AppleArchSpec::Catalyst(_) => {}
Expand Down Expand Up @@ -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
Expand All @@ -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<Arc<str>, Error> {
Expand Down Expand Up @@ -3623,7 +3619,7 @@ impl Build {
"xcrun",
&self.cargo_output,
)
.ok()?;
.ok()?;

let version = std::str::from_utf8(&version).ok()?.trim().to_owned();

Expand Down Expand Up @@ -3753,6 +3749,7 @@ enum AppleOs {
WatchOs,
TvOs,
}

impl std::fmt::Debug for AppleOs {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -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()?;
Expand Down
6 changes: 2 additions & 4 deletions src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2144349

Please sign in to comment.