Skip to content

Commit b1c1794

Browse files
authored
refactor: simplify conditional logic using then method (#1860)
1 parent 82ee29e commit b1c1794

4 files changed

Lines changed: 42 additions & 55 deletions

File tree

find-msvc-tools/src/find_tools.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,21 @@ mod impl_ {
562562
version: &'static str,
563563
env_getter: &dyn EnvGetter,
564564
) -> Box<dyn Iterator<Item = PathBuf>> {
565-
let Some(instances) = vs15plus_instances(target, env_getter) else {
566-
return Box::new(iter::empty());
567-
};
568-
Box::new(instances.into_iter().filter_map(move |instance| {
569-
let installation_name = instance.installation_name()?;
570-
if installation_name.starts_with(&format!("VisualStudio/{}.", version))
571-
|| installation_name.starts_with(&format!("VisualStudioPreview/{}.", version))
572-
{
573-
Some(instance.installation_path()?)
574-
} else {
575-
None
576-
}
577-
}))
565+
Box::new(
566+
vs15plus_instances(target, env_getter)
567+
.into_iter()
568+
.flatten()
569+
.filter_map(move |instance| {
570+
instance
571+
.installation_name()
572+
.filter(|name| {
573+
["VisualStudio", "VisualStudioPreview"]
574+
.into_iter()
575+
.any(|kind| name.starts_with(&format!("{kind}/{version}.")))
576+
})
577+
.and_then(|_| instance.installation_path())
578+
}),
579+
)
578580
}
579581

580582
fn find_tool_in_vs16plus_path(
@@ -864,11 +866,10 @@ mod impl_ {
864866
// We use the first available host architecture that can build for the target
865867
let (host_path, host) = hosts.iter().find_map(|&x| {
866868
let candidate = path.join("bin").join(format!("Host{}", x));
867-
if candidate.join(target_dir).exists() {
868-
Some((candidate, x))
869-
} else {
870-
None
871-
}
869+
candidate
870+
.join(target_dir)
871+
.exists()
872+
.then_some((candidate, x))
872873
})?;
873874
// This is the path to the toolchain for a particular target, running
874875
// on a given host
@@ -963,12 +964,10 @@ mod impl_ {
963964

964965
fn atl_paths(target: TargetArch, path: &Path) -> Option<(PathBuf, PathBuf)> {
965966
let atl_path = path.join("atlmfc");
966-
let sub = target.as_vs_arch();
967-
if atl_path.exists() {
968-
Some((atl_path.join("lib").join(sub), atl_path.join("include")))
969-
} else {
970-
None
971-
}
967+
atl_path.exists().then(|| {
968+
let sub = target.as_vs_arch();
969+
(atl_path.join("lib").join(sub), atl_path.join("include"))
970+
})
972971
}
973972

974973
// For MSVC 14 we need to find the Universal CRT as well as either

src/bin/cc-shim.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,12 @@ fn out_file(program: &str) -> Option<PathBuf> {
4646

4747
let out_dir = PathBuf::from(env::var_os(OUT_DIR)?);
4848
// Find the first nonexistent candidate file to which the program's args can be written.
49-
Some((0..).find_map(|i| {
50-
let candidate = out_dir.join(format!("out{i}"));
51-
52-
if candidate.exists() {
53-
// If the file exists, commands have already run. Try again.
54-
None
55-
} else {
56-
Some(candidate)
57-
}
58-
}).unwrap_or_else(|| panic!("Cannot find the first nonexistent candidate file to which the program's args can be written under out_dir '{}'", out_dir.display())))
49+
Some(
50+
(0..)
51+
.map(|i| out_dir.join(format!("out{i}")))
52+
.find(|candidate| !candidate.exists())
53+
.unwrap_or_else(|| panic!("Cannot find the first nonexistent candidate file to which the program's args can be written under out_dir '{}'", out_dir.display()))
54+
)
5955
}
6056

6157
/// Record the args passed to the command, if this invocation records at all.

src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,11 +2397,8 @@ impl Build {
23972397
// So instead, we pass the deployment target with `-m*-version-min=`, and only
23982398
// pass it here on visionOS and Mac Catalyst where that option does not exist:
23992399
// https://github.com/rust-lang/cc-rs/issues/1383
2400-
let version = if target.os == "visionos" || target.env == "macabi" {
2401-
Some(self.apple_deployment_target(target))
2402-
} else {
2403-
None
2404-
};
2400+
let version = (target.os == "visionos" || target.env == "macabi")
2401+
.then(|| self.apple_deployment_target(target));
24052402

24062403
let clang_target =
24072404
target.llvm_target(&self.get_raw_target()?, version.as_deref());
@@ -3476,11 +3473,9 @@ impl Build {
34763473
let wrapper_path = Path::new(&rustc_wrapper);
34773474
let wrapper_stem = wrapper_path.file_stem()?;
34783475

3479-
if VALID_WRAPPERS.contains(&wrapper_stem.to_str()?) {
3480-
Some(Cow::Owned(rustc_wrapper))
3481-
} else {
3482-
None
3483-
}
3476+
VALID_WRAPPERS
3477+
.contains(&wrapper_stem.to_str()?)
3478+
.then_some(Cow::Owned(rustc_wrapper))
34843479
}
34853480

34863481
/// Returns compiler path, optional modifier name from whitelist, and arguments vec
@@ -3565,11 +3560,10 @@ impl Build {
35653560
Some(s) => Ok(s.as_deref().map(Path::new).map(Cow::Borrowed)),
35663561
None => {
35673562
if let Ok(stdlib) = self.getenv_with_target_prefixes("CXXSTDLIB") {
3568-
if stdlib.is_empty() {
3569-
Ok(None)
3570-
} else {
3571-
Ok(Some(Cow::Owned(Path::new(&stdlib).to_owned())))
3572-
}
3563+
Ok((!stdlib.is_empty())
3564+
.then_some(stdlib)
3565+
.map(PathBuf::from)
3566+
.map(Cow::from))
35733567
} else {
35743568
let target = self.get_target()?;
35753569
if target.env == "msvc" {

src/utilities.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ impl<T> OnceLock<T> {
9494
}
9595

9696
pub(crate) fn get(&self) -> Option<&T> {
97-
if self.is_initialized() {
98-
// Safe b/c checked is_initialized
99-
Some(unsafe { self.get_unchecked() })
100-
} else {
101-
None
102-
}
97+
self.is_initialized().then(|| {
98+
// SAFETY: `is_initialized()` returned `true`, so the value is initialized.
99+
unsafe { self.get_unchecked() }
100+
})
103101
}
104102
}
105103

0 commit comments

Comments
 (0)